Backtrader Community

    • 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/

    Compound value Calculation

    General Code/Help
    2
    11
    560
    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.
    • M
      mudassar031 last edited by

      I am translating Thinkorswim strategy into to backtrader need help translating TOS built in function in backtrader

      https://github.com/sureshja/ThinkOrSwim/blob/master/HeikinAshiSuperTrendStrategy.ts

      input aggrPd1 = AggregationPeriod.FOUR_HOURS;
      def haop1 = CompoundValue(1, (haop1[1] + hacl1[1]) / 2, (open(period = aggrPd1)[1] + close(period = aggrPd1)[1]) / 2);

      compound value reference
      https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue

      1 Reply Last reply Reply Quote 1
      • run-out
        run-out last edited by

        What have you tied so far?

        RunBacktest.com

        M 2 Replies Last reply Reply Quote 0
        • M
          mudassar031 last edited by

          import backtrader as bt
          class TOS(bt.Strategy):

          params = (('ST_Period', 7),
          ('ST_Coeff', 3),
          )
          def log(self, txt, dt=None):
          ''' Logging function for this strategy'''
          dt = dt or self.datas[0].datetime.datetime(0)
          print('%s, %s' % (dt.isoformat(), txt))

          def init(self):
          # Keep a reference to the "close" line in the data[0] dataseries

          self.dataopen = self.datas[0].open
          self.dataclose = self.datas[0].close
          self.datahigh = self.datas[0].high
          self.datalow = self.datas[0].low
          #calculate HAcl1
          self.hacl1 = (self.datas[0].open+self.datas[0].high+self.datas[0].low+self.datas[0].close)/4
          # calculate compoundvalue
          self.haop1 = ''
          #max value
          self.hahi1 = max(self.datas[0].open, max(self.hacl1[0],self.haop1[0]))
          #min value
          self.halo1 = min(self.datas[0].low, min(self.hacl1[0],self.haop1[0]))
          #avg of self.hahi1 and self.halo1
          self.hahl2_1 = (self.hahi1[0] + self.halo1[0]) / 2;
          self.hma = bt.indicators.SmoothedMovingAverage(self.datas[0], period=self.params.ST_Period)
          

          def next(self):
          # Simply log the closing price of the series from the reference

          self.log('Open: %.2f Close: %.2f High: %.2f Low: %.2f Hacl1: %.2f HMA: %.2f' % (self.dataopen[0],self.dataclose[0], self.datahigh[0], self.datalow[0],self.hacl1[0],self.hma[0]))
          
          1 Reply Last reply Reply Quote 0
          • M
            mudassar031 last edited by

            actually i am new to backtrader so compound value will be calculated using recursive call so i am finding way to do it def init(self): section
            here is the reference how compound value calculated if you please can help
            https://stackoverflow.com/questions/59003693/understanding-converting-thinkscripts-compoundvalue-function

            1 Reply Last reply Reply Quote 0
            • M
              mudassar031 last edited by

              import backtrader as bt
              class TOS(bt.Strategy):
              
              params = (('ST_Period', 7),
              ('ST_Coeff', 3),
              )
              def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.datetime(0)
              print('%s, %s' % (dt.isoformat(), txt))
              
              def init(self):
              def init(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataopen = self.datas[0].open
              self.dataclose = self.datas[0].close
              self.datahigh = self.datas[0].high
              self.datalow = self.datas[0].low
              #calculate HAcl1
              self.hacl1 = (self.datas[0].open+self.datas[0].high+self.datas[0].low+self.datas[0].close)/4
              # calculate compoundvalue
              self.haop1 = ''
              #max value
              self.hahi1 = max(self.datas[0].open, max(self.hacl1[0],self.haop1[0]))
              #min value
              self.halo1 = min(self.datas[0].low, min(self.hacl1[0],self.haop1[0]))
              #avg of self.hahi1 and self.halo1
              self.hahl2_1 = (self.hahi1[0] + self.halo1[0]) / 2;
              self.hma = bt.indicators.SmoothedMovingAverage(self.datas[0], period=self.params.ST_Period)
              
              def next(self):
              # Simply log the closing price of the series from the reference
              
              self.log('Open: %.2f Close: %.2f High: %.2f Low: %.2f Hacl1: %.2f HMA: %.2f' % (self.dataopen[0],self.dataclose[0], self.datahigh[0], self.datalow[0],self.hacl1[0],self.hma[0]))
              
              
              1 Reply Last reply Reply Quote 0
              • M
                mudassar031 @run-out last edited by

                @run-out
                i am also facing issue
                line 24, in init
                self.hahi1 = max(self.datas[0].open,max(self.hacl1[0],self.haop1[0]))
                return self.array[self.idx + ago]
                IndexError: array index out of range

                do i need to calculate hacl1 and haop1 in next function ?

                #calculate HAcl1
                self.hacl1 = (self.datas[0].open+self.datas[0].high+self.datas[0].low+self.datas[0].close)/4
                # calculate compoundvalue
                self.haop1 = self.haop1 = (self.datas[1].open+self.datas[1].high+self.datas[1].low+self.datas[1].close)/4
                #max value
                self.hahi1 = max(self.datas[0].open, max(self.hacl1[0],self.haop1[0]))
                
                1 Reply Last reply Reply Quote 0
                • M
                  mudassar031 @run-out last edited by

                  @run-out
                  i not able to find solution of thinkorswim compoundvalue built-in function
                  for now i have placed haop1 with

                  # calculate compoundvalue
                  self.haop1 = (self.datas[1].open+self.datas[1].high+self.datas[1].low+self.datas[1].close)/4
                  
                  1 Reply Last reply Reply Quote 0
                  • run-out
                    run-out last edited by

                    Could you tell me what's different about what you are doing and backtrader's built in indicator? Your formulas look almost the same.

                    https://www.backtrader.com/docu/indautoref/#heikinashi

                    RunBacktest.com

                    M 3 Replies Last reply Reply Quote 0
                    • M
                      mudassar031 @run-out last edited by

                      @run-out
                      Thanks for your help i am checking and update you if needed something

                      1 Reply Last reply Reply Quote 1
                      • M
                        mudassar031 @run-out last edited by

                        @run-out
                        indicator i am building is combination of supertrend and heikinashi
                        i have read this indicator
                        https://www.backtrader.com/docu/indautoref/#heikinashi
                        actually this line ha_open
                        ha_open = (ha_open(-1) + ha_close(-1)) / 2
                        i need to translate like this
                        ha_open = CompoundValue(1, (haop1[1] + hacl1[1]) / 2, (open(period = aggrPd1)[1] + close(period = aggrPd1)[1]) / 2);
                        compound value reference
                        https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue

                        1 Reply Last reply Reply Quote 0
                        • M
                          mudassar031 @run-out last edited by

                          @run-out
                          https://stackoverflow.com/questions/59003693/understanding-converting-thinkscripts-compoundvalue-function

                          i need to convert this in backtrader and want to calculate ha_open like this

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