Enable each entry to have its own corresponding exit when pyramiding?

Viewed 21

Please see my basic entry and exit code:

if sellSig and ( require_rising_falling==false or falling )
    strategy.entry("Sell",strategy.short)
strategy.exit("ES",from_entry="Sell",stop=sell_sl,limit=sell_tp)

This works well when pyramiding is set to 1.

However, if I want to use pyramiding >1, I run into a big issue; namely, the same exit applies to all open trades.

Please see example of current behaviour:

enter image description here

You can see that both positions close on the same bar.

Desired behaviour would look something more like this:

enter image description here

Where each position would have its own exit relative to its entry.

What's the best way to facilitate this (ideally using my existing code as a starting point)? Thank you.

1 Answers

You can use unique entry ids:

if sellSig and ( require_rising_falling==false or falling )
    entryId = "Sell" + str.tostring(bar_index)
    strategy.entry(entryId, strategy.short)
    strategy.exit("ES", from_entry=entryId,stop=sell_sl,limit=sell_tp)
Related