Skip to content

Inconsistency in account updates across event hooks #2513

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 13, 2025 · 3 comments
Closed

Inconsistency in account updates across event hooks #2513

stastnypremysl opened this issue Apr 13, 2025 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@stastnypremysl
Copy link
Collaborator

Bug Report

In the given file, there is a Strategy, which sends two orders one after another (market, then limit).

Expected Behavior

Account is updated consistently on on_portfolio_changed, on_order_filled and on_order_accepted hooks. (Either all before or all after - I would personally prefer the before approach as you can operate with the correct numbers in the time the hook is called)

Actual Behavior

After the market order is filled, the on_order_filled hook shows the old locked balance value. The on_order_accepted and on_portfolio_changed hooks shows current locked balance value.

Steps to Reproduce the Problem

  1. Run the strategy in the Jupyter notebook with "reasonable" trade tick data on volatile pair
  2. Find second, third and forth color line from strategy log
  3. Evaluate the values

Image

Specifications

  • OS platform: Gentoo Linux (native build, amd64)
  • Python version: Python 3.12.9 (native build)
  • nautilus_trader version: 1.215.0 (official build)
@stastnypremysl stastnypremysl added the bug Something isn't working label Apr 13, 2025
@stastnypremysl stastnypremysl changed the title Inconsitency in account updates across event hooks Inconsistency in account updates across event hooks Apr 13, 2025
@cjdsellers
Copy link
Member

Hi @stastnypremysl

Thanks for the report.

This required some deeper investigation. It was found that in the case of OrderFilled handling in the ExecutionEngine, there was a separate path which changed the ordering of event types being published on the message bus. This was because some event publishing was interleaved as a single event was being handling by the engine.

The fix involves centralizing the following into a single code block:

  • Order event publishing
  • Order snapshot
  • Position event(s) publishing
  • Position snapshot(s)

The way Portfolio handles events for account state updates was also improved by consolidating the calculations into a single event hook for order events (and so no longer requires a subscription for position events).

Now on develop branch from commit e9b9f77.

You should now see the account state consistently updated as events arrive at the strategy.

Please report if you see any further issues.

@cjdsellers
Copy link
Member

Consistent ordering of event types is now verified per #2554.

See initial black box MRE style test.

These test asserts are just the minimum necessary to verify the behavior, there is much scope for improvement but at least this issue is now resolved.

@cjdsellers cjdsellers moved this from In progress to Done in NautilusTrader Kanban Board Apr 25, 2025
@cjdsellers
Copy link
Member

Reopened due commit 5f6b89f which partially reverted the refactoring to consolidate account events based on position events.

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