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

Backtesting process halts upon Unsubscribing from Internal TimeBars during Mid-Test #1606

Closed rsmb7z closed 2 months ago

rsmb7z commented 2 months ago

Bug Report

Backtesting process halts upon unsubscribing from Internal TimeBars during a test.

Expected Behavior

Backtest shouldn't raise exception.

Actual Behavior

Raises exception and stops abnormally.

Steps to Reproduce the Problem

  1. Run backtest multiple strategies having Internal time-based Bar subscription
  2. Stop and Unsubscribe the Bars for any of the Strategy (real use case as the FuturesContract approaching expiry)
  3. Backtest terminates with below exception
  File "nautilus_trader\\backtest\\engine.pyx", line 928, in nautilus_trader.backtest.engine.BacktestEngine.run
  File "nautilus_trader\\backtest\\engine.pyx", line 1090, in nautilus_trader.backtest.engine.BacktestEngine._run
  File "nautilus_trader\\backtest\\engine.pyx", line 1179, in nautilus_trader.backtest.engine.BacktestEngine._advance_time
  File "nautilus_trader\\backtest\\engine.pyx", line 1226, in nautilus_trader.backtest.engine.BacktestEngine._process_raw_time_event_handlers
  File "nautilus_trader\\data\\aggregation.pyx", line 730, in nautilus_trader.data.aggregation.TimeBarAggregator._build_bar
  File "nautilus_trader\\data\\aggregation.pyx", line 753, in nautilus_trader.data.aggregation.TimeBarAggregator._build_bar
  File "nautilus_trader\\common\\component.pyx", line 619, in nautilus_trader.common.component.TestClock.next_time_ns
  File "nautilus_trader\\core\\correctness.pyx", line 790, in nautilus_trader.core.correctness.Condition.valid_string
  File "nautilus_trader\\core\\correctness.pyx", line 145, in nautilus_trader.core.correctness.Condition.not_none
TypeError: The 'name' argument was None

Specifications

DracheShiki commented 2 months ago

By live-trading also. Portfolio can't be initialized. But it's weird that no one else reports the same thing... "high-level" node config is used as usual. guess that maybe a node with config "name" is needed.

benjaminsingleton commented 2 months ago

I'm also getting the same portfolio not initialized failure in live (paper) trading. Not sure if that's definitely related to this issue. I can post the full logs later today.

cjdsellers commented 2 months ago

This just seems to affect internally aggregated time bars.

When there are no more subscribers, the DataEngine stops the aggregator which cancels its internal timer - then for some reason the timer name is set to None.

I feel like removing this is a bit of a Chesterton fence thing, but I "can't see a good reason for having this here" and all the tests seem to pass after I remove it.

Pushed to develop branch.

The other issue regarding name is unrelated to this, and was probably caused by a change to accommodate multiple clients where the name wasn't actually being wired through to the client_id. I haven't dug into it yet though.

Closing this one as fixed, and lets open a specific issue for the Portfolio problem, logs or a stack trace would be helpful :pray:.

rsmb7z commented 2 months ago

Pushed to develop branch.

Works like a charm—thanks for the fix!