Handling multi assets with custom indicator
-
Hello, first of all, thank you for the great professional work!
I have created a custom indicator,hoping to run it on a list of symbols
class CLinearRegression(bt.Indicator): lines = ('slope','r_squared',) params = (('rank_period',90),) def __init__(self,bar_stream): self.addminperiod(self.p.rank_period) self.bar_stream = bar_stream def next(self): rank_period_quotes = self.bar_stream.get(size=self.p.rank_period) # Getting a slice x = np.arange(self.p.rank_period) slope, intercept, r_value, p_value, std_err = stats.linregress(x, rank_period_quotes) self.lines.slope[0] = slope self.lines.r_squared[0] = r_value**2
I have loaded a number of assets data and I'm trying to compute the custom indicator,in strategy init, for each asset in the datas list,like this
def __init__(self): self.lrs=dict() self.lrs = {data._name: CLinearRegression(bar_stream=data,rank_period=self.p.rank_period)for data in self.datas} ...
The indicators Next() runs OK for the first asset , but starts the second asset with the last bar, as if continuing from the first asset , not re-running the custom indicator on the new asset in the list
What am I doing wrong? -
Anyone?
Can someone explain why indicators next() method run for second feed starts at the end of the data and does not reiterate (like the first feed)? -
I think there is a bug in the system ( Or I'm doing something wrong),
ways to reproduce:- Create custom indicator( with init and next methods)
in next method - print the date of the last bar in data for the custom indicator - In Cerebro main - add a number (>1) of datas (for example , AAPL, XOM,etc) in the main method
- In strategy Init-initialize custom indicator on every data in datas
The BUG -> the data of the last bar is printed OK for the first data(1st ticker) but stays the same for the second,third data
- Create custom indicator( with init and next methods)
-
@Trade-Prophet said in Handling multi assets with custom indicator:
I think there is a bug in the system ( Or I'm doing something wrong),
Let me vouch for the second ... the BUG
@Trade-Prophet said in Handling multi assets with custom indicator:
def __init__(self,bar_stream): self.addminperiod(self.p.rank_period) self.bar_stream = bar_stream
You are trying to force the data feed (aka bar_stream in your code) as a parameter, thus breaking the platform conventions.
See:
- https://community.backtrader.com/topic/753/indicator-slicing/4
- https://www.backtrader.com/docu/concepts.html (Section: https://www.backtrader.com/docu/concepts.html#data-feeds-passing-them-around)
You don't need to capture the data feeds as parameters of the strategy or indicators. You simply pass them and they are collected in
self.datas
-
You probably want to have a look at how to develop an indicator
- Developing an Indicator (Blog): https://www.backtrader.com/blog/posts/2015-07-18-developing-an-indicator/developing-an-indicator.html
and/or this:
- Indicator Development (Docs): https://www.backtrader.com/docu/inddev.html
-
oops , mistake ..
-
@Alon-Horesh said in Handling multi assets with custom indicator:
How do I know in the custom indicator start() method which is the relevant data in datas?
Sorry but I do really fail to understand the question. Which
start()
method?May it be that you mean you wanted to do this:
self.lrs = {data._name: CLinearRegression(data, rank_period=self.p.rank_period)for data in self.datas}
You pass the data you want to linear-regress (to give it a fancy name)
-
Sorry about the mixup in the previous question ..
I'm probably missing something,
when inside the custom indicator next() method,assuming I have pre-loaded a number of datas (say n in number),which data (data[0],data[1],..data[n]) is the relevant data for the specific linear-regress custom indicator?
when the framework calls custom indicators next() method (which will be called n time) which data in datas is this specific instantiation of the indicators data ?
hope I made myself clearer .. -
Because you pass the specific data. See the answer above.
-
OK,
Thank you again for the high quality implementation and support -
Not mine.