nautechsystems / nautilus_trader

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

backtesting problems #610

Closed graceyangfan closed 2 years ago

graceyangfan commented 2 years ago

Steps to Reproduce the Problem

1.backtesing fx_market_maker_gbpusd_bars.py when change oms_type to OMSType.HEDGING ,and engine.run(end=datetime(2012,4,10))

RiskEngine: SubmitOrder DENIED: Exceeded MAX_ORDER_RATE is meet

2.backtesing fx_market_maker_gbpusd_bars.py set oms_type to OMSType.NETTING . Printing the positions of the backtest process,only one position is record,in fact,several orders has filled,the finial statics can not perform compute on return list,since there is only one element in list.

Specifications

cjdsellers commented 2 years ago

Thanks for the testing.

To your first point, it isn't actually correct to run this strategy in HEDGING OMS mode as every new position is separately ticketed and tracked. This means at the end of the strategy all open positions will be closed at once, and there are 154 of them which will exceed the default max order rate setting.

2012-03-01T00:00:00.000000000Z [INF] BACKTESTER-001.BACKTESTER-001: STOPPING...
2012-03-01T00:00:00.000000000Z [INF] BACKTESTER-001.VolatilityMarketMaker-001: STOPPING...
2012-03-01T00:00:00.000000000Z [INF] BACKTESTER-001.VolatilityMarketMaker-001: Canceling 2 open orders...
2012-03-01T00:00:00.000000000Z [INF] BACKTESTER-001.VolatilityMarketMaker-001: [CMD]--> CancelAllOrders(instrument_id=GBP/USD.SIM).
2012-03-01T00:00:00.000000000Z [INF] BACKTESTER-001.VolatilityMarketMaker-001: Closing 154 open positions...

Probably the logic of this example could be improved further, to keep track of the currently open position ID, regardless of the OMS mode.

To your second point, this is one current limitation of the NETTING OMS mode, in that the analyzer currently relies on separate positions being tracked to assess the performance of each individual trade.

We need to upgrade the cache to retain memory of complete positions open to closed separately, so that we can pull out all the individual returns for each, and properly calculate the statistics.

This also relates somewhat to another issue which has been discovered that running the strategy live over many days sees the memory consumption increase unbounded, because all of the events are being held in memory and never flushed to disk. A solution for this is pending.

cjdsellers commented 2 years ago

The BacktestEngine is now able to compute statistics for strategies using the NETTING OMS.