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

Data Feed Custom CSV List Indices Error



  • Hey guys, just started using backtrader and have been attempting to create a data feed with some Bitcoin minute data that I have in a csv. However, I've been getting an error when I try to create the data feed.

    Traceback (most recent call last):
      File "test.py", line 39, in <module>
        cerebro.run()
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/cerebro.py", line 1315, in runstrategies
        self.stop_writers(runstrats)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/cerebro.py", line 1340, in stop_writers
        datainfos['Data%d' % i] = data.getwriterinfo()
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/dataseries.py", line 101, in getwriterinfo
        info['Timeframe'] = TimeFrame.TName(self._timeframe)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/dataseries.py", line 57, in TName
        return cls.Names[tframe]
    TypeError: list indices must be integers or slices, not str
    

    I tried following the documentation as closely as possible but still wasn't sure why the error was being thrown. In addition, I made sure that when I created the CSV from my Jupyter Notebook that the date column was all objects, and the rest of the columns were all floats. Here's what my code looks like:

    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        datapath = os.path.join(modpath, '../datas/btc500.csv')
    
        # Create a Data Feed
        data = bt.feeds.YahooFinanceCSVData(
            dataname=datapath,
            datetime = 0,
            volume = 6,
            openinterest = -1,
            nullvalue=0.0,
            timeframe="Minutes")
    

    As well as a sample of what my CSV file looks like (opened it as a txt so I can paste it in that form here):

    Date,Open,High,Low,Close,Adj Close,Volume
    2020-04-20 19:01:00,6824.4,6837.13,6821.26,6837.13,6831.870216400000,64015.647342
    2020-04-20 19:02:00,6849.75,6863.77,6844.93,6857.31,6851.0944345,42113.893983
    2020-04-20 19:03:00,6851.25,6857.62,6844.07,6857.62,6846.991535100000,1640.9090463
    2020-04-20 19:04:00,6850.41,6857.61,6836.75,6836.75,6850.873640900000,45066.45302
    2020-04-20 19:05:00,6841.89,6844.48,6840.89,6840.89,6842.6786439,188.92622051
    2020-04-20 19:06:00,6850.66,6860.27,6850.66,6860.27,6858.885866500000,6731.358464400000
    2020-04-20 19:07:00,6852.08,6860.52,6852.08,6860.52,6859.4924315,7822.1489348000000
    2020-04-20 19:08:00,6854.65,6864.44,6854.65,6864.44,6864.3558986,41732.822992
    2020-04-20 19:09:00,6870.46,6878.59,6870.46,6878.59,6872.684638800000,44817.268477
    2020-04-20 19:10:00,6878.1,6878.1,6871.23,6872.54,6873.337389200000,12770.318646
    2020-04-20 19:11:00,6864.43,6868.41,6862.0,6868.41,6865.531478,4331.908764600000
    2020-04-20 19:12:00,6868.33,6869.45,6863.1,6863.1,6869.1421465,9789.0393786
    2020-04-20 19:13:00,6864.01,6868.27,6862.96,6863.3,6863.231328100000,4598.1927227
    2020-04-20 19:14:00,6862.52,6867.1,6862.52,6867.1,6863.8491706,2365.1162504
    2020-04-20 19:15:00,6866.75,6866.95,6864.46,6866.95,6866.765043800000,25422.717032
    2020-04-20 19:16:00,6878.14,6880.6,6868.73,6877.77,6872.2110527000000,13273.973284
    


  • BTW I tried changing bt.feeds.YahooFinanceCSVData to bt.feeds.GenericCSVData but I was getting this error

    Traceback (most recent call last):
      File "test.py", line 39, in <module>
        cerebro.run()
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/cerebro.py", line 1212, in runstrategies
        data.preload()
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/feed.py", line 688, in preload
        while self.load():
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/feed.py", line 479, in load
        _loadret = self._load()
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/feed.py", line 710, in _load
        return self._loadline(linetokens)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader/feeds/csvgeneric.py", line 118, in _loadline
        if self.p.timeframe >= TimeFrame.Days:
    TypeError: '>=' not supported between instances of 'str' and 'int'
    


  • Just solved my own issue, set timeframe parameter incorrectly, is supposed to be:

    timeframe = bt.TimeFrame.Minutes
    

Log in to reply
 

});