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

Dataframe with min bar data cannot be loaded



  • I try to load a plain simple dataframe to backtrader as follows:

    df = ohlc_df[['datetime', 'open', 'high', 'low', 'close', 'volume']]
    df.set_index('datetime', inplace=True)
    df.head()
    

    The datetime is the index and is a proper datetime type.

                                  open     high      low    close      volume
    datetime                                                                 
    2020-10-01 00:00:00+00:00  10778.5  10783.5  10778.0  10783.5   2283372.0
    2020-10-01 00:01:00+00:00  10783.5  10805.5  10783.0  10798.5  12216553.0
    2020-10-01 00:02:00+00:00  10798.5  10798.5  10798.0  10798.0   1290372.0
    2020-10-01 00:03:00+00:00  10798.0  10800.0  10791.0  10797.5   2017493.0
    2020-10-01 00:04:00+00:00  10797.5  10826.5  10797.0  10813.5   7358318.0
    
    data = bt.feeds.PandasData(dataname=df)
    cerebro = bt.Cerebro(stdstats=False)
    cerebro.adddata(data)
    
    cerebro.addstrategy(bt.Strategy)
    
    cerebro.run()
    

    Produces an error that I cannot understand because I think the data is right. Apparently not.

    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-33-c302340a2d92> in <module>
    ----> 1 cerebro.run()
    
    ~/opt/anaconda3/envs/backtrader/lib/python3.8/site-packages/backtrader/cerebro.py in run(self, **kwargs)
       1125             # let's skip process "spawning"
       1126             for iterstrat in iterstrats:
    -> 1127                 runstrat = self.runstrategies(iterstrat)
       1128                 self.runstrats.append(runstrat)
       1129                 if self._dooptimize:
    
    ~/opt/anaconda3/envs/backtrader/lib/python3.8/site-packages/backtrader/cerebro.py in runstrategies(self, iterstrat, predata)
       1210                 data._start()
       1211                 if self._dopreload:
    -> 1212                     data.preload()
       1213 
       1214         for stratcls, sargs, skwargs in iterstrat:
    
    ~/opt/anaconda3/envs/backtrader/lib/python3.8/site-packages/backtrader/feed.py in preload(self)
        436 
        437     def preload(self):
    --> 438         while self.load():
        439             pass
        440 
    
    ~/opt/anaconda3/envs/backtrader/lib/python3.8/site-packages/backtrader/feed.py in load(self)
        477 
        478             if not self._fromstack(stash=True):
    --> 479                 _loadret = self._load()
        480                 if not _loadret:  # no bar use force to make sure in exactbars
        481                     # the pointer is undone this covers especially (but not
    
    ~/opt/anaconda3/envs/backtrader/lib/python3.8/site-packages/backtrader/feeds/pandafeed.py in _load(self)
        266 
        267         # convert to float via datetime and store it
    --> 268         dt = tstamp.to_pydatetime()
        269         dtnum = date2num(dt)
        270         self.lines.datetime[0] = dtnum
    
    AttributeError: 'str' object has no attribute 'to_pydatetime'
    

    What do I miss.

    Thanks for any hint.



  • Seems like the code thinks your date is a string. Maybe try explicitely putting it to datetime.

    df.index = pd.to_datetime(df.index)
    

Log in to reply
 

});