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

Pandas Dataframe issue with datetime index



  • Hello,
    I'm new to backtrader and I'm stuck trying to use a dataframe as feed data:

    My dataframe is like this:

    
                          close	high	low	open	quoteVolume	volume	weightedAverage
    date							
    2018-03-13 18:00:00	9056.879600	9201.690628	9009.000000	9115.026899	166.077552	1.508370e+06	9082.322181
    2018-03-13 20:00:00	9107.425800	9200.000000	8997.700335	9051.086300	139.533502	1.268252e+06	9089.226687
    2018-03-13 22:00:00	9121.007159	9245.000000	9098.370000	9098.370000	135.436130	1.241568e+06	9167.181357
    2018-03-14 00:00:00	9289.999998	9350.000000	9119.423431	9125.999996	155.469243	1.439687e+06	9260.271804
    2018-03-14 02:00:00	9250.000000	9328.695503	9210.000000	9289.999994	96.691300	8.970925e+05	9277.902688
    

    and I'm trying to use in this way:

    cerebro = bt.Cerebro()
    cerebro.broker.setcash(100000.0)
    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    # Create a Data Feed
    data = bt.feeds.PandasData(dataname=btc, timeframe=120,
                               openinterest=None)
    # Add the Data Feed to Cerebro
    cerebro.adddata(data)
    cerebro.run()
    
    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    # Plot the result
    cerebro.plot(style='bar')
    

    The error message is:

    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-48-d6cee4ffb289> in <module>()
    ----> 1 cerebro.run()
          2 
          3 print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
          4 # Plot the result
          5 cerebro.plot(style='bar')
    
    C:\Users\grafeer\AppData\Local\Continuum\Anaconda3\lib\site-packages\backtrader\cerebro.py in run(self, **kwargs)
       1140             # let's skip process "spawning"
       1141             for iterstrat in iterstrats:
    -> 1142                 runstrat = self.runstrategies(iterstrat)
       1143                 self.runstrats.append(runstrat)
       1144         else:
    
    C:\Users\grafeer\AppData\Local\Continuum\Anaconda3\lib\site-packages\backtrader\cerebro.py in runstrategies(self, iterstrat, predata)
       1222                 data._start()
       1223                 if self._dopreload:
    -> 1224                     data.preload()
       1225 
       1226         for stratcls, sargs, skwargs in iterstrat:
    
    C:\Users\grafeer\AppData\Local\Continuum\Anaconda3\lib\site-packages\backtrader\feed.py in preload(self)
        433 
        434     def preload(self):
    --> 435         while self.load():
        436             pass
        437 
    
    C:\Users\grafeer\AppData\Local\Continuum\Anaconda3\lib\site-packages\backtrader\feed.py in load(self)
        474 
        475             if not self._fromstack(stash=True):
    --> 476                 _loadret = self._load()
        477                 if not _loadret:  # no bar use force to make sure in exactbars
        478                     # the pointer is undone this covers especially (but not
    
    C:\Users\grafeer\AppData\Local\Continuum\Anaconda3\lib\site-packages\backtrader\feeds\pandafeed.py in _load(self)
        260 
        261         # convert to float via datetime and store it
    --> 262         dt = tstamp.to_pydatetime()
        263         dtnum = date2num(dt)
        264         self.lines.datetime[0] = dtnum
    
    AttributeError: 'int' object has no attribute 'to_pydatetime'
    

    Thanks for your help!



  • @ernegraf Forget about it! I'm running from Jupyter notebook and I need to restart the kernel and start from scratch. Just re-run the creation of data feed is not enough. Thanks


  • administrators

    @ernegraf said in Pandas Dataframe issue with datetime index:

    timeframe=120

    That's for sure wrong. You probably want to explain what your intention is, but timeframes have simbolic names: Days, Minutes.

    @ernegraf said in Pandas Dataframe issue with datetime index:

    --> 262         dt = tstamp.to_pydatetime()
        263         dtnum = date2num(dt)
        264         self.lines.datetime[0] = dtnum
    
    AttributeError: 'int' object has no attribute 'to_pydatetime'
    

    It seems that what you are giving to the PandasData feed is something with an int where an object should be.

    The conclusion, which may be completely wrong, is that you have redacted your code to hide some details and this is hiding the actual picture from us, because your dataframe has no single int field (or else there is a huge bug somewhere in that data feed)


  • administrators

    @ernegraf said in Pandas Dataframe issue with datetime index:

    and I need to restart the kernel and start from scratch. Just re-run the creation of data feed is not enough. Thanks

    Shooting yourself in the foot by running inside a broken environment is nothing we can cure over here (those environments hijack the initialization of Python and that has side effects. For some people this is not relevant, but as you see it is relevant and it for example destroys multiprocessing under Windows)



  • @backtrader this was an another failed attempt, now it's working and I don't need to include this parameter anymore. Thanks!