[Python] How to access the previous value in Indie without using negative indices?

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am working with Indie and trying to access the previous value of a time series (e.g., self.close).

    However, Indie does not support negative indices like Python, so trying to use self.close[-1] throws an error:


    Error: Series offset must be non-negative, got offset=-1
    ​
    An example that does NOT work:


    # indie:lang_version = 5
    import math
    from indie import indicator, param, plot, color
    from indie.algorithms import Sma, Highest, Lowest

    @indicator('Volume Surge Reversal', overlay_main_pane=True)
    @param.int('vol_length', default=10, min=1, title='Volume MA Length') # Уменьшен период
    @param.float('threshold', default=1.5, min=1.0, title='Volume Spike Multiplier') # Уменьшен порог
    @param.int('lookback', default=5, min=1, title='Lookback for High/Low')
    @plot.marker(color=color.GREEN, title="Buy Signal")
    @plot.marker(color=color.RED, title="Sell Signal")
    def Main(self, vol_length, threshold, lookback):
    vol_ma = Sma.new(self.volume, vol_length)
    vol_spike = self.volume[0] > threshold * vol_ma[0]

    recent_high = Highest.new(self.high, lookback)[0]
    recent_low = Lowest.new(self.low, lookback)[0]

    price_high = self.close[0] >= (recent_high * 0.998)
    price_low = self.close[0] <= (recent_low * 1.002)

    buy_signal = vol_spike and price_low
    sell_signal = vol_spike and price_high

    return (
    self.close[0] if buy_signal else self.close[-1], # It doesn't work this way of using
    self.close[0] if sell_signal else self.close[-1]
    )

    What I want to achieve:


    I need a way to access the previous value of self.close (equivalent to self.close[-1] in Python).

    Are there built-in functions for shifting series, or should I use a mutable series (MutSeriesF) to store past values?

    Continue reading...

Compartilhe esta Página