Indicator values before period kicks in
fivo last edited by
I want to know how to calculate initial values for an indicator that don't fall yet into the period. So for example if my period is 12 and my indicator uses recursive values how and where do I specify the first 11 eleven values of the indicator. Let's take the EMA as an example. I am aware that bt already implements a solution for the EMA, but lets just take it as an example.
class EMA(bt.Indicator): lines = ('ema',) params = (('period', 12), def __init__(self): self.addminperiod(self.p.period) self.smtfactor = 2.0 / (1.0 + self.p.period) #Where do I put this part # here I refer to data data and ema ema to the first values in the whole dataset self.ema = self.data for i in range(1, self.p.period): self.ema[i] = self.ema[i-1] * ( 1.0 - self.smtfactor) + self.data[i] * self.smtfactor def next(self): self.ema = self.ema[-1] * ( 1.0 - self.smtfactor) + self.data * self.smtfactor
The method to take into account:
It will be called once (and only once), when the minimum period is met. The default implementation does simply forward the call to
next. But in your case you can see the 1st non-recursive value there. Depending on your use case you may call
nextor not (It's not compulsory if you have already filled in the values)