I’m trying to delay trades after a losing trade has happened, but with no success so far. My logic tells me this should work:
waitafterloss = input(1,'No. of bars to wait after loss')
newloss = (strategy.losstrades > strategy.losstrades[1]) and (strategy.wintrades == strategy.wintrades[1]) and (strategy.eventrades == strategy.eventrades[1])
barssince = ta.barssince(newloss)
waitcondition = barssince >= waitafterloss
But it doesnt.
I need the "waitcondition" to return true or false so I could use it in my order/entry execution.
Any ideas? What am I doing wrong? Maybe it would also be possible to have two inputs – delay and losing trades streak? Thanks
>Solution :
waitcondition returns true/false, however I would revert the comparison logic to match the purpose and variable name. The script below will pause new long entries in case the condition is met:
//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)
waitafterloss = input(100,'No. of bars to wait after loss')
newloss = (strategy.losstrades > strategy.losstrades[1]) and (strategy.wintrades == strategy.wintrades[1]) and (strategy.eventrades == strategy.eventrades[1])
barssince = ta.barssince(newloss)
waitcondition = barssince < waitafterloss
// bgcolor(waitcondition ? color.red : na) // <- uncomment to see the 'pause' on a chart.
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (longCondition) and not waitcondition
strategy.entry("long", strategy.long)
closeCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (closeCondition)
strategy.close("long")