Backtesting with Futures data (Actual individual contract)
I'm currently backtesting a Futures strategy that trades the entire curve (i.e. does not only trade front contract). It means that I need to work actual Futures contract rather than stitched futures contract (e.g. #1, #2, #3).
What I do is to add every Futures contract as a data feed and then the strategy has code to retrieve the datafeeds (that corresponds to contract that have not expired) sorted by expiration date. One of the issue I have faced is that these individual contracts don't have all data for every date (let assume I perform a backtest over 10Y; it is easy to understand that Dec2019 contract had no data early 2010) and therefore the backtest is only performed with a subset of the contracts (i.e. Datafeed) and only from the date where the last datafeed added to Cerebro has data.
I have temporary "resolved" this issue by reindexing my dataframes (and therefore adding nan date) before loading them to backtrader to ensure that all datafeeds have records from the start date till the end date I want. Is there by chance something more elegant than this I could do?
Have you tried the rollover functionality?
yes, for other strategies. but in this case, I need to be able to trade at different point of the forward curve (e.g. front contract and contract expiring in one year; a few weeks later front contract and contract expiring in 6 months). Plus in this case, it is actually useful to me to know which actual individual contract has been traded
ab_trader last edited by
If you load all contracts as separate data feeds into
btwill build the date-time axis from the very first date of the very first contract to the last date of the last contract. If I remember correctly, this is how I did it some time ago.
therefore the backtest is only performed with a subset of the contracts (i.e. Datafeed) and only from the date where the last datafeed added to Cerebro has data
To make the backtest along the whole history put trading logic into both
next(). But accurately check if data available in the
Thanks, ab_trader and sorry for getting back so late. I'll double-check your claim and will update if it does not behave like you describe; for now, my trick works for it feels unnecessary.