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



  • 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



  • What have you tied so far?



  • 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]))


  • 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



  • 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]))
    
    


  • @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]))
    


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


  • 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


Log in to reply
 

});