Adding lines and shifts from indicators



  • I have this scenario:

    • I´ve defined an indicator (let´s say RSI) at the init function : self.rsi = bt.indicators.RSI(...)
    • Then I need to add that line with it´s previous values, or in other words the RSI shifted by 1

    So if RSI[0] = 80 and RSI[-1] = 50, the new line[0] would be 80+50..

    Is that possible? I´m trying to create a Fisher transformation with some changes but can´t know how to shift lines in order to sum them as an init definition..


  • administrators

    Shifting is implemented with the (x) operators in lines objects.

    See: Docs - Concepts and look for delayed. The use of the term delayed was meant to indicate that looking into the future is not possible. But it is possible for indicators to project values into the future (like Ichimoku does), although it is seldom used.

    Basically:

    newline = self.rsi + self.rsi(-1)
    

    Notice how the first term doesn't use the (x) notation and with it it simply takes the current value. One could of course also do it formally as:

    newline = self.rsi(0) + self.rsi(-1)
    

    which is functionally 100% equivalent but not 100% memory equivalent because self.rsi(0) will create an extra buffer for the copies of itself.

    RSI has a single line, so there is not much need to address the specific lines and by simply using the indicator itself the 1st line in the indicator is automatically used.

    But for indicators with multiple lines like a MACD, one can also do things like:

    newline = self.macd + self.macd(-1)  # 1st line
    newline = self.macd.lines.macd + self.macd.lines.macd(-1)  # the 1st line is also called macd
    newline = self.macd + self.macd.lines.signal(-1)  # sum of present 1st line + delayed line "signal"
    

Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.