Я пытаюсь преобразовать этот код в python.
bandedge= 20
whitenoise= (Close - Close[2])/2
if barindex>bandedge then
// super smoother filter
a1= Exp(-1.414 * 3.14159 / bandedge)
b1= 2*a1 * Cos(1.414*180 /bandedge)
c2= b1
c3= -a1 * a1
c1= 1 - c2 - c3
filt= c1 * (whitenoise + whitenoise[1])/2 + c2*filt[1] + c3*filt[1]
filt1 = filt
if ABS(filt1)>pk[1] then
pk = ABS(filt1)
else
pk = 0.991 * pk[1]
endif
if pk=0 then
denom = -1
else
denom = pk
endif
if denom = -1 then
result = result[1]
else
result = filt1/pk
endif
endif
RETURN result COLOURED(66,66,255) as "Universal Oscillator", 0 as "0"
Я взял ссылки по этим ссылкам:
http://traders.com/Documentation/FEEDbk_docs/2015/01/TradersTips.html
https://www.tradingview.com/script/ieFYbVdC-Ehlers-Universal-Oscillator-LazyBear/
https://www.prorealcode.com/prorealtime-indicators/universal-oscillator-john-ehlers/
Я использую pandas, и мой код выполняется без ошибок, но я не получаю те же результаты, что и с tradingview.
import pandas as pd
def euo(df):
df['close'] = [ float(x) for x in df['close']]
df['whitenoise'] = [(row['close'] - df['close'].iloc[index-2])/2 for index,row in df.iterrows()]
bandedge = 20
a1= 2.718*(-1.414 * 3.14159 / bandedge)
c2= 2.0*a1 * cos(1.414*180 / bandedge)
c3= -a1 * a1
c1= 1 - c2 - c3
df['filt']= [c1*((row['whitenoise'] + df['whitenoise'].iloc[index-1])/2) for index,row in df.iterrows()]
df['filter']= [row['filt']+c2*df['filt'].iloc[index-1]+c3*df['filt'].iloc[index-2] for index,row in df.iterrows()]
df['pk0'] = [abs(row['filter']) for index,row in df.iterrows()]
df['pk'] = [row['pk0'] if row['pk0'] > df['pk0'].iloc[index-1] else 0.991*df['pk0'].iloc[index-1] for index,row in df.iterrows()]
df['denom'] = [-1 if row['pk'] == 0 else row['pk'] for index,row in df.iterrows()]
df['res'] = [row['filter']/row['pk'] for index,row in df.iterrows()]
df['uniosc'] = [df['res'].iloc[index-1] if row['denom'] == -1 else row['res'] for index,row in df.iterrows()]
Может кто-нибудь помочь мне?