For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Indicator values before period kicks in



  • 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[0] data[1] and ema[0] ema[1] to the first values in the whole dataset
            self.ema[0] = self.data[0]
            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[0] = self.ema[-1] * ( 1.0 - self.smtfactor) + self.data[0] * self.smtfactor

  • administrators

    See Docs - Indicator Development

    The method to take into account:

    • nextstart

    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 next or not (It's not compulsory if you have already filled in the values)


  • administrators