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 multiple symbols with different date ranges - backtest won't start until all data feeds have data



  • Hi i have been researching backtesting systems and i read that Backtrader won't start a backtest until all data feeds have data.
    For example if if i have AAPL from 2000-2017 and GOOG from 2012-2017, the backtest won't start until 2012 (first day of GOOG data)
    Is this true is there anyway to fix?

    ref: https://ntguardian.wordpress.com/2017/06/12/getting-started-with-backtrader/



  • Just for some background info: i have a database of thousands of stocks (survivorship bias free) so some of the stocks have varied date ranges and no longer trade, i want to backtest over the whole universe of stocks.



  • I believe it is possible with certain basic code improvements. I think this was also discussed on the forum, but I was not able to find the topic.

    PS You will need to do operations in both prenext() and next() functions as well as need to check what data is already delivered and what doesn't.



  • Thanks ab_trader, i'm surprised as i thought there would be many people wanting to backtest multiple symbols at once, and naturally some symbols will have a longer history than others.
    I'll look into prenext to see if there is anything that can solve this problem.



  • looks like prenext might be a good solution, run the strategy in prenext and deal with min bars for my indicators in my own code.
    I do my heavy analysis and backtesting in quantstrat (R framework) which handles this issue i'm talking about. But "backtester" looks promising so being able to backtest and go live in the same framework would be cool. I can't see myself fully doing a way with R though.



  • def prenext(self):
        self.next()
    
    def next(self):
        feeds_with_data = [(i, d) for i, d in enumerate(self.datas) if len(d)]
        ...
    

    That should give you the data feeds which actually have data. The 2nd feat is to keep the indicators associated to the data. That's what enumerate is doing by giving you the index, which you can use into arrays which keep the indicators for the data.

    You should bear in mind that even if the feed has already data, that doesn't mean the indicators are ready yet. A similar scheme (len check) for each indicator is therefore needed.



  • Note that data0 is the master data and sets the time when trades will start (using Paska Houso's trick above). So make sure data0 is the data feed with the oldest start date.



  • @benoît-zuber said in Backtesting multiple symbols with different date ranges - backtest won't start until all data feeds have data:

    Note that data0 is the master data and sets the time when trades will start

    I thought that restriction was removed some time ago in backtrader. Are you sure?



  • @paska-houso Yes I have just tried again with the latest version and I am still getting the same result.