How to highlight area of AFTER 3 bars have passed?

Viewed 56

enter image description here

Here is an image to get a clearer picture. I'm trying to highlight the area of RSI that is over 70, BUT highlighting begins AFTER 3 bars have passed. I get how to do highlight whenever RSI is over 70, but can't figure out how to do it AFTER 3 bars have passed when it crosses over 70. Thank you so much

EDIT: Here is my code so far

//@version=5
indicator(title="Relative Strength Index", shorttitle="RSI")

rsi = ta.rsi(close,14)
l_60 = 60
l_40 = 40
ob = rsi > 60 ? rsi : l_60
os = rsi < 40 ? rsi : l_40

p1 = plot(series=l_60, color=color.new(#FF5252,20), linewidth=1)
p2 = plot(series=ob, color=color.new(#FF5252,20), linewidth=1)
p3 = plot(series=l_40, color=color.new(#4CAF50,20), linewidth=1)
p4 = plot(series=os, color=color.new(#4CAF50,20), linewidth=1)
fill(p1, p2, color=color.new(#4CAF50,10))
fill(p3, p4, color=color.new(#FF5252,10))
plot(rsi, "RSI", color=#7E57C2)

Edit#2: if this isn't possible, could making a rectangular box work? Im not sure how to make a rectangular box using pinescript but how do you make a box with the vertical line starting THREE bars since crossover? thnx

1 Answers

I would personally use a simple counter to solve this. Here is an example:

//@version=5
indicator(title="Relative Strength Index", shorttitle="RSI")

l_70 = 70
rsi = ta.rsi(close, 20)

plot_70 = plot(l_70, display=display.none)

// counter
var counter = 0
if rsi > l_70
    counter += 1
else
    counter := 0 

rsiCondition = rsi > l_70 and counter > 2

plotCondition = rsiCondition ? rsi : na
plot_rsi =  plot(plotCondition, style=plot.style_linebr)

fill(plot_rsi, plot_70)
plot(rsi)
Related