Pine语言代码报错:未声明标识符,请求排查问题原因
Pine代码未声明标识符错误排查
- 错误根源:调用
sma()函数时缺少数据源参数。Pine Script v3中sma()的语法为sma(source, length),必须指定计算均线的标的数据(比如收盘价close),仅传入周期会导致系统识别到未声明的标识符。 - 修正后的代码:
// This is a simple moving average cross-over strategy //@version=3 strategy("Crossover") // Buy rule entryLong = crossover(sma(close, 10), sma(close, 20)) if (entryLong) strategy.entry("MyLong", strategy.long, qty=1, limit=10, stop=5) // Sell rule entryShort = crossunder(sma(close, 10), sma(close, 20)) if entryShort strategy.entry("MyShort", strategy.short, qty=1, limit=10, stop=5)
- 额外提示:代码里
limit=10和stop=5是固定价格,实际回测中可能不符合行情逻辑,建议根据价格动态设置(比如close * 1.01这类形式),不过这并非当前错误的诱因。
内容的提问来源于stack exchange,提问作者Sulkhan Metreveli
相关产品推荐
相关产品推荐

