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/

    Pandas data frame Error

    General Code/Help
    3
    5
    2677
    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.
    • S
      siavash last edited by

      I'm going to do back testing using panda data frame. This is my code:

      import backtrader as bt
      import pandas as pd
      
      df = pd.read_csv('EURUSD-h1.csv',skiprows=1,
                                      parse_dates=True,
                                      index_col=0)
      train_size = int(len(df) * 0.66)
      train, test = df[0:train_size], df[train_size:len(df)]
      print(train)
      
      data0 =bt.feeds.PandasData(dataname=train,timeframe='Minutes',openinterest=None)
      cerebro = bt.Cerebro(stdstats=False)
      cerebro.addstrategy(bt.Strategy)
      cerebro.adddata(data0)
      
      cerebro.broker.setcash(100000.0)
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      

      And this is my data :0_1534025271866_Untitled.png

      But there is error at the output.

      B 1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        Show the error message.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • S
          siavash last edited by

          this is the error:

          Traceback (most recent call last):
          
            File "<ipython-input-1-f7930e186e47>", line 1, in <module>
              runfile('C:/python/spyder/untitled6.py', wdir='C:/python/spyder')
          
            File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
              execfile(filename, namespace)
          
            File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
              exec(compile(f.read(), filename, 'exec'), namespace)
          
            File "C:/python/spyder/untitled6.py", line 18, in <module>
              cerebro.run()
          
            File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\cerebro.py", line 1127, in run
              runstrat = self.runstrategies(iterstrat)
          
            File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies
              data.preload()
          
            File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\feed.py", line 435, in preload
              while self.load():
          
            File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\feed.py", line 476, in load
              _loadret = self._load()
          
            File "c:\users\sorosh\appdata\local\programs\python\python37-32\lib\site-packages\backtrader\feeds\pandafeed.py", line 268, in _load
              dt = tstamp.to_pydatetime()
          
          AttributeError: 'str' object has no attribute 'to_pydatetime'
          
          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators @siavash last edited by

            @siavash said in Pandas data frame Error:

            df = pd.read_csv('EURUSD-h1.csv',skiprows=1,
                                            parse_dates=True,
                                            index_col=0)
            

            Content of column 0:

            EURUSD
            

            Error

            @siavash said in Pandas data frame Error:

            AttributeError: 'str' object has no attribute 'to_pydatetime'
            

            It seems to be a straightforward thing. In any case you probably want to read how PandasData work:

            • Docs - Pandas DataFeed Example
            • Docs - Data Feeds Reference
            1 Reply Last reply Reply Quote 0
            • S
              siavash last edited by

              Thanks. It was helpful

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors