Indie中与PineScript的StDev算法对应的实现方式是什么?
Indie中与PineScript的StDev算法对应的实现方式是什么?
嘿,我之前也碰到过这个坑!你遇到的错误原因其实很简单——Indie标准库里面的标准差算法,正确名称是StdDev(注意结尾是大写的D),而不是你代码里写的Stdev,这就是为什么会抛出「package indie.algorithms does not contain symbol Stdev」这个错误。
你只需要对代码做两处小修改就能解决问题:
- 修正导入语句里的函数名:
from indie.algorithms import StdDev, Ema # 把Stdev改成StdDev
- 修正
calc方法里的调用代码:
stddev = StdDev.new(source, length) # 同样把Stdev改成StdDev
修改后的完整代码如下:
# indie:lang_version = 5 from indie import indicator, param, plot, color, source, MainContext from indie.algorithms import StdDev, Ema @indicator("StdDev Volatility Indicator") @param.int("length", default=20, min=1, title="StdDev Period") @param.int("smooth_length", default=10, min=1, title="Smoothing Period") @param.float("threshold_high", default=2.0, title="High Volatility Threshold") @param.float("threshold_medium", default=1.0, title="Medium Volatility Threshold") @param.source("source", default=source.CLOSE, title="Price Source") @plot.line(title="StdDev", color=color.BLUE) @plot.line(title="Smoothed StdDev", color=color.RED) @plot.line(title="High Threshold", color=color.RED(0.5)) @plot.line(title="Medium Threshold", color=color.YELLOW(0.5)) class Main(MainContext): def calc(self, length, smooth_length, threshold_high, threshold_medium, source): stddev = StdDev.new(source, length) smoothed_stddev = Ema.new(stddev, smooth_length) avg_stddev = Ema.new(stddev, length * 3)[0] high_threshold = avg_stddev * threshold_high medium_threshold = avg_stddev * threshold_medium return ( stddev[0], # Raw standard deviation smoothed_stddev[0], # Smoothed standard deviation high_threshold, # High volatility threshold line medium_threshold # Medium volatility threshold line )
这个StdDev.new()的用法和你熟悉的PineScript里的ta.stdev()逻辑完全一致,都是传入数据源和计算周期就能得到标准差结果,剩下的逻辑你写的都没问题,修改后应该就能正常运行了。
备注:内容来源于stack exchange,提问作者turfira
相关产品推荐
相关产品推荐

