'Execute something upon strategy.exit()

I'd like to change a couple of variables or maybe call a function upon strategy.exit(), preventing a new trade to open on the same bar (I have "calculate on every tick" set to true), but can't find a way. Is there any syntax to do it? It's actually odd, because I trigger strategy.entry() when I have a ta.cross(), but once the trade closes, if in that same bar a ta.cross() has occurred, the strategy will entry again, and of course not at the ta.cross price. :(((

I would love to update a flag upon strategy.exit().



Solution 1:[1]

How about adding this to your entry condition?

canEnter = strategy.closedtrades.exit_bar_index(strategy.closedtrades-1) != bar_index

This would be true on all bars except on the bar the latest closed trade has exited. So you could say something like this to add it to your entry condition:

longCondition and canEnter

Cheers and best of luck with your trading and coding

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 Bjorgum