Page 1 of 1

Disable trading after Stop Loss is triggered

Posted: Thu Feb 09, 2023 1:57 am
by JAG
I'm having problems getting my strategy to stop re-entering the trade after the stop loss is triggered. Any advice would be appreciated thank you in advance. JAG

TPSLswitch = input(defval=false,title="Use SL & TP?",group="TP/SL CONDITION INPUTS HERE")
stopPer = input.float(1.5, step= 0.25, title='Reverse %',group="TP/SL CONDITION INPUTS HERE") / 100
takePer = input.float(100, title='Take Profit %',group="TP/SL CONDITION INPUTS HERE") / 100

//Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)

// Check if stop loss has been triggered
stopLossTriggered = false

if TPSLswitch and strategy.position_size < 0
strategy.exit('short', stop=shortStop, limit=shortTake, alert_message=shortexitmessage, comment="Stop Loss Hit")
stopLossTriggered != true

if TPSLswitch and strategy.position_size > 0
strategy.exit('long', stop=longStop, limit=longTake, alert_message=longexitmessage, comment="Stop Loss Hit")
stopLossTriggered != true

// Only enter trade if stop loss has not been triggered
if buyCond and not stopLossTriggered
strategy.close('short', alert_message=shortexitmessage, immediately = true)
strategy.entry('long', strategy.long, alert_message=longentrymessage)

if sellCond and not stopLossTriggered
strategy.close('long', alert_message=longexitmessage, immediately = true)
strategy.entry('short', strategy.short, alert_message=shortentrymessage)

Re: Disable trading after Stop Loss is triggered

Posted: Fri Feb 10, 2023 1:04 am
by Steve Burman
Hey JAG,

You could wrap the strategy.entry statement around an if statement such as:

if strategy.position_size == 0
strategy.entry()

I hope this helps!