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

Error with two data feeds with different history length



  • Hi guys,

    I've found 2 problems when using 2 data feeds - one with history longer than the other:

    from datetime import datetime
    import backtrader as bt
    
    class SmaCross(bt.SignalStrategy):
        def __init__(self):
            # ko
            crossover1 = bt.ind.CrossOver(self.data0, self.data1)
            # ok
            crossover2 = bt.ind.CrossOver(self.data1, self.data0)
    
            # ko
            self.data0 - self.data1
            # ok
            self.data1 - self.data0
    
    cerebro = bt.Cerebro()
    cerebro.addstrategy(SmaCross)
    
    # data since 2004-11-18
    data0 = bt.feeds.YahooFinanceCSVData(dataname='data/GLD.csv', 
                                         fromdate=datetime(2006, 1, 1),
                                         todate=datetime(2007, 1, 1))
    cerebro.adddata(data0)
    
    # data since 2006-04-28
    data1 = bt.feeds.YahooFinanceCSVData(dataname='data/SLV.csv', 
                                         fromdate=datetime(2006, 1, 1),
                                         todate=datetime(2007, 1, 1))
    cerebro.adddata(data1)
    
    cerebro.run()
    cerebro.plot()
    

    The first error is in indicators/crossover.py:

    $ python pair_bug.py
    Traceback (most recent call last):
      File "pair_bug.py", line 31, in <module>
        cerebro.run()
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/cerebro.py", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/cerebro.py", line 1293, in runstrategies
        self._runonce(runstrats)
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/cerebro.py", line 1652, in _runonce
        strat._once()
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/lineiterator.py", line 297, in _once
        indicator._once()
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/lineiterator.py", line 297, in _once
        indicator._once()
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/lineiterator.py", line 297, in _once
        indicator._once()
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/lineiterator.py", line 318, in _once
        self.once(self._minperiod, self.buflen())
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/indicators/crossover.py", line 58, in once
        d = d0array[i] - d1array[i]
    IndexError: array index out of range
    

    The second is in linebuffer.py:

    $ python pair_bug.py
    Traceback (most recent call last):
      File "pair_bug.py", line 31, in <module>
        cerebro.run()
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/cerebro.py", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/cerebro.py", line 1293, in runstrategies
        self._runonce(runstrats)
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/cerebro.py", line 1652, in _runonce
        strat._once()
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/lineiterator.py", line 297, in _once
        indicator._once()
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/linebuffer.py", line 631, in _once
        self.once(self._minperiod, self.buflen())
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/linebuffer.py", line 755, in once
        self._once_op(start, end)
      File "/Users/adam/.pyenv/versions/backtrader/lib/python3.5/site-packages/backtrader/linebuffer.py", line 772, in _once_op
        dst[i] = op(srca[i], srcb[i])
    IndexError: array index out of range
    

    As you can see in my code sample, it works when you flip the data feeds - if you put the shorter history data first (data1), it works fine, otherwise it throws error.

    My env is macOS Catalina 10.15.3, Python 3.5.9 with packages:

    $ pip freeze
    backtrader==1.9.74.123
    cycler==0.10.0
    kiwisolver==1.1.0
    matplotlib==3.0.3
    numpy==1.18.3
    pyparsing==2.4.7
    python-dateutil==2.8.1
    six==1.14.0
    

    Any help would be appreciated :)



  • use

    cerebro.run(runonce=False)
    

    this will turn off vectorized mode.



  • It worked, thanks!

    But isn't vectorized mode much faster for optimization, etc.?



  • yes, faster. but in your case you have empty data bars, and looks like this doesn't work with vectorized approach.


Log in to reply
 

});