KeyError when using multiple dataframes



  • Pretty new to this library.. I'm loading a folder full of quandl csv files like this:

    stocks_backtest=[]
    for _ in dir_file_list('../stocks/SSE/'):
        stocks_backtest.append(GenericCSV_C(
        dataname=_,
        seperator=',',
        fromdate=datetime.datetime(2016, 01, 05),
        todate=datetime.datetime(2017, 01, 31),
    
        nullvalue=0.0,
        dtformat=('%Y-%m-%d'),
        datetime=0,
        high=1,
        low=2,
        open=4,
        close=3,
        volume=-1,
        openinterest=-1,
        rsi_6=5,
        rsi_12=6,
        macd=7,
        macds=8,
        adxr=9,
        boll=10,
        boll_ub=11,
        boll_lb=12,
        close_20_sma=13,
        close_50_sma=14,
        close_200_sma=15,
        dma=16))
    
    for idx, stock in enumerate(stocks_backtest):
       stock.plotinfo.plotmaster=stocks_backtest[0]
       cerebro.adddata(stock, name='d'+str(idx))
    
    cerebro.addsizer(TestSizer)
    cerebro.broker.setcommission(commission=0.001)
    
    cerebro.run()
    cerebro.plot()
    

    However stock.plotinfo.plotmaster leads to a key error:

    Traceback (most recent call last):
      File "/home/dav3/Documents/gits/stocks-predictor/backtest/backtest.py", line 64, in <module>
        cerebro.plot()
      File "/home/dav3/.local/lib/python2.7/site-packages/backtrader/cerebro.py", line 927, in plot
        start=start, end=end)
      File "/home/dav3/.local/lib/python2.7/site-packages/backtrader/plot/plot.py", line 220, in plot
        self.plotdata(data, self.dplotsover[data])
      File "/home/dav3/.local/lib/python2.7/site-packages/backtrader/plot/plot.py", line 630, in plotdata
        axdatamaster = self.pinf.daxis[data.plotinfo.plotmaster]
    KeyError: <mycsv.GenericCSV_C object at 0x7fe4246c5c50>
    

    when omitting the stock.plotinfo.plotmaster line it compiles, but only seems to consider the first stock. Not sure where to go with this.
    thx!


  • administrators

    One can't be plotmaster of itself. Try:

    cerebro.adddata(stocks_backtest[0], name = 'd0')
    for idx, stock in enumerate(stocks_backtest[1:], 1):
       stock.plotinfo.plotmaster=stocks_backtest[0]
       cerebro.adddata(stock, name='d'+str(idx))
    

  • administrators

    The answer was known because this was already a detected use case and was in the works. You may alternatively try the latest development version which allows plotmaster to reference to the data it belongs to (which allows for a clean loop like the one you were trying)


Log in to reply
 

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