[Python] Indie.Var[int].new can only be called from specific contexts

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

  1. Stack

    Stack Membro Participativo

    I am trying to implement the Elder’s Force Index in Indie, but I’m getting an error when I call indie.Var[int].new() in the global scope.

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

    # ERROR: Using Var[int].new() in the global scope
    trend_counter = Var[int].new(0)

    @indicator("Elder's Force Index (FI)", overlay_main_pane=False)
    @param.int('length', default=13, min=1, title='EMA Length')
    @plot.line(id='#plot_0')
    def Main(self, length):
    # Create a mutable time series for the Force Index
    fi_series = MutSeriesF.new((self.close[0] - self.close[1]) * self.volume[0])

    smoothed_fi = Ema.new(fi_series, length)[0]
    fi_color = color.GREEN if smoothed_fi > 0 else color.RED

    return plot.Line(smoothed_fi, color=fi_color)


    Error:


    Error: 6:16 indie.Var[int].new is a syntactic sugar function in Indie and can only be called from a function decorated with @indie.algorithm, @indie.sec_context or @indie.indicator or from non-__init__ method of a class inherited from indie.MainContext, indie.SecContext or indie.Algorithm
    ​

    I don’t know what the error means, but it seems like Var\[int\].new() can’t be used where I put it. It mentions something about specific functions or contexts, but I’m not sure what that means or how to fix my code.

    Where do I put Var\[int\] so that this error doesn’t happen? What does the error mean by “specific contexts” and how do I set that up?

    Continue reading...

Compartilhe esta Página