Pine Script : Entering after x consecutive Green Bars / Candles

Viewed 169

I am trying to enter a strategy after "X" consecutive bars.

greenCandle = barstate.isconfirmed and (close > open)
G = input(11, minval=1)

strategy.entry("buy", true, 1, when = greenCandle[G] and close[0]>open[0])

This gives me an entry 11 bars after the green but does not give me an entry after 11 consecutive greens.

1 Answers

I'd code it that way:

//@version=5
x = input.int(11)
red = close < open                       //red bar condition
count = ta.barssince(red)                //count of bars since red

if barstate.isconfirmed and count == x   //evaluates whether barstate is confirmed and count is equal to the given x
    strategy.entry("Long", strategy.long) //Enter Long

Related