Feed Pandas directly into Backtrader
-
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.
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 :))
-
As an update, I am now just using the default PandasDataFeeds class like so:
data = bt.feeds.PandasData(dataname=df) cerebro.adddata(data)
-
@dfg12 Could you share the top couple of rows from your dataframe?
-
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 -