'How to set closing price based on last swing high prior to entry?

I am trying to set a close/exit price to be equal to or greater than the last swing high prior to my entry (by "swing high" I mean upFractal in this case).

Also, it must work with pyramiding, just to complicate matters.

Please see this example - I colour-coded each entry with its related previous swing high. upFractal, and its exit:

enter image description here

I currently don't understand how to reference that swing high that is prior to each entry, or how to keep them distinct/unique to each entry as shown in the screen shot. I believe it possibly involves referencing the open trade number.

My maximum number of open trades (pyramiding) is likely going to be 5, which may be helpful to know.



Solution 1:[1]

Thanks for clarifying your question, in strategy, set process_orders_on_close to true

strategy("StackOverFlow", overlay=true, process_orders_on_close=true)`

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1