Search code examples
pine-scriptpine-script-v5candlestick-chart

pinescript V5 I'd like to have more than 5 candles of clearance between entry


enter image description here

I'd like to have more than 5 candles of clearance between entry

But my code doesn't run

s = strategy.position_size > 0

t = ta.barssince(ss)

can_buy = (t1 > 5)

help me please. thanks.


Solution

  • can_buy = (t[1] > 5)
    

    is wrong, just use t:

    can_buy = (t > 5)
    

    This for example works, every five candles it opens and closes a position

    s = strategy.position_size > 0
    
    if s
        strategy.close("long")
    
    t = ta.barssince(s)
    
    can_buy = (t > 5) or barstate.isfirst
    
    if can_buy
        strategy.entry("long", strategy.long)
    

    A dummy example just to show you. Also use barstate.isfirst, otherwise the strategy will never open the first position (cause position_size has neve been > 0)