I don't want to enter the same situation like image.

enter image description here

CandleSize = abs(high-low)

Length = CandleSize < 30

I want to make code to satisfy the condition that applicable only green candle before entry

It would be nice if code could satisfy the condition either the candle before or the one before last

1

There are 1 answers

0
vitruvius On

You can use the ta.valuewhen() for this purpose.

Returns the value of the source series on the bar where the condition was true on the nth most recent occurrence.

//@version=5
indicator("My script", overlay=true)

is_green = close >= open
cond = (math.abs(high-low)) < 30

last_green = ta.valuewhen(is_green, cond, 0)  // If the condition was true on the last green bar
last_green_1 = ta.valuewhen(is_green, cond, 1)  // If the condition was true on the second green from the last bar

should_enter = last_green and last_green_1
plotshape(should_enter, size=size.small)