Open illi4 opened 2 months ago
And another attempt that gives PSAR result rather than Lucid SAR even though the rule to never move the SAR into the previous day’s range or today’s range are in:
def get_psar(barsdata, iaf=0.02, maxaf=0.2):
length = len(barsdata)
high = list(barsdata['high'])
low = list(barsdata['low'])
close = list(barsdata['close'])
psar = close[0:len(close)]
psarbull = [None] * length
psarbear = [None] * length
bull = True
af = iaf
ep = low[0]
hp = high[0]
lp = low[0]
for i in range(2,length):
if bull:
psar[i] = psar[i - 1] + af * (hp - psar[i - 1])
else:
psar[i] = psar[i - 1] + af * (lp - psar[i - 1])
reverse = False
if bull:
if low[i] < psar[i]:
bull = False
reverse = True
psar[i] = hp
lp = low[i]
af = iaf
else:
if high[i] > psar[i]:
bull = True
reverse = True
psar[i] = lp
hp = high[i]
af = iaf
if not reverse:
if bull:
if high[i] > hp:
hp = high[i]
af = min(af + iaf, maxaf)
if low[i - 1] < psar[i]:
psar[i] = low[i - 1]
if low[i - 2] < psar[i]:
psar[i] = low[i - 2]
else:
if low[i] < lp:
lp = low[i]
af = min(af + iaf, maxaf)
if high[i - 1] > psar[i]:
psar[i] = high[i - 1]
if high[i - 2] > psar[i]:
psar[i] = high[i - 2]
if bull:
psarbull[i] = psar[i]
barsdata.loc[i, 'PSAR_direction'] = 1
barsdata.loc[i, 'PSAR_val'] = psar[i]
else:
psarbear[i] = psar[i]
barsdata.loc[i, 'PSAR_direction'] = -1
barsdata.loc[i, 'PSAR_val'] = psar[i]
return barsdata
I am trying to refactor the pinescript code to have an ability to calculate lucid SAR in Python. I am not having success and need some guidance / advice. Could someone please advice what I am doing wrong here?