[Python] How to resolve the error: "cannot initialize variable with None; optionals are...

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

  1. Stack

    Stack Membro Participativo

    I'm using TakeProfit's Indie Script Indicator. I'm getting an error when trying to initialize a variable to None.

    Here's a simplified version of the code:

    # indie:lang_version = 5
    from math import nan, isnan
    from indie import indicator, param, plot, color, MutSeriesF
    from indie.algorithms import Atr

    @indicator('SuperTrend', overlay_main_pane=True)
    @param.float('atr_mult', default=1.0, min=0.1, max=10.0, title='ATR Multiplier')
    @param.int('n_atr', default=4, min=1, title='ATR Length')
    @param.str('ma_type', default='HULL', options=['SMA', 'EMA', 'WMA', 'RMA', 'VWMA', 'HULL'], title='MA Type')
    @param.bool('paint_bars', default=True, title='Color Bars')
    @plot.line(title='SuperTrend')
    @plot.bar_color()
    def Main(self, atr_mult, n_atr, ma_type, paint_bars):
    hl2 = (self.high[0] + self.low[0]) / 2
    atr = Atr.new(n_atr, ma_type)

    up = MutSeriesF.new(hl2 + atr_mult * atr[0])
    dn = MutSeriesF.new(hl2 - atr_mult * atr[0])
    st = MutSeriesF.new(nan)

    if isnan(st[1]):
    st[0] = up[0]
    else:
    if self.close[0] < st[1]:
    st[0] = up[0]
    else:
    st[0] = dn[0]

    bar_col = None
    if paint_bars:
    bar_col = color.RED if self.close[0] < st[0] else color.GREEN

    return st[0], bar_col



    But Indie compiler gives this error:


    Error: cannot initialize bar_col with None; in Indie language optionals are explicit, so to make this assignment, you need to declare bar_col as indie.Optional[...]
    ​

    How should I properly initialize a variable that can be either a color or None in TakeProfit's Indie Script language?

    Continue reading...

Compartilhe esta Página