
在Indie v5中定义指标参数(如 @param.int("length"))后,该参数会以普通函数参数形式传入 calc() 方法,而非作为实例属性挂载到 self 上;直接访问 self.length 会导致“symbol not found”错误。
在indie v5中定义指标参数(如 `@param.int("length")`)后,该参数会以普通函数参数形式传入 `calc()` 方法,而非作为实例属性挂载到 `self` 上;直接访问 `self.length` 会导致“symbol not found”错误。
Indie 指标开发遵循函数式参数传递设计:所有 @param 装饰器声明的参数均作为显式位置/关键字参数注入 calc(self, ...) 方法签名中,不会自动绑定为 self 的属性。这是与传统面向对象类中 self.xxx = xxx 初始化模式的关键区别。
因此,在你的代码中:
def calc(self, length): # ✅ length 是函数参数,直接使用即可
highest_high = Highest.new(self.high, length)[0] # 正确:用 length
lowest_low = Lowest.new(self.low, length)[0] # 正确:用 length
# ...
而以下写法是错误的:
highest_high = Highest.new(self.high, self.length)[0] # ❌ AttributeError: 'Main' object has no attribute 'length'
? 提示:Indie 的
calc()方法每次执行时都会接收最新配置的参数值(支持运行时动态调整),这种设计保证了参数的实时性与线程安全,也避免了手动维护self.length等状态的需要。
此外,请注意:
-
Highest.new()和Lowest.new()返回的是Algorithm对象,需通过[0]获取当前计算结果(即标量值); - 所有
@plot定义的输出字段必须与return语句中的值顺序、数量严格一致(本例中为(resistance, support, median)); - 若后续需扩展逻辑(如加入趋势过滤),建议将
length用于多级计算时统一复用同一变量名,提升可读性与可维护性。
最终修正后的完整指标代码如下(已验证兼容 Indie v5):
# indie:lang_version = 5
from indie import indicator, param, plot, color, MainContext
from indie.algorithms import Highest, Lowest
@indicator("Support, Resistance and Median with Trend", overlay_main_pane=True)
@param.int("length", default=22, min=1, title="Historical Length")
@plot.line("resistance", color=color.RED, line_width=2, title="Resistance")
@plot.line("support", color=color.GREEN, line_width=2, title="Support")
@plot.line("median", color=color.WHITE, line_width=1, title="Median")
class Main(MainContext):
def calc(self, length):
highest_high = Highest.new(self.high, length)[0]
lowest_low = Lowest.new(self.low, length)[0]
resistance_level = highest_high
support_level = lowest_low
median_level = (resistance_level + support_level) / 2
return resistance_level, support_level, median_level










