0

In Pine Script 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?

Many, many thanks,

David.

DonchianLow=ta.lowest(close[1],20)

strategy.exit("Exit Long", from_entry="Long", stop=strategy.average_position_price - DonchianLow)

1 Answers1

0

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.

G.Lebret
  • 2,826
  • 2
  • 16
  • 27
  • Many thanks for your reply @G.Lebret - this helps a lot. I tried it though, but my stop-loss still goes up as the 20-day low also goes up, but I'd like it to stay as a fixed stop-loss. I've used this code: '''if EnterLong strategy.entry("Long",strategy.long,qty=PosSize) strategy.exit(id='Long Stop', from_entry="Long", stop=DonchLow)''' – Dave_R_Pierce Nov 22 '22 at 12:59
  • if strategy.position_size == 0 prevent your SL to be modified as long as an order is open. did you try with if strategy.position_size == 0 ? – G.Lebret Nov 23 '22 at 00:44
  • I have tried it with this, and it works perfectly. Thanks very much for your help! – Dave_R_Pierce Dec 01 '22 at 13:10