figured it out
df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'EP']-df.loc[a-1, 'PSAR']))
Should be df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'PSAR']-df.loc[a-1, 'EP']))
last two variables transposed!
now I can clean up the function and make it better.
Answer from Greg D on Stack Overflowfigured it out
df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'EP']-df.loc[a-1, 'PSAR']))
Should be df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'PSAR']-df.loc[a-1, 'EP']))
last two variables transposed!
now I can clean up the function and make it better.
I updated the script, fixed the bug with the increasing PSAR value and also overlapping PSAR with high/low price.
Now this function gives the exact same PSAR like TradingView.

def PSAR(df, af=0.02, max=0.2):
df.loc[0, 'AF'] = 0.02
df.loc[0, 'PSAR'] = df.loc[0, 'low']
df.loc[0, 'EP'] = df.loc[0, 'high']
df.loc[0, 'PSARdir'] = "bull"
for a in range(1, len(df)):
if df.loc[a-1, 'PSARdir'] == 'bull':
df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'EP']-df.loc[a-1, 'PSAR']))
df.loc[a, 'PSARdir'] = "bull"
if df.loc[a, 'low'] < df.loc[a-1, 'PSAR'] or df.loc[a, 'low'] < df.loc[a, 'PSAR']:
df.loc[a, 'PSARdir'] = "bear"
df.loc[a, 'PSAR'] = df.loc[a-1, 'EP']
df.loc[a, 'EP'] = df.loc[a-1, 'low']
df.loc[a, 'AF'] = af
else:
if df.loc[a, 'high'] > df.loc[a-1, 'EP']:
df.loc[a, 'EP'] = df.loc[a, 'high']
if df.loc[a-1, 'AF'] <= 0.18:
df.loc[a, 'AF'] =df.loc[a-1, 'AF'] + af
else:
df.loc[a, 'AF'] = df.loc[a-1, 'AF']
elif df.loc[a, 'high'] <= df.loc[a-1, 'EP']:
df.loc[a, 'AF'] = df.loc[a-1, 'AF']
df.loc[a, 'EP'] = df.loc[a-1, 'EP']
elif df.loc[a-1, 'PSARdir'] == 'bear':
df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] - (df.loc[a-1, 'AF']*(df.loc[a-1, 'PSAR']-df.loc[a-1, 'EP']))
df.loc[a, 'PSARdir'] = "bear"
if df.loc[a, 'high'] > df.loc[a-1, 'PSAR'] or df.loc[a, 'high'] > df.loc[a, 'PSAR']:
df.loc[a, 'PSARdir'] = "bull"
df.loc[a, 'PSAR'] = df.loc[a-1, 'EP']
df.loc[a, 'EP'] = df.loc[a-1, 'high']
df.loc[a, 'AF'] = af
else:
if df.loc[a, 'low'] < df.loc[a-1, 'EP']:
df.loc[a, 'EP'] = df.loc[a, 'low']
if df.loc[a-1, 'AF'] < max:
df.loc[a, 'AF'] = df.loc[a-1, 'AF'] + af
else:
df.loc[a, 'AF'] = df.loc[a-1, 'AF']
elif df.loc[a, 'low'] >= df.loc[a-1, 'EP']:
df.loc[a, 'AF'] = df.loc[a-1, 'AF']
df.loc[a, 'EP'] = df.loc[a-1, 'EP']
return df
How to use Parabolic SAR indicator? - QuantConnect.com
Help with a parabolic SAR
Maybe post what your algorithm to calculate the parabolic SAR is?
Have you compared your derivation to the reference? Is it possible that IG has tweaked or made theirs custom?
More on reddit.compandas - Parabolic SAR calculated in Python seems to be reversed - Stack Overflow
Stop Trading with backtrader in Python
Yeah that title is terrible. Is “Stop Trading” even a term anyone uses? I’ve always hear Stop Order
More on reddit.comHi all!
This is my first post on r/python but I’ve been using the language for a while to make small projects, mainly using API for a number of websites.
My current project is to create an automated forex trading software using data from the IG.com API and the parabolic SAR indicator. With IG you cannot download indicator values live as this isn’t supported so I am having to calculate it myself. However I really cannot get my calculation to remain in check with the value displayed in the IG.com trading window and sometimes the SAR value I calculate rises when it should be falling?!
Does anyone have any experience dealing with a parabolic SAR calculation using values attained every minute?
Thank you all!