Normalize Indicator over several stocks while creating it
I would like to normalize the momentum indicator, i.e. the values of all stocks should range between 0 and 1. I fear it is not possible to do that within the indicator since the data of all stocks is needed to normalize it, isn't it?
lines = ('trend',)
params = (('period', 90),)
def __init__(self): self.addminperiod(self.params.period) def next(self): returns = np.log(self.data.get(size=self.p.period)) x = np.arange(len(returns)) slope, _, rvalue, _, _ = linregress(x, returns) annualized = (1 + slope) ** 252 self.lines.trend = annualized * (rvalue ** 2)
So I thought the most straight forward way would be to divide every self.lines.trend by the maximum self.lines.trend of all stocks. But I don't know how to access that while creating the indicator...
Any ideas for an easy implementation?
Thanks in advance!
run-out last edited by
@jonny8 This post might prove useful. https://community.backtrader.com/topic/308/how-do-i-normalize-volume-on-resampled-data