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/

    A confused question about Pandas DataFeed

    General Code/Help
    2
    2
    88
    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.
    • L
      life last edited by

      Hi, maybe it is very basic, but I am glad that some one cloud give some tips about it:

      when I load the orcl-1995-2014.txt from github datas with command:

      class PandasData(feed.DataBase):
          params = (
              ('datetime', None), # here I use "None" to  indicate the  index of dataframe  is datetime
              ('open', 'Open'),
              ('high', 'High'),
              ('low', 'Low'),
              ('close', 'Close'),
              ('volume', 'Volume'),
              ('openinterest', None),
          )
      df = pd.read_csv('orcl-1995-2014.txt',
                                      skiprows=0,
                                      header=0,
                                      parse_dates=True,
                                      index_col='Date')
      data = bt.feeds.PandasData(dataname=df)
      # ... and go on ....
      cerebro.run( )
      

      in this case above, everything runs well, but if I make a little different from the example code, as below:

      class PandasData(feed.DataBase):
          params = (
              ('datetime', "Date"), # here I use "Date" to  indicate the "Date" column  is datetime
              ('open', 'Open'),
              ('high', 'High'),
              ('low', 'Low'),
              ('close', 'Close'),
              ('volume', 'Volume'),
              ('openinterest', None),
          )
      df = pd.read_csv('orcl-1995-2014.txt',
                                      skiprows=0,
                                      header=0,
                                      parse_dates=True) # remove index_col='Date'
      data = bt.feeds.PandasData(dataname=df)
      # ... and go on ....
      cerebro.run( ) 
      

      I make those little change follow the docs, but then, I got this error:

          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: 'int' object has no attribute 'to_pydatetime'
      

      It is very confused for me, It seems the docs maybe out of date or misunderstood?

      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @life last edited by

        @life said in A confused question about Pandas DataFeed:

        df = pd.read_csv('orcl-1995-2014.txt',
        skiprows=0,
        header=0,
        parse_dates=True) # remove index_col='Date'

        I the second instance tell pandas where to parse dates:

        dataframe = pd.read_csv(datapath, header=0, parse_dates=["Date"], index_col=None)
        
        data = bt.feeds.PandasData(
            dataname=dataframe,
            datetime="Date",
            open='Open',
            high='High',
            low='Low',
            close='Close',
            volume='Volume',
            openinterest=None
        
        )
        

        RunBacktest.com

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