[Python] Indie indicator loads, but plotting randomly fails

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

  1. Stack

    Stack Membro Participativo

    I'm trying to create the Oscillator based on Indie Script. The logic seems fine, and the indicator compiles successfully in Indie. However, at runtime, I get a math domain error with no line number or stack trace With some actives. For Examples funds -> SPY

    [​IMG]

    I've checked that all values are initialized properly and that the algorithm matches the expected pattern. The error only happens occasionally.

    What could be causing this issue in Indie, and how can I avoid it?

    Full code:

    # indie:lang_version = 5
    from indie import indicator, param, plot, color, MutSeriesF
    from indie.algorithms import Ema
    from math import sqrt

    @indicator('Andean Oscillator')
    @param.int('length', default=50)
    @param.int('sig_length', default=9)
    @plot.line('bull_line', title='Bullish Component', color=color.GREEN, line_width=2)
    @plot.line('bear_line', title='Bearish Component', color=color.RED, line_width=2)
    @plot.line(title='Signal', color=color.YELLOW, line_width=2)
    @plot.columns('bull_hist', title='Bull Histogram', color=color.rgba(204, 255, 0, 0.3), base_value=0.0)
    @plot.columns('bear_hist', title='Bear Histogram', color=color.rgba(246, 45, 174, 0.3), base_value=0.0)
    def Main(self, length, sig_length):
    alpha = 2 / (length + 1)

    up1 = MutSeriesF.new(self.close[0])
    up2 = MutSeriesF.new(self.close[0] * self.close[0])
    dn1 = MutSeriesF.new(self.close[0])
    dn2 = MutSeriesF.new(self.close[0] * self.close[0])

    prev_up1 = up1[1]
    prev_up2 = up2[1]
    prev_dn1 = dn1[1]
    prev_dn2 = dn2[1]

    up1[0] = max(self.close[0], self.open[0], prev_up1 - (prev_up1 - self.close[0]) * alpha)
    up2[0] = max(self.close[0]**2, self.open[0]**2, prev_up2 - (prev_up2 - self.close[0]**2) * alpha)

    dn1[0] = min(self.close[0], self.open[0], prev_dn1 + (self.close[0] - prev_dn1) * alpha)
    dn2[0] = min(self.close[0]**2, self.open[0]**2, prev_dn2 + (self.close[0]**2 - prev_dn2) * alpha)

    bull = sqrt(dn2[0] - dn1[0] * dn1[0])
    bear = sqrt(up2[0] - up1[0] * up1[0])

    signal_input = MutSeriesF.new(max(bull, bear))
    signal = Ema.new(signal_input, sig_length)[0]

    amp = abs(bull - bear)
    hist_green = amp if bull > bear else 0.0
    hist_red = amp if bear > bull else 0.0

    return bull, bear, signal, hist_green, hist_red

    Continue reading...

Compartilhe esta Página