nautechsystems / nautilus_trader

A high-performance algorithmic trading platform and event-driven backtester
https://nautilustrader.io
GNU Lesser General Public License v3.0
1.71k stars 402 forks source link

Sandbox: Error on applying OrderFilled due to incorrect venu_order_id #1636

Closed davidsblom closed 1 month ago

davidsblom commented 1 month ago

Bug Report

Expected Behavior

No errors when using the sandbox.

Actual Behavior

The execution engine throws errors when applying order filled events. The venue_order_id of the order is not consistent with the event.

2024-05-11T07:13:22.162775001Z [ERROR] PAPER-TRADING-001.ExecEngine: Error on applying OrderFilled(trader_id=PAPER-TRADING-001, strategy_id=ReinforcementLearningMarketMakerStrategy-000, instrument_id=BTCUSDT-LINEAR.BYBIT, client_order_id=O-20240511-0713-001-000-1055, venue_order_id=BYBIT-617-853, account_id=BYBIT-001, trade_id=BYBIT-617-1338, position_id=BTCUSDT-LINEAR.BYBIT-ReinforcementLearningMarketMakerStrategy-000, order_side=BUY, order_type=LIMIT, last_qty=0.003, last_px=60_866.72 USDT, commission=0.0365 USDT, liquidity_side=MAKER, event_id=18d5b490-7862-42fb-95f1-a407faef5a2d, ts_event=1715411600913736001, ts_init=1715411600913736001) to LimitOrder(BUY 0.003 BTCUSDT-LINEAR.BYBIT LIMIT @ 60_866.72 GTC, status=ACCEPTED, client_order_id=O-20240511-0713-001-000-1055, venue_order_id=BYBIT-617-851, position_id=None, tags=None)
ValueError(The 'self.venue_order_id' <class 'nautilus_trader.model.identifiers.VenueOrderId'> of BYBIT-617-851 was not equal to the 'event.venue_order_id' <class 'nautilus_trader.model.identifiers.VenueOrderId'> of BYBIT-617-853)
Traceback (most recent call last):
  File "nautilus_trader/execution/engine.pyx", line 1007, in nautilus_trader.execution.engine.ExecutionEngine._apply_event_to_order
  File "nautilus_trader/model/orders/base.pyx", line 946, in nautilus_trader.model.orders.base.Order.apply
  File "nautilus_trader/core/correctness.pyx", line 306, in nautilus_trader.core.correctness.Condition.equal

Steps to Reproduce the Problem

  1. Use a sandbox live test.

Specifications

davidsblom commented 1 month ago

Pasting the logs here for reference. Note that there is no intelligence in this backtest. The agent actually submits random limit orders.

One observation is that there are many InvalidStateTrigger warnings. Then, at some point there errors appear. So, maybe it is related.

log.log

cjdsellers commented 1 month ago

Thanks for the feedback @davidsblom

I just pushed another change which further improves and tightens the venue_order_id indexing in the cache https://github.com/nautechsystems/nautilus_trader/commit/ee47d5ea492d3c3f8b2693b1a02f9331d095f529. Before this change, it was still possible for multiple venue order IDs to be indexed for the same client order ID.

Lets see how this goes.

davidsblom commented 1 month ago

Awesome! I'll give a try later this weekend.

davidsblom commented 1 month ago

Test is running. I'll update later tonight.

davidsblom commented 1 month ago

I think it is working now, at least the error hasn't happened for 2 hours. Another error occurred, but let's close this one :)