Backtrader Community

    • 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/

    Problems with Pandas Datafeed

    General Discussion
    1
    1
    60
    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.
    • Marco Cetraro
      Marco Cetraro last edited by

      Hello,

      I have a python strategy that is working pretty well. I created my own backtest in python and it is showing good results. In order to reconfirm my results and obtain multiple ratios about the strategy I am trying to implement Backtrater.

      My problem is related to the pandas Datafeed. I am retrieving the data from Oanda, and I am only retrieving the basic columns to test the pandas Datafeed. Here the code:

      import backtrader.feeds as btfeed
      
      
      # =============================================================================
      # class PandasData(btfeed.PandasData):
      #     datetime=0,
      #     time=1,
      #     open=2,
      #     high=3,
      #     low=4,
      #     close=5,
      #     volume=6,
      #     openinterest=-1
      # 
      # =============================================================================
      
      class PandasData(bt.feeds.PandasData):
      #    lines = ('Time',)
          params = (
              ('datetime', 'Datetime'),
              ('open','Open'),
              ('high','High'),
              ('low','Low'),
              ('close','Close'),
              ('volume','Volume'),
              ('openinterest',None),
      #        ('Time', 'Time'),
          )
      
      
      #df = bt.feeds.PandasData(dataname=data)
      df2=PandasData(dataname=df)
      cerebro.adddata(df2)
      cerebro.run()
      code_text
      

      The error that I am getting is the following:

      class PandasData(bt.feeds.PandasData):
      #    lines = ('Time',)
          params = (
              ('datetime', 'Datetime'),
              ('open','Open'),
              ('high','High'),
              ('low','Low'),
              ('close','Close'),
              ('volume','Volume'),
              ('openinterest',None),
      #        ('Time', 'Time'),
          )
      
      
      #df = bt.feeds.PandasData(dataname=data)
      df2=PandasData(dataname=df)
      cerebro.adddata(df2)
      cerebro.run()
      Traceback (most recent call last):
      
        File "C:\Users\giaco\AppData\Local\Temp/ipykernel_14740/920099771.py", line 18, in <module>
          cerebro.run()
      
        File "C:\Users\giaco\anaconda3\lib\site-packages\backtrader\cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
      
        File "C:\Users\giaco\anaconda3\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies
          data.preload()
      
        File "C:\Users\giaco\anaconda3\lib\site-packages\backtrader\feed.py", line 438, in preload
          while self.load():
      
        File "C:\Users\giaco\anaconda3\lib\site-packages\backtrader\feed.py", line 479, in load
          _loadret = self._load()
      
        File "C:\Users\giaco\anaconda3\lib\site-packages\backtrader\feeds\pandafeed.py", line 268, in _load
          dt = tstamp.to_pydatetime()
      
      AttributeError: 'numpy.int64' object has no attribute 'to_pydatetime'code_text
      

      I know that the error is related to the DateTime column. Here the data:

      [4460 rows x 6 columns]
                                  Datetime     Open  ...    Close   Volume
      0     2022-03-01T12:00:00.000000000Z  1.84334  ...  1.84286  1.84334
      1     2022-03-01T12:15:00.000000000Z  1.84286  ...  1.84408  1.84286
      2     2022-03-01T12:30:00.000000000Z  1.84408  ...  1.84512  1.84408
      3     2022-03-01T12:45:00.000000000Z  1.84512  ...  1.84402  1.84512
      4     2022-03-01T13:00:00.000000000Z  1.84401  ...  1.84482  1.84401
                                   ...      ...  ...      ...      ...
      4455  2022-05-04T21:15:00.000000000Z  1.73933  ...  1.74036  1.73933
      4456  2022-05-04T21:30:00.000000000Z  1.74038  ...  1.74132  1.74038
      4457  2022-05-04T21:45:00.000000000Z  1.74114  ...  1.74106  1.74114
      4458  2022-05-04T22:00:00.000000000Z  1.74096  ...  1.74106  1.74096
      4459  2022-05-04T22:15:00.000000000Z  1.74110  ...  1.73914  1.74110code_text
      

      I have tried to extract the date and the time and format them from the Datetime column and use the Datafeed without any success either. I am using a 15 Minutes timeframe for the forex pair GBP_AUD. I have spent several hours searching the internet as welll as this community and I found some info that other users faced similar problems but they didn't get any answers.

      I will appreciate very much any help about this issue.

      Thank you very much.

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