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/

    Feed Pandas directly into Backtrader

    General Code/Help
    2
    5
    189
    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.
    • D
      dfg12 last edited by

      Hi,

      I'd like to draw data from my database and feed it into backtrader via a Pandas dataframe, without the CSV intermediate step.

      When I print my Pandas df, I get normal printed results.

      Screenshot 2021-08-10 at 09.53.19.png

      Then I feed my df into my custom PandasData class

      class PandasData(bt.feed.DataBase):
          lines = ('datetime', 'unix', 'open', 'high', 'low', 'close', 'volume')
      
          params = (
              ('datetime', None),
              ('unix', 'unix'),
              ('open', 'open'),
              ('high', 'high'),
              ('low', 'low'),
              ('close', 'close'),
              ('volume', 'volume')
          )
      

      After which I add the data to bt:

         feed = PandasData(dataname=df)
      
          print('feed: ---', feed)
      
          cerebro.adddata(feed)
      

      Once I add the strategy, however, I cannot access the data and cannot print it.

      And get the following error message:

      IndexError: array index out of range
      

      Thank you in advance :))

      D 1 Reply Last reply Reply Quote 0
      • D
        dfg12 @dfg12 last edited by

        As an update, I am now just using the default PandasDataFeeds class like so:

            data = bt.feeds.PandasData(dataname=df)
        
            cerebro.adddata(data)
        
        run-out 1 Reply Last reply Reply Quote 0
        • run-out
          run-out @dfg12 last edited by

          @dfg12 Could you share the top couple of rows from your dataframe?

          RunBacktest.com

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

            @run-out

            Sure, can do!! :) Thanks for your response

            datetime unix symbol open high low close volume exchange
            0 2021-01-01 00:00:00 1609459200000 BTC/USDT 28923.63 28961.66 28913.12 28961.66 27.457032 binance
            1 2021-01-01 00:01:00 1609459260000 BTC/USDT 28961.67 29017.50 28961.01 29009.91 58.477501 binance
            2 2021-01-01 00:02:00 1609459320000 BTC/USDT 29009.54 29016.71 28973.58 28989.30 42.470329 binance
            3 2021-01-01 00:03:00 1609459380000 BTC/USDT 28989.68 28999.85 28972.33 28982.69 30.360677 binance
            4 2021-01-01 00:04:00 1609459440000 BTC/USDT 28982.67 28995.93 28971.80 28975.65 24.124339 binance

            Screenshot 2021-08-11 at 13.03.26.png

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

              @dfg12 Try making sure datetime is a datetime object and not string. Move this to the index. You should be ok, but may need to identify the columns as per the docs.

              RunBacktest.com

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