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)
initial_af
): 0.02max_af
): 0.20af_increment
): 0.02These 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.
| 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:
First Candle:
Second Candle:
Third Candle:
Fourth Candle:
Fifth Candle:
Sixth Candle:
Seventh Candle:
Eighth Candle:
Ninth Candle:
Tenth Candle:
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:
Twelfth Candle:
… Continue this process …
Eighteenth Candle:
Nineteenth Candle:
Twentieth Candle:
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.
Here’s a step-by-step breakdown of how the Parabolic SAR is constructed:
1. Initialization:
2. Calculation:
SAR(tomorrow) = SAR(today) + AF(today) * (EP(today) - SAR(today))
So far, What We’ve learnt regarding the calculation of the Parabolic SAR indicator: