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

Indicator - High since most recent Low

  • Hi,
    I am relatively new to python and backtrader, and am struggling to create an addition to the DMI indicator where I track the recent High or Low ADX, and reset the Low after a new high is established and then reset the high once a new low is in place rather than take the high and low for a particular period.

    I can find the low or high for a period as below

    self.dmi = btind.DirectionalMovement(period = self.params.fastdays)
    self.ADXhi = bt.indicators.Highest(self.dmi.adx, period = self.params.fastdays)
    self.ADXlo = bt.indicators.Lowest(self.dmi.adx, period = self.params.fastdays)

    but how can I make it more dynamic so that if for example the ADX continues to set new highs the ADX low does not change till the high is in place?

  • administrators

    You need custom logic in next. Those Highest and Lowest do simply record what has happened during the last fastdays but have no relationship to each other.

    Your custom indicator defines those in __init__ but you then need to keep extra variables and info which you manipulate during next. When you record a new high (because self.ADXhi changes), you can the manipulate the value of the low.

    Docs - Indicator Development

  • Thanks that worked .... I had an issue with kicking off the calc at the right time as this was an indicator on another indicator which is an average of another average, so had to find a way to delay the start. but the below is not very pretty but it worked. I think it might be better in a new indicator class but I kept getting a variety of errors with that and my python isn't good enough. Thanks for the help.

    def __init__(self):
        #keep a reference to the close
        self.dataclose = self.datas[0].close
        self.dataopen = self.datas[0].open
        self.datahi = self.datas[0].high
        self.datalo = self.datas[0].low
        self.datadate = self.datas[0].datetime
        self.dmi = btind.DirectionalMovement(period = self.params.fastdays)
        self.dyn_adx_hi = [0,]
        self.dyn_adx_lo = [0,]
    def next(self):
        if len(self.dmi.adx) > (self.params.fastdays)*3+1 and len(self.dmi.adx) <=(self.params.fastdays)*3+2:
            self.dyn_adx_hi[0] = self.dmi.adx[-1]
            self.dyn_adx_lo[0] = self.dmi.adx[-1]
        if len(self.dmi.adx) > (self.params.fastdays)*3+2:
            if self.dmi.adx[0] > self.dyn_adx_hi[-1] or self.dmi.adx[0] >= self.dmi.adx[-1]:
                self.dyn_adx_hi[0] = self.dmi.adx[0]
                self.dyn_adx_lo[0] = self.dyn_adx_lo[-1]
            elif self.dmi.adx[0] < self.dyn_adx_lo[-1] or self.dmi.adx[0] < self.dmi.adx[-1]:
                self.dyn_adx_lo[0] = self.dmi.adx[0]
                self.dyn_adx_hi[0] = self.dyn_adx_hi[-1]