Description
Expected Behavior
It is possible to track which order resulted in which trade.
Actual Behavior
Once an order is filled, it is discarded. This is fine, especially to save on resource usage, but there is no way to track the relationships between orders and trades over the long term.
Steps to Reproduce
- Run a long backtest which will result in multiple trades being taken out by multiple orders at the same price and tp/sl
- Attempt to record exactly what happened during trading for later dissection and detailed reporting
Additional info
Background on this is that all our usual trading software traces an order with an order id
and that id
persists throughout all the other record types. In backtesting.py's case, this is trades
and closed_trades
. If similar tracking was possible in backtesting.py, that would enable:
- a wealth of existing tools to be used with the data that can be output from this solution
- more complex contingent trading algorithms to be tested in backtesting.py
- additional data to be stored about each order or trade, depending on trading method
Proposed change
I am willing to submit a pull request containing changes to:
- Enable an optional integer parameter for
strategy.buy()
,strategy.sell()
andTrade()
, which would be aninteger
(? or any data type? ) namedid
- This user supplied number would be accessible as another parameter on both
Order
andTrade
- This user supplied number would be transferred to a
Trade
when an order is filled and subsequently discarded
However before I do this, I just wanted to check that people (and particularly the author @kernc) think it is a good idea and that there isn't a better way of solving this problem.
Version info
- Backtesting version: 0.3.0