0

i want to make an 1 hour delay after my strategy hit stop loss or take profit. Here is an example that is not working, would you explain me why or figure it out? PineScript v5 on tradingview



xSMA = ta.sma(close, Length)
xHighBand = xSMA + (xSMA * PercentShift / 100)
xLowBand = xSMA - (xSMA * PercentShift / 100)

entershort = close > xHighBand
enterlong = close < xLowBand
//reopenPositionAfter(timeSec) =>
   // if strategy.closedtrades > 0
        //if time - strategy.closedtrades.exit_time(strategy.closedtrades - 1) >= timeSec * 100000
           // strategy.entry("Long", strategy.long)

conditionFilter =  not ta.barssince(ta.change(strategy.closedtrades)) == 5
//barsSinceClosed = strategy.closedtrades > 0 ? bar_index - strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1) : na
//barsSinceClosed = bar_index - strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1)

isFlat = (strategy.position_size == 0)
    
if (  isFlat and enterlong and  inDateRange and conditionFilter )
    strategy.entry("Long", strategy.long)
if (  isFlat and entershort and  inDateRange and conditionFilter)
    strategy.entry("Short", strategy.short)  

strategy.exit("Long Exit", "Long", profit = close * 0.01 / syminfo.mintick, loss = close * 0.005 / syminfo.mintick)
strategy.exit("Short Exit", "Short", profit = close * 0.01 / syminfo.mintick, loss = close * 0.005 / syminfo.mintick)

plot(xSMA, color=color.green, title="SMA")
plot(xHighBand, color=color.blue, title="High Band")
plot(xLowBand, color=color.blue, title="Low Band")

if (not inDateRange)
    strategy.close_all()

I tried function barssince or strategy.closedtrades

1 Answers1

0

To pause my strategy for X bars, I use this code :

var tradesperdus = 0
var tradesgagnes = 0

var index_bar_echec = 0
var cooldown = 8 // Here I want to pause for 8 bars

if strategy.losstrades > tradesperdus // A new LOSS
    tradesperdus := strategy.losstrades
    index_bar_echec := bar_index // I record the position (in bars) of my loss
        
if strategy.wintrades > tradesgagnes // A new WIN
    tradesgagnes := strategy.wintrades
    index_bar_echec := 0

// I can now delay my strategy below if I get a loss before
if bar_index > index_bar_echec + cooldown
    // my strategy here ...
G.Lebret
  • 2,826
  • 2
  • 16
  • 27