我正在尝试将此代码转换为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/
我正在使用熊猫,并且我的代码运行没有错误,但是我没有从交易视图中获得相同的结果。
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()]
有人可以帮我吗?