How Parabolic SAR is Calculated

The Parabolic SAR (Stop and Reverse) is a trend-following indicator developed by J. Welles Wilder Jr., the creator of other seminal indicators like the RSI and ADX. Its primary purpose is to identify the direction of a trend and provide exit points. The “SAR” in its name stands for “Stop and Reverse,” which aptly describes its function: when the trend reverses, the indicator stops and repositions itself on the other side of the price.

Visually, it appears as a series of dots on the chart, either below the price in an uptrend or above the price in a downtrend. These dots act as a trailing stop-loss, moving closer to the price as the trend progresses. This lesson breaks down the precise mathematical calculation behind this elegant and effective indicator.

The Parabolic SAR Formula

The entire logic of the Parabolic SAR is captured in a single iterative formula. For any given time period t, the SAR for the next period (t+1) is calculated based on the current period’s values.

The core formula is:

This formula looks simple, but its power lies in how the components—the Extreme Point (EP) and the Acceleration Factor (α)—are updated dynamically.

Key Components Defined

  • SAR (Stop and Reverse): The value of the indicator for a given period. is today’s SAR value, and is tomorrow’s.
  • EP (Extreme Point): The highest high of the current uptrend or the lowest low of the current downtrend. This value is updated whenever a new extreme is reached.
  • α (Acceleration Factor): A variable that controls the sensitivity of the SAR. It starts at a default value (typically 0.02) and increases by a set increment (also 0.02) each time a new EP is made, up to a maximum value (typically 0.20).

The Calculation Logic Step-by-Step

Let’s break down the process of calculating the Parabolic SAR series for a given price history (Open, High, Low, Close).

1. Initialization and Trend Direction

To start, you need to determine the initial trend. A simple method is to compare the first two periods. If the close of the second candle is higher than the first, an uptrend is assumed.

  • For an initial uptrend: The first SAR value is set to the Low of the first period. The first EP is the High of the first period.
  • For an initial downtrend: The first SAR value is set to the High of the first period. The first EP is the Low of the first period.

The Acceleration Factor (α) always starts at its initial value, 0.02.

2. Calculating the SAR for the Next Period

Using the main formula, calculate the next period’s SAR. However, there are some specific rules to prevent the SAR from moving into the prior period’s price range:

  • In an uptrend: The calculated SAR for tomorrow can never be above today’s low or yesterday’s low. If it is, set the SAR to the lower of those two lows.
  • In a downtrend: The calculated SAR for tomorrow can never be below today’s high or yesterday’s high. If it is, set the SAR to the higher of those two highs.

3. Updating the Extreme Point (EP) and Acceleration Factor (AF)

The indicator’s sensitivity is controlled by the AF (), which changes only when the trend makes a new price extreme.

  • In an uptrend: If the high of the current period is greater than the current EP, a new extreme has been reached. Update the EP to this new high and increase the AF by the increment (e.g., 0.02), but not beyond the maximum value (0.20).
  • In a downtrend: If the low of the current period is lower than the current EP, a new extreme has been reached. Update the EP to this new low and increase the AF by the increment, again capping it at the maximum.

If no new extreme is made, the EP and AF remain unchanged for the next period’s calculation.

4. The Stop and Reverse (SAR) Event

A reversal is the signal that the current trend has ended.

  • An uptrend reverses when the price falls and touches or crosses below the SAR dot. The low of the period will be less than or equal to the SAR value for that period.
  • A downtrend reverses when the price rises and touches or crosses above the SAR dot. The high of the period will be greater than or equal to the SAR value for that period.

When a reversal occurs:

  1. The trend flips (UP to DOWN, or DOWN to UP).
  2. The SAR value for the reversal period is set to the EP of the previous trend.
  3. The AF is reset to its initial value (0.02).
  4. The EP is reset to the high (for a new uptrend) or low (for a new downtrend) of the current reversal period.
Intuition. Think of the Parabolic SAR as a dynamic, accelerating trailing stop-loss. In a strong trend, it starts further away from the price to give it room to breathe. As the trend matures and makes new highs (or lows), the SAR accelerates and moves closer to the price, tightening the stop to lock in profits. A price break of the SAR signals that the trend’s momentum has likely faded.

Worked Example: Synthetic NIFTY Data

Let’s walk through an example using a hypothetical 15-minute chart for NIFTY futures. We will use the standard parameters: Initial AF = 0.02, Increment = 0.02, Max AF = 0.20. The results are rounded to two decimal places for clarity.

Candle Open High Low Close Trend SAR EP AF Notes
1 50.0 52.0 49.0 51.0 UP 49.00 52.0 0.02 Initial UP trend assumed. SAR is set to Low, EP to High.
2 51.0 54.0 50.0 53.0 UP 49.06 54.0 0.04 SAR = 49 + 0.02(52-49). New High made, so EP is updated and AF increases.
3 53.0 53.5 51.0 52.0 UP 49.26 54.0 0.04 SAR = 49.06 + 0.04(54-49.06). No new High, so EP and AF are unchanged.
4 52.0 52.5 49.0 49.5 DOWN 54.00 49.0 0.02 Next SAR (49.45) > Low (49.0). Reversal! SAR flips to previous EP.
5 49.5 50.0 47.0 48.0 DOWN 53.90 47.0 0.04 SAR = 54 + 0.02(49-54). New Low made, so EP is updated and AF increases.
6 48.0 49.0 46.0 46.5 DOWN 53.62 46.0 0.06 SAR = 53.9 + 0.04(47-53.9). New Low made, so EP is updated and AF increases.
7 46.5 48.0 45.0 47.5 DOWN 53.17 45.0 0.08 SAR = 53.62 + 0.06(46-53.62). New Low made, so EP is updated and AF increases.
8 47.5 48.5 46.0 47.0 DOWN 52.53 45.0 0.08 SAR = 53.17 + 0.08(45-53.17). No new Low. EP and AF unchanged.
9 47.0 49.0 46.0 48.5 DOWN 51.93 45.0 0.08 SAR = 52.53 + 0.08(45-52.53). No new Low. EP and AF unchanged.
10 48.5 50.0 47.5 49.0 UP 45.00 50.0 0.02 Next SAR (51.38) > High (50.0). Reversal! SAR flips to previous EP.
Pitfall. The primary weakness of the Parabolic SAR is its performance in non-trending, sideways, or choppy markets. In such conditions, the indicator is prone to “whipsaws,” generating frequent reversal signals that result in small losses. It is therefore best used in conjunction with a trend-strength indicator like the ADX to confirm that the market is actually trending.

Python Implementation from Scratch

Here is a Python function that implements the Parabolic SAR calculation on a Pandas DataFrame. The DataFrame `df` must contain ‘high’ and ‘low’ columns. The code follows the logic described above.

import pandas as pd
import numpy as np

def calculate_parabolic_sar(df, initial_af=0.02, af_increment=0.02, max_af=0.20):
    # Make a copy to avoid SettingWithCopyWarning
    df = df.copy()

    # Create columns for SAR and its components
    df['SAR'] = 0.0
    df['EP'] = 0.0
    df['AF'] = 0.0

    # Determine the starting trend (True for uptrend, False for downtrend)
    uptrend = df['close'].iloc[1] > df['close'].iloc[0]

    # Initialize first row values
    if uptrend:
        df.loc[df.index[0], 'SAR'] = df['low'].iloc[0]
        df.loc[df.index[0], 'EP'] = df['high'].iloc[0]
    else:
        df.loc[df.index[0], 'SAR'] = df['high'].iloc[0]
        df.loc[df.index[0], 'EP'] = df['low'].iloc[0]
    df.loc[df.index[0], 'AF'] = initial_af

    # Iterate through the DataFrame starting from the second row
    for i in range(1, len(df)):
        # Get previous row's values
        prev_sar = df['SAR'].iloc[i-1]
        prev_ep = df['EP'].iloc[i-1]
        prev_af = df['AF'].iloc[i-1]
        
        # Carry over the trend direction initially
        current_uptrend = uptrend 

        # Calculate the potential next SAR
        sar_next = prev_sar + prev_af * (prev_ep - prev_sar)

        # -- Reversal Check --
        if current_uptrend:
            # If the next SAR is below the current low, a reversal occurs
            if sar_next > df['low'].iloc[i]:
                uptrend = False # Flip to downtrend
                sar_next = prev_ep # New SAR is the previous Extreme Point
                ep_next = df['low'].iloc[i]
                af_next = initial_af
            else: # No reversal
                uptrend = True
                af_next = prev_af
                ep_next = prev_ep
                # Update EP and AF if new high is made
                if df['high'].iloc[i] > prev_ep:
                    ep_next = df['high'].iloc[i]
                    af_next = min(max_af, prev_af + af_increment)
        else: # Current trend is down
            # If the next SAR is above the current high, a reversal occurs
            if sar_next < df['high'].iloc[i]:
                uptrend = True # Flip to uptrend
                sar_next = prev_ep # New SAR is the previous Extreme Point
                ep_next = df['high'].iloc[i]
                af_next = initial_af
            else: # No reversal
                uptrend = False
                af_next = prev_af
                ep_next = prev_ep
                # Update EP and AF if new low is made
                if df['low'].iloc[i] < prev_ep:
                    ep_next = df['low'].iloc[i]
                    af_next = min(max_af, prev_af + af_increment)

        # Set the values for the current row
        df.loc[df.index[i], 'SAR'] = sar_next
        df.loc[df.index[i], 'EP'] = ep_next
        df.loc[df.index[i], 'AF'] = af_next

    return df

# Example usage:
# Assuming df is your DataFrame with OHLC data
# df_with_sar = calculate_parabolic_sar(df)

Standard Parameters

  • Initial Acceleration Factor (initial_af): 0.02. This is the starting sensitivity.
  • Acceleration Factor Increment (af_increment): 0.02. This is how much the sensitivity increases on each new price extreme.
  • Maximum Acceleration Factor (max_af): 0.20. This caps the sensitivity to prevent the SAR from becoming too tight in a prolonged trend.

These standard values, proposed by Wilder, offer a balance between responsiveness and reliability. While they can be adjusted, most charting platforms and traders use them as the default setting. Understanding how these parameters influence the SAR's calculation is key to interpreting its signals correctly.

Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *

×Close