[Python] Variable Initialization Error (indie.Var[int]) in Indicator Context

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

  1. Stack

    Stack Membro Participativo

    I'm developing indicator in Indie v4, and I'm encountering the following error during the initialization of a state variable using indie.Var[int]:


    Error: [internal] cannot initialize variable self.trend_state of type <class 'indie.Var[int]'> with None that has type <class 'NoneType'>.
    ​

    This is my simplified code to understand the error:

    # indie:lang_version = 4
    from indie import (
    indicator, MainContext, algorithm,
    SeriesF, MutSeriesF, Var,
    param, plot, color, Plot
    )
    from indie.algorithms import Sma, Ema

    @indicator("Trend Following System")
    class Main(MainContext):
    def __init__(self):
    """Initializing state tracking variables"""
    self.trend_state: Var[int] = None #!! Causes Error
    self.signal_counter: Var[int] = None #!! Causes Error

    def calc(self):
    """Trend calculation and state tracking"""
    if self.trend_state is None:
    self.trend_state = Var[int].new(0)
    if self.signal_counter is None:
    self.signal_counter = Var[int].new(0)

    prev_state = self.trend_state.get()

    # Test logic
    strength = self.close[0] - self.close[1] # Simplified for testing

    # Update trend state
    if strength > 0.5:
    self.trend_state.set(1)
    elif strength < -0.5:
    self.trend_state.set(-1)
    else:
    self.trend_state.set(0)

    if self.trend_state.get() != prev_state:
    self.signal_counter.set(self.signal_counter.get() + 1)

    return Plot(strength, color=color.GREEN if strength > 0 else color.RED)

    What I’ve Tried


    • Declare self.trend_state in __init__() → Gives NoneType initialization error


    • Move Var.new() to calc() → Is this best practice?


    • Check self.trend_state.get() is only called after initialization


    • Try Optional[Var[int]] but Indie doesn’t seem to support it
    Suspected Issues


    • Should Var.new() only be called in calc() instead of __init__()?


    • Does Indie require a different way to declare state-tracking variables in indicators?


    • Is there a better way to initialize Var[int] in Indie without hitting type errors?


    • Are there any guidelines for stateful variables in Indie indicators?

    Continue reading...

Compartilhe esta Página