How to Feed Backtrader Alternative Data
-
Hi there,
I am new to backtrader, it seems a powerful platform!
I have a pre-written trading algorithm which modifies the pandas dataframes for many stocks, adding additional columns to the standard OHLCV dataframe. Many new columns are added for the time series to calculate different characteristics.
What is the best way to bring these dataframes, with my columns, into Back Trader? Can I just use the standard "data= bt.feeds.PandasData(..."?
How can I then call these columns in "Def next()" so that I can say things like "if Column X > Column Y Then BUY BUY BUY!!" ...i.e. self.order = self.buy().
Appreciate your help! Hopefully I don't need to re-write my code and can just pull the dataframes into Backtrader!
Thank you!
cwse -
Hi,
seems to me that you could use GenericCSVData feature described here:
https://www.backtrader.com/docu/datafeed.html#genericcsvdata -
Each and every lines object in the platform can get the
lines
definition extended.GenericCSVData
can be extended: Docs - Extending a Data FeedAnd the same principles apply to
PandasData
. Due to the nature of a dataframe and to look for things in the dataframe by name, the class features also adatafields
atttribute which names the fields (columns in the dataframe).You should also add the new fields to the
params
with the expected position or expect autodetection by means of the name. -
@administrators @backtrader is there a way to do it without importing CSVs? I have many dataframes and it would be more efficient to load the dataframes direct.
Thanks!
-
Yes. That was the intention of the answer.
You have to add the additional
lines
. The Docs - Extending a Data Feed gives you an example of how to add an additional line.As stated above and due to the nature of a
DataFrame
, thePandasData
class carries adatafields
attribute, to which you add the extra names (this aids in mapping column names to actual fields)And the parameters tells where to find them (or to try to find them).
There was an issue some time ago where this was discussed: https://github.com/mementum/backtrader/issues/65
-
Awesome, got it thank you!
-
Hi, I am using the "Extending teh pandasDataDeed #65" approach with the following class for two new columns at the right hand side of OHLCV:
class CustomDataLoader(btfeeds.PandasData): lines = ('TOTAL_SCORE','Beta',) params = (('TOTAL_SCORE',-1), ('Beta',-1) ) datafields = btfeeds.PandasData.datafields + (['TOTAL_SCORE','Beta'])
This works file. But are there any issues that I DONT have an "Openinterest" column in my data source? I am not seeing any errrors but worried that backtrader may be taking the "Total Score" column as the Openinterest?
Thanks!
-
If in doubt disable that column by passing
None
for it.See the rereference of
PandasData
here: -
So is this correct??
class CustomDataLoader(btfeeds.PandasData): lines = ('TOTAL_SCORE','Beta',) params = (('Open_Interest", None), ('TOTAL_SCORE',-1), ('Beta',-1) ) datafields = btfeeds.PandasData.datafields + (['TOTAL_SCORE','Beta'])
-
@cwse
openinterest
is the name in the lines hierarchy of backtrader. -
Meaning?? Is my code correct? If not perhaps you can kindly advise how it should be?
-
You said you wanted to ignore
openinterest
, but your code containsOpen_Interest
, which is obviously not the same. And that's what the answer pointed out. -
@backtrader said in How to Feed Backtrader Alternative Data:
code contains Open_Interest, which is obviously not the same. And that's what the answer pointed out.
Yes, I do want to ignore it. You said to pass "None" so I am trying to!! I have obviously done it wrong, so please correct me! Thanks, CWSE
-
@cwse The name of the line is
openinterest
and notOpen_Interest
. -
perfect, so this is it:
class CustomDataLoader(btfeeds.PandasData): lines = ('TOTAL_SCORE','Beta',) params = ( ('openinterest',None), #None= column not present ('TOTAL_SCORE',-1), ('Beta',-1) ) datafields = btfeeds.PandasData.datafields + (['TOTAL_SCORE','Beta'])