Search code examples
pine-scriptpine-script-v5

Consecutive green/red bars or more since x


PLaying around with a new strategy. One of the conditions is to check if we have had 3 or more consecutive red bars (for long entry), then entering on the first green bar.

I've managed to code a basic strategy which I've attached that allows you to set the amount of red bars you want to consecutively see but I'm wondering is there a way to check if there been 3 or more red bars?

I was thinking maybe a loop but even then you need to give it a length to loop over

Also, shout to user @kyu23 for this code that I've modified slightly!

//@version=5
indicator("Consecutive bars since filter", overlay = true)
x = input.int(3)
red = close < open                       //red bar condition
countR = ta.barssince(red)                //count of bars since red

y = input.int(3)
green = close > open                       //green bar condition
countG = ta.barssince(green) 

bc = barstate.isconfirmed and countR == x
gc = barstate.isconfirmed and countG == x


plotshape(bc, style = shape.triangledown)
plotshape(gc, style = shape.triangleup)

Solution

  • Instead of countR == x and countG == x, you can do countR >= x and countG >= x