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



  • 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



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


  • @dasch Nice, problem solved, thanks a lot!



  • @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?



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


Log in to reply
 

});