Skip to content

Add additional event hook and system process for strategy stop/graceful shutdown #2572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stastnypremysl opened this issue Apr 28, 2025 · 0 comments
Labels
RFC A request for comment

Comments

@stastnypremysl
Copy link
Collaborator

stastnypremysl commented Apr 28, 2025

RFC: Change of the system of stopping strategy, so positions and orders are both always closable in the live trading

Context

Consider situation, when you want to close all orders and all positions in on_stop of the strategy. Now, you can't know at the time of closing position, how large reduce order is needed to be sent. You could close the positions on on_cancel_order hook, when all the orders are closed, but it won't be called in current design. (if I understand the design right - it certainly won't be called, if it is disposed in meantime)

For example, if you use following code:

    cpdef void on_stop(self):
         self.cancel_all_orders(self.instrument_id)
         self.close_all_positions(self.instrument_id)

There might be some orders filled in between, before cancel_order(s) and reduce_only orders from close_all_positions and cancel_all_orders are delivered to the venues.

This means, that after this block of code, it is still possible, the position is opened.

Considerations

I propose to implement this, until NT is beta, as this will be probably required by some users in the future and it presents breaking API change.

I would propose to make a solution, when the strategy itself must make a signal, it is stopped, so all positions are always possible to be closed on_cancel_order hook.

There is also a situation, when you trade such large quantity, that you need more time for a graceful shutdown, and only the strategy knows, when it stopped (in general case).

I would also propose to make the backtest fail, when a strategy on the end of a simulation doesn't send the signal, so the developer always knows about the mistake. I also propose a parameter for the graceful shutdown timeout in the backtest, so the strategy has always some time to shutdown. (the on_stop hook would be called at stop_time - shutdown_timeout).

@stastnypremysl stastnypremysl added the RFC A request for comment label Apr 28, 2025
@stastnypremysl stastnypremysl changed the title Change of the system of stopping strategy, so positions and orders are both always closable in live trading Change of the system of stopping strategy, so positions and orders are both always closable in the live trading Apr 28, 2025
@cjdsellers cjdsellers changed the title Change of the system of stopping strategy, so positions and orders are both always closable in the live trading Add additional event hook and system process for strategy stop/graceful shutdown May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC A request for comment
Projects
None yet
Development

No branches or pull requests

1 participant