Pine script 顺序计数和标签触发器

Pine script 顺序计数和标签触发器,pine-script,Pine Script,当rsi越过较低的波段时,我试图计算并标记长触发器。每次交叉触发发生时,os1、os2、os3都会出现一个新标签。最大值为3,然后返回到1。我分析并使用了TD顺序脚本中的一些代码,但坦率地说,我不知道它是如何工作的。我使用默认的RSI脚本进行学习。它只会一直给我os 1。有没有线索表明它出了问题?非常感谢您的任何建议。:)谢谢 //@version=4 study(title="Relative Strength Index", shorttitle="RSI&qu

当rsi越过较低的波段时,我试图计算并标记长触发器。每次交叉触发发生时,os1、os2、os3都会出现一个新标签。最大值为3,然后返回到1。我分析并使用了TD顺序脚本中的一些代码,但坦率地说,我不知道它是如何工作的。我使用默认的RSI脚本进行学习。它只会一直给我os 1。有没有线索表明它出了问题?非常感谢您的任何建议。:)谢谢

//@version=4
study(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, resolution="")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#8E1599)

upper = input(70)
lower = input(30)

band1 = hline(upper, "Upper Band", color=#C0C0C0)
band0 = hline(lower, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")

os = crossover(rsi, lower)
position = rsi
plot (os?position:na,color=color.red, style=plot.style_circles,linewidth=3)

oss = 0
oss := os==1 ? nz(oss[1])==0 ? 1: oss[1]==1 ?2: oss[1]==2 ? 3: 0: 0

plotshape(oss==1?true:na,style=shape.arrowup,text="os1",color=color.blue,location=location.absolute)
plotshape(oss==2?true:na,style=shape.arrowup,text="os2",color=color.blue,location=location.absolute)
plotshape(oss==3?true:na,style=shape.arrowup,text="os3",color=color.blue,location=location.absolute)

您需要使用'var'关键字来防止变量在每次更新时重置为零。嵌套的?:表达式也有问题。下面是一个修改过的脚本,它保持较低频带交叉的计数,并在3之后重置

//@version=4
study(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, resolution="")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#8E1599)

upper = input(70)
lower = input(30)

band1 = hline(upper, "Upper Band", color=#C0C0C0)
band0 = hline(lower, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")

os = crossover(rsi, lower)
position = rsi
plot (os?position:na,color=color.red, style=plot.style_circles,linewidth=3)

var oss = 0
if(os)
    oss := oss[1]==3 ? 1 : oss[1]+1

plotshape(oss==1 and os?true:na,style=shape.arrowup,text="os1",color=color.blue,location=location.absolute)
plotshape(oss==2 and os?true:na,style=shape.arrowup,text="os2",color=color.blue,location=location.absolute)
plotshape(oss==3 and os?true:na,style=shape.arrowup,text="os3",color=color.blue,location=location.absolute)

谢谢!因为我的问题尝试了其他脚本变体,并且确实发现每次更新时计数都重置为0。我没有尝试“var”方法。我将更深入地了解它的逻辑。谢谢不客气。如果答案回答了你的问题,请投上一票。谢谢!:)