How Parabolic SAR is Calculated

In this case, you’ll be working with a DataFrame df with columns ‘timestamp’, ‘open’, ‘high’, ‘low’, ‘close’, and ‘volume’. For the Parabolic SAR calculation, you would primarily need the ‘high’ and ‘low’ columns. Here’s how you could code the Parabolic SAR indicator based on your DataFrame:

				
					import pandas as pd
import numpy as np

def calculate_parabolic_sar(df):
    # Assumed/Standard Values
    initial_af = 0.02
    max_af = 0.20
    af_increment = 0.02

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

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

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

    for i in range(1, len(df)):
        prev_sar = df['SAR'].iloc[i - 1]
        prev_af = df['AF'].iloc[i - 1]
        prev_ep = df['EP'].iloc[i - 1]

        # Calculate SAR for today
        df['SAR'].iloc[i] = prev_sar + prev_af * (prev_ep - prev_sar)

        # Check for trend reversal
        if (uptrend and df['SAR'].iloc[i] > df['low'].iloc[i]) or (not uptrend and df['SAR'].iloc[i] < df['high'].iloc[i]):
            uptrend = not uptrend  # Reverse the trend
            df['SAR'].iloc[i] = prev_ep  # Set SAR to the EP of the previous trend
            df['AF'].iloc[i] = initial_af  # Reset AF
            df['EP'].iloc[i] = df['high'].iloc[i] if uptrend else df['low'].iloc[i]  # Set EP to today's high/low
        else:
            # Update AF and EP if a new high/low is made
            if (uptrend and df['high'].iloc[i] > prev_ep) or (not uptrend and df['low'].iloc[i] < prev_ep):
                df['AF'].iloc[i] = min(max_af, prev_af + af_increment)  # Increment AF, cap at max_af
                df['EP'].iloc[i] = df['high'].iloc[i] if uptrend else df['low'].iloc[i]  # Update EP to today's high/low
            else:
                df['AF'].iloc[i] = prev_af  # Carry over AF
                df['EP'].iloc[i] = prev_ep  # Carry over EP

    return df

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

				
			

Assumed/Standard Values:

  • Initial Acceleration Factor (initial_af): 0.02
  • Maximum Acceleration Factor (max_af): 0.20
  • Acceleration Factor Increment (af_increment): 0.02

These values are standard in the calculation of the Parabolic SAR and are used to control the sensitivity and speed at which the SAR approaches the price.

Real World Example

				
					| Candle | Open | High | Low | Close |
|--------|------|------|-----|-------|
| 1      | 50.0 | 52.0 | 49.0 | 51.0  |
| 2      | 51.0 | 54.0 | 50.0 | 53.0  |
| 3      | 53.0 | 53.5 | 51.0 | 52.0  |
| 4      | 52.0 | 52.5 | 49.0 | 49.5  |
| 5      | 49.5 | 50.0 | 47.0 | 48.0  |
| 6      | 48.0 | 49.0 | 46.0 | 46.5  |
| 7      | 46.5 | 48.0 | 45.0 | 47.5  |
| 8      | 47.5 | 48.5 | 46.0 | 47.0  |
| 9      | 47.0 | 49.0 | 46.0 | 48.5  |
| 10     | 48.5 | 50.0 | 47.5 | 49.0  |

				
			

I understand that working through a real example can be very helpful in understanding how the Parabolic SAR is calculated. Let’s consider a hypothetical set of 10 candles with the following OHLC (Open, High, Low, Close) values:

Now let’s calculate the Parabolic SAR step-by-step for these candles:

Initialization:

  • Initial AF = 0.02
  • Maximum AF = 0.20
  • AF Increment = 0.02

First Candle:

  • As it’s the first candle, we’ll assume an uptrend and set the SAR to the Low of the first candle (SAR = 49.0).
  • EP = High of first candle = 52.0
  • AF remains at 0.02

Second Candle:

  • SAR(2) = SAR(1) + AF * (EP – SAR(1)) = 49.0 + 0.02 * (52.0 – 49.0) = 49.06
  • As the high of the second candle is higher than the current EP, EP is updated to 54.0.
  • AF is increased by 0.02 to 0.04 as EP was updated.

Third Candle:

  • SAR(3) = SAR(2) + AF * (EP – SAR(2)) = 49.06 + 0.04 * (54.0 – 49.06) = 49.26
  • High of third candle is lower than EP, so no change in EP or AF.

Fourth Candle:

  • SAR(4) = SAR(3) + AF * (EP – SAR(3)) = 49.26 + 0.04 * (54.0 – 49.26) = 49.45
  • Trend reversal as SAR(4) is higher than the Low of the fourth candle (49.0).
  • SAR(4) is set to the previous EP: SAR(4) = 54.0
  • Reset AF = 0.02 and EP = Low of fourth candle = 49.0

Fifth Candle:

  • SAR(5) = SAR(4) + AF * (EP – SAR(4)) = 54.0 + 0.02 * (49.0 – 54.0) = 53.9
  • EP and AF remain unchanged as no new low is made.

Sixth Candle:

  • SAR(6) = SAR(5) + AF * (EP – SAR(5)) = 53.9 + 0.02 * (49.0 – 53.9) = 53.8
  • EP is updated to the Low of the sixth candle: EP = 46.0
  • AF is increased to 0.04 as a new low is made.

Seventh Candle:

  • SAR(7) = SAR(6) + AF * (EP – SAR(6)) = 53.8 + 0.04 * (46.0 – 53.8) = 53.48
  • EP and AF remain unchanged as no new low is made.

Eighth Candle:

  • SAR(8) = SAR(7) + AF * (EP – SAR(7)) = 53.48 + 0.04 * (46.0 – 53.48) = 53.18
  • EP and AF remain unchanged as no new low is made.

Ninth Candle:

  • SAR(9) = SAR(8) + AF * (EP – SAR(8)) = 53.18 + 0.04 * (46.0 – 53.18) = 52.89
  • Trend reversal as SAR(9) is higher than the Low of the ninth candle (46.0).
  • SAR(9) is set to the previous EP: SAR(9) = 46.0
  • Reset AF = 0.02 and EP = High of ninth candle = 49.0

Tenth Candle:

  • SAR(10) = SAR(9) + AF * (EP – SAR(9)) = 46.0 + 0.02 * (49.0 – 46.0) = 46.06
  • EP is updated to the High of the tenth candle: EP = 50.0
  • AF is increased to 0.04 as a new high is made.

In this example, the Maximum AF of 0.20 was not reached. However, if the trend had continued and new highs (in an uptrend) or new lows (in a downtrend) were made in each subsequent candle, the AF would have continued to increase by 0.02 with each new extreme point, up to a maximum of 0.20.

Let’s extend the table with more candles and continue with the calculations until the Maximum AF of 0.20 is reached.

Continuing from the 10th candle with the updated EP of 50.0 and AF of 0.04, let’s add more candles:

				
					| Candle | Open  | High  | Low   | Close | 
|--------|-------|-------|-------|-------| 
| 11     | 49.0  | 51.0  | 48.5  | 50.5  | 
| 12     | 50.5  | 52.0  | 50.0  | 51.5  | 
| 13     | 51.5  | 53.0  | 51.0  | 52.5  | 
| 14     | 52.5  | 54.0  | 52.0  | 53.5  | 
| 15     | 53.5  | 55.0  | 53.0  | 54.5  | 
| 16     | 54.5  | 56.0  | 54.0  | 55.5  | 
| 17     | 55.5  | 57.0  | 55.0  | 56.5  | 
| 18     | 56.5  | 58.0  | 56.0  | 57.5  | 
| 19     | 57.5  | 59.0  | 57.0  | 58.5  | 
| 20     | 58.5  | 60.0  | 58.0  | 59.5  | 

				
			

Eleventh Candle:

  • SAR(11) = 46.06 + 0.04 * (50.0 – 46.06) = 46.22
  • EP = 51.0 (new high)
  • AF = 0.06 (AF increased)

Twelfth Candle:

  • SAR(12) = 46.22 + 0.06 * (51.0 – 46.22) = 46.51
  • EP = 52.0 (new high)
  • AF = 0.08 (AF increased)

… Continue this process …

Eighteenth Candle:

  • SAR(18) = 48.51 + 0.18 * (58.0 – 48.51) = 49.51
  • EP = 58.0 (new high)
  • AF = 0.20 (AF increased to max AF)

Nineteenth Candle:

  • SAR(19) = 49.51 + 0.20 * (58.0 – 49.51) = 50.51
  • EP = 59.0 (new high)
  • AF = 0.20 (AF remains at max AF)

Twentieth Candle:

  • SAR(20) = 50.51 + 0.20 * (59.0 – 50.51) = 51.51
  • EP = 60.0 (new high)
  • AF = 0.20 (AF remains at max AF)

Now, the Maximum AF of 0.20 has been reached on the eighteenth candle, and it remains at 0.20 for subsequent candles as the uptrend continues and new highs are made. The SAR continues to accelerate towards the price, but at a constant rate from now on as the AF is capped at 0.20.

Recap

Here’s a step-by-step breakdown of how the Parabolic SAR is constructed:

1. Initialization:

  • Start: Select a start point for the Parabolic SAR. This is usually the high price of a bullish trend or the low price of a bearish trend.
  • Initial EP (Extreme Point): This is the highest high of the current uptrend or the lowest low of the current downtrend.
  • Initial AF (Acceleration Factor): The acceleration factor starts at 0.02 and increases by 0.02 each time a new EP is set, with a maximum of 0.20.
    • In an Uptrend:The EP is the highest high reached during the current uptrend. If today’s high is higher than the previous EP, the AF increases by the predefined increment.
    • In a Downtrend:The EP is the lowest low reached during the current downtrend. If today’s low is lower than the previous EP, the AF increases by the predefined increment.
 Note that Extreme Point and Maximum Acceleration Factor is same thing. c

2. Calculation:

  • SAR for Tomorrow: The formula for tomorrow’s SAR is:
				
					SAR(tomorrow) = SAR(today) + AF(today) * (EP(today) - SAR(today))

				
			
  • Updating EP and AF: If a new high (in an uptrend) or a new low (in a downtrend) is made, update the EP to this new high/low and increase the AF by 0.02, with a maximum value of 0.20.
  • Trend Reversal: If the price crosses the SAR, the trend is considered to be reversed. The SAR then flips to the other side of the price, and is set to the last EP of the previous trend. The AF is reset to 0.02 and a new trend begins.
3. Default Values:
  • Starting AF: The default starting value for the AF is 0.02. This value determines how sensitive the SAR is to price changes. A smaller value makes the SAR less sensitive, and a larger value makes it more sensitive.
  • Incremental AF: The default incremental value for the AF is also 0.02. This value determines how quickly the SAR will converge towards the price as the trend continues.
  • Maximum AF: The default maximum value for the AF is 0.20. This value acts as a cap to ensure that the SAR does not become overly sensitive to price changes as the trend continues.
4. Additional Notes:
  • The Parabolic SAR is graphically represented by dots placed above or below the price bars, depending on the trend.
  • In a bullish trend, the dots are placed below the price bars, and in a bearish trend, they are placed above the price bars.
  • The default values are chosen to balance between sensitivity and signal reliability, but traders may adjust these to fit their specific trading style and the characteristics of the asset they are trading.
This is a detailed breakdown of how the Parabolic SAR is constructed and the rationale behind its default values.

So far, What We’ve learnt regarding the calculation of the Parabolic SAR indicator:

  • The Parabolic SAR calculation primarily requires the ‘high’ and ‘low’ columns from the data.
  • Assumed/Standard values are set: initial acceleration factor (AF) is 0.02, maximum AF is 0.20, and AF increment is 0.02.
  • Creation of additional columns for SAR, AF, and EP in the DataFrame.
  • Determination of the starting trend based on the closing prices.
  • Iterative calculation of the SAR value for each day, checking for trend reversals and updating AF and EP values accordingly.
Post a comment

Leave a Comment

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

×Close