@run-out
Thanks for your help. I found out that I was not using the enumerate for loop in init function so that it can run for each data feed
Best posts made by btr
-
RE: Multiple assets with custom pandas dataframe
-
RE: Programmatically extending a datafeed
@run-out I needed a break and coffee. It was silly
Latest posts made by btr
-
RE: Programmatically extending a datafeed
@run-out I needed a break and coffee. It was silly
-
RE: Programmatically extending a datafeed
@backtrader @tmorgan4 Hey, I am trying to understand this and the question is about programatically extending the data feed when you do not know the additional column names of df. After the discussion, @tmorgan4 posted a code where ('optix_close', 'optix_pess', 'optix_opt',) lines are already known. How is this created programatically? Am I missing something?
thanks you in advance for the response
-
Dynamically extending the dataframe
Hello,
I am currently extending the data feeds using PandasData feeds and have multiple data feeds going into cerebro.
class PandasData(bt.feeds.PandasData): lines = ('abc','xyz') params = ( ('abc', 'abc'), ('xyz','xyz'), )
for df in datas: data=PandasData(dataname=df) cerebro.adddata(data, name='example')
and using these lines in init function as:
for i, d in enumerate(self.datas): self.inds[d] = dict() self.inds[d]['abc'] = d.abc self.inds[d]['xyz'] = d.xyz
Now, what if I don't know the name of extra columns 'abc' and 'xyz' that the pandas dataframe would have and would like to extend the dataframe for those data feeds dynamically.
I saw this topic, what I don't understand here is that they already know these 'optix_close', 'optix_pess', 'optix_opt' column names. How is this programatically extending datafeed?
Thanks in advance
-
RE: Multiple assets with custom pandas dataframe
@run-out
Thanks for your help. I found out that I was not using the enumerate for loop in init function so that it can run for each data feed -
RE: Multiple assets with custom pandas dataframe
@run-out
Yes, there's misunderstanding here. p/e is just an example. Take a simple moving average strategy. I have a dataframe that already has sma crossover calculated. I am sending that dataframe to custom pandas data function and creating a line. Like this:class PandasData(bt.feeds.PandasData): lines = ('crossover') params = ( ('crossover', 'crossover'), )
and feeding it to cerebro
df=PandasData(dataname=current_df) cerebro.adddata(df, name=current_df['symbol'][0])
Now, I use this in next as following:
if self.datas[0].crossover > 0: buy() if self.datas[0].crossover < 0: sell()
This works perfectly fine. Now I am extending my strategy so that it can run over multiple assets as the same time. I am sending single dataframes to single data feed like:
resp is a list of dataframe where each has crossover already calculatedfor df in resp: data=PandasData(dataname=df) cerebro.adddata(df, name='data0') #name here would be dynamic so I can get the name in next and find which asset/dataframe's order is placed cerebro.run()
and then in next:
for i, d in enumerate(self.datas): dt, dn = self.datas[0].datetime.date(), d._name pos = self.getposition(d).size if not pos:
In order to do this in next, I would need multiple lines of crossover from custom pandas data function, one for each asset/datafeed/dataframe
-
RE: Multiple assets with custom pandas dataframe
@run-out
I am trying to follow this https://backtest-rookies.com/2017/08/22/backtrader-multiple-data-feeds-indicators/ but instead of using backtrader indicators, I want to use the lines create from custom dataframe in next. I was able to implement this with single data feed. Creating the lines from custom pandas data and using that line in next.I am having issues extending the same with multiple data feeds
-
RE: Multiple assets with custom pandas dataframe
@run-out
I am able to do this. I have custom loader that has extra columns and I am feeding it to cerebro and using it next. The issue I am facing is with applying the same logic to multiple datafeeds.Multiple data feeds means there would be pe_largcap and pe_international for all data feeds. So, if the cerebro is feed with 2 assets data, it should have 4 lines. and somehow I need to check those 4 lines in next depending on the data feed.
-
Multiple assets with custom pandas dataframe
Hello,
I am trying to run strategy on multiple assets that have custom pandas dataframe.
class PandasData(bt.feeds.PandasData): lines = ('pe') params = ( ('pe', 'pe'), )
There is a list of dataframes that has information for different assets
for data in list: df=PandasData(dataname=data) cerebro.adddata(df, name='abc') cerebro.run()
I am able to use this for single asset in next() using
for i, d in enumerate(self.datas): dt, dn = self.datas[0].datetime.date(0)), d._name pos = self.getposition(d).size
How can I create multiple 'pe' lines for multiple assets and use it in the next()?