Reputation: 3
I am trying to index a variable so it only triggers up to a max of 5 times after the initial condition is met. I am having issues with the if statement. Here is a tradingview screenshot link of the objective https://www.tradingview.com/x/JimOkiQO/
redcon = x > high and open[1] < out1[1]
var counter = 0
red = redcon
if not (high > out2 and low < out2) and not (high > out3 and low < out3)
redcon
red := redcon
for i = -5 to 0 by 1
counter := counter[abs(i-1)]
if counter == 0
red != redcon
break
barcolor(red ? color.yellow : na)
Upvotes: 0
Views: 961
Reputation: 3082
Using a loop is a bad practice Set a var that you update once your conditions are validated
Here is an example
// © LucemAnb
//@version=4
study("PullBack Limit", overlay=true)
max_pullbacks = 5
pull = ema(close, 21)
trend = ema(close, 100)
plot(pull, "Pull", color.blue, 2)
plot(trend, "Trend", pull>trend ? color.teal : color.maroon, 2)
var pullbacks = 0
long_pullback = crossunder(low, pull) and pull > trend
short_pullback = crossover(high, pull) and pull < trend
if long_pullback or short_pullback
pullbacks := pullbacks + 1
if cross(pull, trend)
pullbacks := 0
bgcolor( pullbacks < max_pullbacks ? ( long_pullback ? color.purple : short_pullback ? color.yellow : na) : na )
The pullbacks will stop being shown once the maximum specified limit is reached
Upvotes: 2