How To Compute Parabolic SAR (Stop and Reverse) using Python?

Member

by delpha , in category: SEO Tools , a month ago

How To Compute Parabolic SAR (Stop and Reverse) using Python?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by emelie , a month ago

@delpha 

To compute the Parabolic SAR (Stop and Reverse) using Python, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
import pandas as pd

def parabolic_sar(data, acceleration=0.02, max_acceleration=0.2):
    high = data['High']
    low = data['Low']
    
    trend = 1
    acceleration_factor = acceleration
    extreme_point = low.iloc[0]
    sar = []

    for i in range(len(data)):
        if trend == 1:
            sar.append(extreme_point + acceleration_factor * (high.iloc[i] - extreme_point))
        else:
            sar.append(extreme_point - acceleration_factor * (extreme_point - low.iloc[i]))

        if trend == 1:
            if low.iloc[i] < extreme_point:
                trend = -1
                sar.append(high.iloc[i])
                sar.append(extreme_point)
                acceleration_factor = acceleration
                extreme_point = high.iloc[i]
        else:
            if high.iloc[i] > extreme_point:
                trend = 1
                sar.append(low.iloc[i])
                sar.append(extreme_point)
                acceleration_factor = acceleration
                extreme_point = low.iloc[i]

        acceleration_factor = min(acceleration_factor + acceleration, max_acceleration)

    return pd.Series(sar, index=data.index)

# Example usage
data = pd.DataFrame({
    'High': [10, 15, 20, 25, 30],
    'Low': [5, 10, 15, 20, 25]
})

parabolic_sar_values = parabolic_sar(data)
print(parabolic_sar_values)


In this code snippet, we define a function parabolic_sar that computes the Parabolic SAR values based on the high and low prices of the data. The function takes two parameters acceleration and max_acceleration, which control the rate at which the acceleration factor increases.


You can provide your own data as a pandas DataFrame with columns 'High' and 'Low' and call the parabolic_sar function to get the Parabolic SAR values.