我正在尝试在交易视图中使用 Ehlers 屋顶过滤器和 Ehlers 超级平滑器以及 pine-script 来编写随机动量指数。我的具有埃勒斯屋顶和平滑器的随机动量指数代码如下 -
//@version=5
indicator(title="Stochastic Momentum Index", shorttitle="SMI", overlay=false)
//Ehlers Roofing Filter
erf(x, t_hp, t_lp)=>
omega1 = 4*math.asin(1)/t_hp
omega2 = 4*math.asin(1)/t_lp
alpha = (math.cos((math.sqrt(2)/2)*omega1) + math.sin((math.sqrt(2)/2)*omega1) - 1)/math.cos((math.sqrt(2)/2)*omega1)
hp = 0.0
hp := math.pow(1 - alpha/2, 2)*(x - 2*x[1] + x[2]) + 2*(1 - alpha)*nz(hp[1]) - math.pow(1 - alpha, 2)*nz(hp[2])
a1 = math.exp(-math.sqrt(2)*2*math.asin(1)/t_lp)
b1 = 2*a1*math.cos((math.sqrt(2)/2)*omega2)
c2 = b1
c3 = -math.pow(a1, 2)
c1 = 1 - c2 - c3
erf = 0.0
erf := c1*hp + c2*nz(erf[1]) + c3*nz(erf[2])
erf
src = input(defval=close, title="Source")
lp_per = input.int(defval=10, minval=2, title="Low Pass Cutoff Period")
hp_per = input.int(defval=30, minval=2, title="High Pass Cutoff Period")
roof = erf(src, hp_per, lp_per)
applyerf = input.bool(true, title="Apply Ehlers Roofing?")
erflen = input.int(10, title="Ehlers Roofing Length")
//Ehler's Smoother Filter
PI=3.14159265359
EhlersSuperSmootherFilter(price, lower) =>
var filt=0.0
a1 = math.exp(-PI * math.sqrt(2) / lower)
coeff2 = 2 * a1 * math.cos(math.sqrt(2) * PI / lower)
coeff3 = - math.pow(a1,2)
coeff1 = 1 - coeff2 - coeff3
filt := coeff1 * (price + nz(price[1])) / 2 + coeff2 * nz(filt[1]) + coeff3 * nz(filt[2])
applyes = input.bool(true, title="Apply Ehlers Smoother?")
eslen = input.int(10, title="Ehlers Smoother Length")
//Input
q = input.int(title="Stochastic Lookback", defval=13, minval=1)
r = input.int(title="1st Smoothing Length", defval=25, minval=1)
s = input.int(title="2nd Smoothing Length", defval=2, minval=1)
signalLength = input.int(title="Signal Length", defval=12, minval=1)
//src = input.source(title="Source", defval=close)
showsignals = input.bool(defval=true, title="Buy/Sell Signals")
//Calculation
hh = ta.highest(q)
ll = ta.lowest(q)
numerator = ta.ema(ta.ema(src - 0.5 * (hh + ll), r), s)
denominator = 0.5 * ta.ema(ta.ema(hh - ll, r), s)
sm = 100 * numerator / denominator
smrf = applyerf ? roof(sm, erflen) : sm
smi = applyes ? EhlersSuperSmootherFilter(smrf, eslen) : smrf
signal = ta.ema(smi, signalLength)
hist = smi - signal
//Hist-Color
col_grow_above = input(#26A69A, "Above Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
//Plotting
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(smi, title="SMI", color=color.green, transp=0)
plot(signal, title="Signal", color=color.red, transp=0)
maxLevel = hline(+75, title="Max Level", color=color.black)
obLevel = hline(+40, title="Overbought Level", color=#00796b)
median = hline(0, title="Zero Level", color=#989898)
osLevel = hline(-40, title="Oversold Level", color=#f57f17)
minLevel = hline(-75, title="Min Level", color=color.black)
fill(obLevel, osLevel, title="SMI Background Fill", color=color.purple, transp=95)
我遇到以下错误 -
line 56: Could not find function or function reference 'roof'
请帮我解决这个问题。谢谢你的时间。问候。
我在发布问题后立即找到了答案。解决办法如下-
第 56 行:smrf = applyerf ? erf(sm, hp_per, lp_per) : sm
非常感谢您的建议。真的很感激。