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/

    Incorrect `_timeframe` after loading Pandas dataframe to a data feed

    General Discussion
    2
    5
    144
    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
      soulmachine last edited by

      I've built 1-minute time bars from tick-by-tick trading data of Bitcoin, which is loaded into a Pandas dataframe first, then converted to a data feed using the following code:

      class CryptoPandasData(bt.feeds.PandasData):
          lines = ('mean', 'median', 'volume_sell', 'volume_buy',
                   'volume_quote', 'volume_quote_sell', 'volume_quote_buy',
                   'count', 'count_sell', 'count_buy')
          params = (
              ('datetime', 'timestamp'),
              ('openinterest',None),
              ('mean', -1),
              ('median', -1),
              ('volume_sell', -1),
              ('volume_buy', -1),
              ('volume_quote', -1),
              ('volume_quote_sell', -1),
              ('volume_quote_buy', -1),
              ('count', -1),
              ('count_sell', -1),
              ('count_buy', -1),
          )
      

      Then print data_feed._timeframe:

      bt.TimeFrame.Names[data_feed._timeframe]
      'Days'
      

      We can see that the timeframe of the data feed is bt.TimeFrame.Days, which is apparently not correct, because my time bars are precisely 1-minute bars.

      Any ideas? Thanks!

      Full Jupyter notebook: https://github.com/soulmachine/crypto-notebooks/blob/master/backtest/pandas-dataframe-as-a-backtrader-data-feed.ipynb

      1 Reply Last reply Reply Quote 0
      • D
        dasch last edited by

        When you create the datafeed, you need to specify the timeframe and compression your data is using. Backtrader will not identify it by itself.

        So when you do:

         data_feed = CryptoPandasData(dataname=time_bars)
        

        you would do it that way:

         data_feed = CryptoPandasData(dataname=time_bars, timeframe=bt.TimeFrame.Minutes, compression=1)
        
        S 2 Replies Last reply Reply Quote 2
        • S
          soulmachine @dasch last edited by

          @dasch Nice, problem solved, thanks a lot!

          1 Reply Last reply Reply Quote 0
          • S
            soulmachine @dasch last edited by

            @dasch What if my bars are volume bars(from Chapter 2 "Financial Data Structures" of the book "Advances in Financial Machine Learning" by Marcos Prado), which don't have a fixed timeframe, use timeframe=bt.TimeFrame.Ticks?

            1 Reply Last reply Reply Quote 1
            • D
              dasch last edited by

              I am not really sure, since I never used such a usecase. Just give it a try.

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