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/

    Make calcul with indicator

    Indicators/Strategies/Analyzers
    4
    8
    2100
    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.
    • G
      GwadaMan last edited by

      Hi BackTrader !!!
      How are you ?
      Could you explain to me how can I use the line values to use them in math formuler ?

      class MyStrategy(bt.Strategy):
      
      params = dict(period=20)
      def __init__(self):
      
          self.movav = btind.SimpleMovingAverage(self.data, period=self.p.period)
          self.test = ((self.movav.lines.sma(0) - self.movav.lines.sma(-2)) / 2
      
      def next(self):
          if self.movav.lines.sma[0] > self.data.lines.close[0]:
              print('Simple Moving Average is greater than the closing price')
      

      Regards

      1 Reply Last reply Reply Quote 0
      • C
        cgi1 last edited by

        You are actually doing it.

        self.movav.lines.sma[0] > self.data.lines.close[0] is an mathematic operation of comparing the current bars sma-value over current bars close value. Its a>b.

        1 Reply Last reply Reply Quote 0
        • G
          GwadaMan last edited by

          @cgi1 said in Make calcul with indicator:

          a

          Hi cgi1
          thanks for your help.

          I would like to do something like this :

          def __init__(self):
          
              self.sma0 = btind.MovAv.SMA(self.data, period=12)
          
              self.sma1 = ((self.sma0.lines.sma(0) - self.sma0.lines.sma(1)) / 2)
          
          
          def next(self):
              print(" sma1: %s" %( self.sma1[0]))
          

          but the result I 've is "nan"

          sma1: nan
          sma1: nan
          sma1: nan
          sma1: nan
          sma1: nan

          Curtis Miller B 2 Replies Last reply Reply Quote 0
          • C
            cgi1 last edited by

            The value is nan if you access a value which could not be calculated from the indicator. (e.g. the first 199 bars of an EMA200).

            G 1 Reply Last reply Reply Quote 0
            • G
              GwadaMan @cgi1 last edited by

              I would like to play with the indicator. @backtrader How can I do it ?
              I'm have a lots of fun working with this plateforme.

              Regards,

              1 Reply Last reply Reply Quote 0
              • Curtis Miller
                Curtis Miller @GwadaMan last edited by

                @GwadaMan Replace self.sma0.lines.sma(1) with self.sma0.lines.sma(-1). You think you're referencing the past, but positive numbers reference the future, not the past, and strategies should never reference the future, for obvious reasons. This is probably why you are getting nan. Make that change and see if it fixes the problem.

                Blog posts: Walk Forward Analysis Demonstration, Stock Trading Analytics and Optimization

                Books/Video Courses: Unpacking NumPy and Pandas

                1 Reply Last reply Reply Quote 0
                • G
                  GwadaMan last edited by

                  Hi Curtis,

                  Thank you very much for the correction. It works !!!
                  We can close this case.
                  Regards,

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

                    @GwadaMan said in Make calcul with indicator:

                    def __init__(self):
                    
                        self.sma0 = btind.MovAv.SMA(self.data, period=12)
                    
                        self.sma1 = ((self.sma0.lines.sma(0) - self.sma0.lines.sma(1)) / 2)
                    

                    A couple of comments for potential simplifications (some may for sure see it as more complex)

                        self.sma0 = btind.MovAv.SMA(self.data, period=12)
                    

                    This can be usually simplified to:

                        self.sma0 = btind.MovAv.SMA(period=12)
                    

                    When no specific data is specified, the 1st data of wherever the calculation is being made is implied.

                        self.sma1 = ((self.sma0.lines.sma(0) - self.sma0.lines.sma(-1)) / 2)
                    

                    can be also be simplified to:

                        self.sma1 = ((self.sma0.lines.sma - self.sma0.lines.sma(-1)) / 2)
                    

                    where there is no need to specify the (0), because the reference is implicit in the operation

                    Although @ntguardian is right and (1) points to the future, this can be used for real reasons. For example to push values from the future into the past as it is done in the Ichimoku indicator. For example:

                            ...
                            self.l.chikou_span = self.data.close(self.p.chikou)
                            ...
                    

                    This says that the current value of chikou_span has to be that of the future close (self.p.chikou has a default positive value of 26)

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors