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

more data more problems

  • presume I add 4 sets of data, exactly as the following, just different names:

    start_date =, 1, 1)
    end_date =, 4, 1)
    AAPL = bt.feeds.GenericCSVData(
        dtformat=('%Y-%m-%d %H:%M:%S'),
        sessionstart=datetime.time(9, 30),
        sessionend=datetime.time(16, ),

    then I add the data and a SINGLE strategy:

    #     cerebro.adddata(AAPL)
    #     cerebro.adddata(MSFT)
    cerebro.addstrategy(TestStrategy, ticker_0='GOOG',ticker_1='C')

    the output for the strategy might be :
    Starting Portfolio Value: 1000000.00
    Final Portfolio Value: 1001815.16

    now if I uncomment those two cerebro.adddata lines with out changing anything else, and my data in strategy is defined by the dname:

        self.dataset_0 = eval(self.p.ticker_0)
        self.dataset_1 = eval(self.p.ticker_1)

    my output for the strategy changes to the following:
    Starting Portfolio Value: 1000000.00
    Final Portfolio Value: 1001554.27

    ***How does adding data that is never called seem to effect my live strategies ? ***

    data sample is as followed:

    2017-01-03 11:30:00,28.3400,27.9901,28.2500,28.0600,216786,188503
    2017-01-03 12:50:00,28.1100,27.9800,28.0500,28.0400,316431,79297
    2017-01-03 14:10:00,28.0400,27.8900,28.0400,27.9100,411203,78593
    2017-01-03 15:30:00,27.9500,27.8600,27.9150,27.8900,548365,114589
    2017-01-03 16:00:00,28.0700,27.8800,27.8900,28.0600,701523,130827

    strategy logic for placing orders is as followed:

        if self.days > 1:
            # If we're not in the market...
            if not self.invested:
                if et < -sqrt:
                    # Long Entry
                    self.cur_hedge_qty = 500
          , size=self.qty)
                    self.sell(data=self.dataset_0, size=self.cur_hedge_qty)
                    self.invested = "long"
                elif et > sqrt:
                    # # Short Entry
                    self.cur_hedge_qty =500
                    self.sell(data=self.dataset_1, size=self.qty)
          , size=self.cur_hedge_qty)
                    self.invested = "short"
            # If we are in the market...
            if self.invested:
                if self.invested == "long" and et > -sqrt:
                    self.log('CLOSING LONG')
                    self.order = self.close(data=self.dataset_1)
                    self.order = self.close(data=self.dataset_0)
                    self.invested = None
                elif self.invested == "short" and et < sqrt:
                    self.log('CLOSING SHORT')
                    self.invested = None

  • @backtrader
    this problem seems to be related to different files not having matching bars, however, they are the same time frame.

    when filter out in the CSV file the pre-market and after market data i have no issues with this problem at all.

    I guess to properly fix this I would need to before I import a file to check the length and then import the largest to smallest in terms of length of bars first?

  • That could happen if your data doesn't have the same start/end dates or is not properly aligned. What I do to deal with this is load a dummy reference data that goes from the start to the end (I don't use the data in the code, just the date).

    Adding the ticker in your strategy as below seems a bit dodgy:

    cerebro.addstrategy(TestStrategy, ticker_0='GOOG',ticker_1='C')

    Instead what I do is something like:

    cerebro.adddata(GOOG, name='GOOG')

    And then when you go through your data, you can discard any serie which you do not want to trade. For dealing with data with different start/end dates, you need to add prenext(), there are plenty of posts on this, you should go through the forum.

    Looks like this:

        def prenext(self):
        def next(self):
            if self.order:
            # Initialize Data
            fdata = [(i, dta) for i, dta in enumerate(self.datas[1:]) if len(dta)]
            if not fdata:
            # Debugging
            series = ['(Index: {} - Date: {})'.format(fdata[i][0], fdata[i][1] for i, dta in enumerate(fdata)]
            self.debug('Reference: {} - {}'.format(, series))

    You can get the datanames like below:

            self.names = self.getdatanames()
            del self.names[0]  # Remove Reference
            self.debug('Symbols: {}'.format(self.names))

    PS: This is an extract from my code so you might have to adapt it a bit to get it to work for you.
    PS2: See

  • administrators

    @laurent-michelizza said in more data more problems:

    That could happen if your data doesn't have the same start/end dates or is not properly aligned

    Only the 1st. The system won't enter the next phase of the strategy until all data feeds (and also the associated indicators) have covered the warm-up period (aka as minperiod)

    The reason is clear: in next all elements are guaranteed to have a value available at index [0]. Hence the need to await the minimum period of all elements to be consumed.

    If you need to access data before, use prenext, but each element will have to be checked for actual data.


Log in to reply

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.