Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    General Code/Help
    2
    3
    946
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      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[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
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        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)

        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators last edited by

          See this: https://www.backtrader.com/blog/posts/2018-01-27-recursive-indicators/recursive-indicator.html

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }