0
votes

I am trying to count and label the long triggers when the rsi crosses over the lower band. Every time the crossover trigger happens, a new label appears with os1, os2, os3. With a maximum of 3 and then back to 1. I analysed and used some code from a TD sequential script, but frankly I have no clue how it works. I use the default RSI script for my study. It only keeps giving me os 1. Any clue were it goes wrong? Any suggestions are very much appriciated. :) Thank you!!

//@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)
1

1 Answers

0
votes

You need to use the 'var' keyword to prevent variable from resetting to zero every update. There is also something wrong with nested ?: expressions. Here's modified script that keeps count of lower band crossing and resets after 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)

enter image description here