Skip to content

Infinite recursion on submit order in the position opened hook #2554

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

Closed
stastnypremysl opened this issue Apr 24, 2025 · 1 comment
Closed
Assignees
Labels
bug Something isn't working

Comments

@stastnypremysl
Copy link
Collaborator

Bug Report

In given files infinite-recursion-on-submit-order-in-position-opened_bug.py and infinite-recursion-on-submit-order-in-position-opened_bug.out are a strategy script and its output. The strategy opens a position and then on_opened_position opens a order.

Expected Behavior

Strategy doesn't end up in a infinite recursion loop due to order submission. Furthermore on_position_opened can be called only once per position opened.

Actual Behavior

Strategy ends up in a infinite recursion loop due to order submission. Furthermore on_position_opened is called multiple times on single position opened.

Steps to Reproduce the Problem

  1. Setup catalog and pair in ./infinite-recursion-on-submit-order-in-position-opened_bug.py with "reasonable" trade tick data on volatile pair (if data for exact replication of the problem is wanted, please contact me directly)
  2. ./infinite-recursion-on-submit-order-in-position-opened_bug.py >./infinite-recursion-on-submit-order-in-position-opened_bug.out 2>&1
  3. less ./infinite-recursion-on-submit-order-in-position-opened_bug.out

Specifications

  • OS platform: Gentoo Linux (native build, amd64)
  • Python version: Python 3.12.9 (native build)
  • nautilus_trader version: 1.217.0 (revision 1a7aa65; make build-wheel)

Additional notes

  • The simulation is running on aggregated trade ticks from Binance data collection
  • The instrument information was downloaded using the NT Binance client
  • Ignore the 1.217.5 version in the log - no changes in the source codes was made other then change of the version
@stastnypremysl stastnypremysl added the bug Something isn't working label Apr 24, 2025
@cjdsellers cjdsellers self-assigned this Apr 25, 2025
@cjdsellers cjdsellers moved this to In progress in NautilusTrader Kanban Board Apr 25, 2025
@cjdsellers
Copy link
Member

cjdsellers commented Apr 25, 2025

Hi @stastnypremysl

Thanks for the report.

This was an interesting regression caused by the changes made to improve the event sequencing consistency for #2513.

Now fixed from commit 62b02ed by ensuring we pop pending events to be published to avoid recursion when the subsequent arrival of these events may trigger further order/position events.

An initial black box MRE style test was also added to assert both the expected ordering of account, order and position events arriving at the strategy, and also no recursion per the MRE.

The MRE now runs to the end of the backtest without the recursion (when account balance is increased to 100,000 USDT - to avoid a premature stop due account balance exceeded).

Also worth mentioning is that the original MACD strategy had logic issues which were causing the risk engine to correctly deny the reduce_only orders (this will be outlined in more detail in a follow-up to #2534).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

2 participants