'How to close a position only if it's positive

I'd like to set a condition to close my position only if the trade is positive. Meaning that if the condition to close my trade happened but we are actually negative then it doesn't close the trade and wait for the moment this closing condition happen again when we are in the green ?

Thank you



Solution 1:[1]

This will give the profit of the latest open trade. Combine it with your exit condition.

open_profit = strategy.opentrades.profit(strategy.opentrades - 1)

is_exit = my_exit_cond and (open_profit > 0)

Then use is_exit to exit a trade.

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 vitruvius