I'm trying to add a simple background color when on a stochastic, k crosses over d any time but when k is above 80, and equally k crosses under d any time but when k is below 20.
Yet constantly I'm getting crossover and crossunder signals in these condition. In this case in the picture there is not ever a crossover at all.
Can anyone help me explaining why this happens?
Thanks
//@version=4
study(title="Stochastic RSI", shorttitle="Stoch RSI", format=format.price, precision=2, resolution="")
smoothK = input(3, "K", minval=1)
smoothD = input(3, "D", minval=1)
lengthRSI = input(14, "RSI Length", minval=1)
lengthStoch = input(14, "Stochastic Length", minval=1)
cross = input(false, "Highlight Only Crosses in Ob & Os Areas")
src = input(close, title="RSI Source")
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
plot(k, "K", color=#0094FF)
plot(d, "D", color=#FF6A00)
h0 = hline(80, "Upper Band", color=#606060)
h1 = hline(20, "Lower Band", color=#606060)
fill(h0, h1, color=#9915FF, transp=80, title="Background")
col_ = (k > 80 and crossover(k, d)) or (k < 20 and crossunder(k, d))
? na
: crossunder(k, d)
? #d7000080
: crossover(k, d)
? #00d70080
: na
bgcolor(col_)
//@version=4
study(title="Help (Stochastic RSI)", shorttitle="Stoch RSI", format=format.price, precision=2, resolution="")
smoothK = input(3, "K", minval=1)
smoothD = input(3, "D", minval=1)
lengthRSI = input(14, "RSI Length", minval=1)
lengthStoch = input(14, "Stochastic Length", minval=1)
cross = input(false, "Highlight Only Crosses in Ob & Os Areas")
src = input(close, title="RSI Source")
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
plot(k, "K", color=#0094FF)
plot(d, "D", color=#FF6A00)
h0 = hline(80, "Upper Band", color=#606060)
h1 = hline(20, "Lower Band", color=#606060)
fill(h0, h1, color=#9915FF, transp=80, title="Background")
//col_ = (k > 20 and crossover(d, k)) ? #d7000080 : ((k < 80 and crossunder(d, k)) ? #00d70080 : na)
col_ = (k > 20 and k[1]<d[1] and k[0]>d[0]) ? #d7000080 : ((k < 80 and k[1]>d[1] and k[0]<d[0]) ? #00d70080 : na)
bgcolor(col_)
thanks for the reply but now it's giving signals only when overbought and crossing down or oversold and crossing up. What I am looking is getting signals crossing down from levels 20 to 100 and crossing up from 0 to 80. Also I'm still struggling to understand why with my code it signals a crossunder when there is none where there is a the purple vertical bar. Thanks
Fixed the script.
Thanks, it's now working almost perfectly but there are still time when there is a crossover and it doesn't highlight it. eg. I have a candle around 60 level which is a crossunder and it's signaled but the next candle, always around the same area, is a crossover and it doesn't signal it. Is it possible that the crossover/crossunder functions are not working properly?
Write the ticker, timeframe, and time when this situation occurred.
Strange, but it really is. Fixed the script.