// Entry and Exit Conditions with a delay of 15 minutes (3 bars on 5-minute chart)
longCondition = close < smaValue and bullCond and (highVolatility or mediumVolatility or extremeLowVolatility)
shortCondition = close > smaValue and bearCond and (highVolatility or mediumVolatility or extremeLowVolatility)
// Initialize counter
// Define a function to enter positions after a delay
enterPositionAfter(delayBars, isLong) =>
var int counter = 0
if (na(counter))
counter := 0
if (na(counter) and (isLong ? longCondition : shortCondition))
counter := delayBars
// Decrement counter on each bar
counter := math.max(counter - 1, 0)
// Buy or sell signal when counter becomes zero
counter == 0
// Entry and Exit Conditions with a delay of 15 minutes (3 bars on 5-minute chart)
if (enterPositionAfter(3, true))
strategy.entry("Long", strategy.long)
if (enterPositionAfter(3, false))
strategy.entry("Short", strategy.short)
// Exit Long if close crosses above 50 SMA or if ATR-based stop loss is hit
if (close > smaValue)
strategy.close("LongExit")
// Exit Short at market price or if ATR-based stop loss is hit
if (close < smaValue )
strategy.close("ShortExit")
I am building pinescript strategy based on the above conditions. I need to delay the execution of the trade after the divergence occured and 3 candles has passed since. Is there anyway to implement it using pinescript.
I have tried different methods like barsince and bar_index, but nothing seems to be working.
To execute a trade after an event happens, you can use the ta.barssince function. This function accepts an event condition and returns how many bars have passed since its occurrence. I cannot understand everything your code is doing, so here's a very simple example using sma20 crossover as the event: