Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pinescript trade delay after loss

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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") 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading