I'm coding a Donchian Channel trading system, where you go long on a 20-day high, and go short on a 20-day low.
I'm fine with coding the opening and closing of the positions, but I'm struggling with coding the initial Stop-Loss level, which I need to remain *fixed *while the whole position is open.
E.g. When you go long on the break of a 20-day high, I would like the initial stop loss to remain fixed at the 20-day low at the time of entry - and for this stop loss to not change.
I can code other versions of stop losses - like "strategy.position_average_price - atr(4)" but how would I link the stop loss to be fixed at the 20-day low at the point of entry?
DonchianLow=ta.lowest(close[1],20)
strategy.exit("Exit Long", from_entry="Long", stop=strategy.average_position_price - DonchianLow)
You must set your strategy.exit only once.
You can use a piece of code like this :
if strategy.position_size == 0 // no open order
if my_entry_condition
strategy.entry('long', strategy.long, qty=Q_Jetons, limit=limiteachat)
strategy.exit(id='Exit', from_entry="long", stop=StopLoss)
This way your strategy.exit is call one time only, and your SL won't change.