Delayed trade reentry

Viewed 122

I’m new to stackoverflow and have some coding experience. I’m having an issue with my trading algo reentering a trade directly after my stop loss is hit. I would like to add a variable to delay trade reentry for a certain number of bars even if the conditions are being met. I’ve tried many different things and I am stuck. Any help would be greatly appreciated.

Thanks, Rob

1 Answers

This pseudo-code is an example of how to delay your trade entry.

//@version=5
indicator("Trade reentry", overlay=true)

var bool    canEnterNewTrade    = true
var bool    enterTradeCondition = true
var bool    stopLossHit         = true
var bool    tradeActive         = false
var int     stopLossBar         = 0
var int     delayInBars         = 20

canEnterNewTrade := not tradeActive and (bar_index - stopLossBar) >= delayInBars

if canEnterNewTrade and enterTradeCondition
    tradeActive := true // enter the trade

if stopLossHit
    stopLossBar := bar_index
    tradeActive := false // exit the trade

plot(na)
Related