Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    KeyError when using multiple dataframes

    General Code/Help
    2
    3
    1146
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      derdav3 last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        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))
        
        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          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)

          • https://github.com/mementum/backtrader/commit/dd5c0c87b1895a7428b35930cffe8d364d21a271
          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }