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: fills are generated by internally aggregated bars instead of quote/ticks #1645

Closed davidsblom closed 1 month ago

davidsblom commented 1 month ago

Bug Report

Expected Behavior

Context: sandbox paper trading with Bybit. The strategy is subscribed to quote ticks and trade ticks. Internally aggregated bars should not generate fills.

Actual Behavior

A fill is generated based on an internally aggregated bar.

2024-05-16T13:13:47.194220001Z [INFO] PAPER-TRADING-001.ReinforcementLearningMarketMakerStrategy: <--[EVT] OrderAccepted(instrument_id=ETHUSDT-LINEAR.BYBIT, client_order_id=O-20240516-1313-001-000-37484, venue_order_id=BYBIT-679-29793, account_id=BYBIT-001, ts_event=1715865227193603001)
2024-05-16T13:13:47.203959001Z [DEBUG] PAPER-TRADING-001.OrderMatchingEngine(BYBIT): Processing QuoteTick(ETHUSDT-LINEAR.BYBIT,2970.62,2970.63,19.10,9.98,1715865227132999936)
2024-05-16T13:13:47.213644001Z [DEBUG] PAPER-TRADING-001.OrderMatchingEngine(BYBIT): Processing QuoteTick(ETHUSDT-LINEAR.BYBIT,2970.62,2970.63,20.78,12.70,1715865227143000064)
2024-05-16T13:13:47.224158001Z [DEBUG] PAPER-TRADING-001.OrderMatchingEngine(BYBIT): Processing QuoteTick(ETHUSDT-LINEAR.BYBIT,2970.62,2970.63,20.78,14.50,1715865227153999872)
2024-05-16T13:13:47.225103001Z [DEBUG] PAPER-TRADING-001.OrderMatchingEngine(BYBIT): Processing Bar(ETHUSDT-LINEAR.BYBIT-1-SECOND-MID-INTERNAL,2971.220,2971.220,2970.625,2970.625,928.31,1715865227000000000)
2024-05-16T13:13:47.225182001Z [DEBUG] PAPER-TRADING-001.OrderMatchingEngine(BYBIT): Applying fills to LimitOrder(SELL 0.03 ETHUSDT-LINEAR.BYBIT LIMIT @ 2_971.22 GTC, status=ACCEPTED, client_order_id=O-20240516-1313-001-000-37484, venue_order_id=BYBIT-679-29793, position_id=None, tags=None), venue_position_id=None, position=None, fills=[(Price(2971.220), Quantity(0.03))]
2024-05-16T13:13:47.227014001Z [ERROR] PAPER-TRADING-001.DataEngine: RuntimeError: Invalid price precision for fill 3 when instrument price precision is 2. Check that the data price precision matches the ETHUSDT-LINEAR.BYBIT instrument
2024-05-16T13:13:47.227219001Z [DEBUG] PAPER-TRADING-001.DataEngine: Data message queue stopped

Steps to Reproduce the Problem

  1. sandbox paper trading with Bybit
  2. Subscribe to quote ticks, trade ticks and 1-SECOND-MID-INTERNAL internal bars.

Specifications

cjdsellers commented 1 month ago

Hey @davidsblom

One thing you could try is setting bar_execution=False for add_venue.

In addition, I think it makes sense if internally aggregated bars are never processed for execution by the matching engine? (regardless of the above setting). I'm thinking that whatever data is being used to internally aggregate the bar will also be passing through the matching engine anyway, so this reduces the chance of these sorts of issues.

davidsblom commented 1 month ago

Cool! I'll use that setting. I also think that internally aggregated bars should never be processed for execution by the matching engine. Then the price precision error can also not happen, and the data that is being used to aggregate is indeed also passed through the matching engine.

cjdsellers commented 1 month ago

Nothing broke so pushed the change 876912de13d9bfb9a3c0a9d16de812519e1ebdaf.

Classifying it as a breaking behavior change, even though I'm not observing any test breaks.

davidsblom commented 1 month ago

Awesome! Many thanks 🙏