For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Backtesting with Futures data (Actual individual contract)



  • Dear All,

    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?

    Many thanks
    Regards,
    Lamp'


  • administrators



  • 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



  • If you load all contracts as separate data feeds into bt, than bt will 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.

    @lampalork said in Backtesting with Futures data (Actual individual contract):

    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 prenext() and next(). But accurately check if data available in the prenext() call.



  • 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.


Log in to reply
 

});