TradingView Pine Script报错求助:第13行'sma = 0.0'匹配错误
解决Pine Script报错:Mismatched input 'sma' expecting 'end of line without line continuation'
报错核心原因
- 自定义函数名
sma和Pine Script内置的sma()函数重名,同时函数内的变量名也叫sma,引发命名冲突 - 自定义函数的代码块未缩进,Pine Script要求函数体内容必须缩进(通常用4个空格)
修改后的完整代码
//@version=4 study("CLARITY ALGO FREE", overlay=true) slow = input(21, title="Slow EMA") //ema definitions emaSlow = ema(close, slow) // 自定义SMA函数,改名避免冲突,同时缩进函数体 custom_sma(src, length) => custom_sma_val = 0.0 custom_sma_val := na(custom_sma_val[1]) ? sum(src, length)/length : (custom_sma_val[1]*(length-1)+src)/length custom_sma_val smaFast = custom_sma(close, 9) //Color definition for Moving Averages col = close > emaSlow ? color.lime : close < emaSlow ? color.red : color.yellow //Moving Average Plots and Fill p1 = plot(emaSlow, title="Slow MA", style=plot.style_linebr, linewidth=4, color=col) p2 = plot(smaFast, title="Fast MA", style=plot.style_linebr, linewidth=2, color=col) fill(p1, p2, color=col, transp=70) plotshape(crossover(smaFast, emaSlow), style=shape.triangleup, size=size.tiny, color=color.green, text="Buy", title="Buy", location=location.belowbar) plotshape(crossover(emaSlow, smaFast), style=shape.triangledown, size=size.tiny, color=color.red, text="Sell", title="Sell") alertcondition(crossover(emaSlow, smaFast), title="Sell", message="Sell") alertcondition(crossover(smaFast, emaSlow), title="Buy", message="Buy")
关键修改说明
- 将自定义函数名从
sma改为custom_sma,避开内置函数的命名冲突 - 函数内的变量名改为
custom_sma_val,避免和函数名重名 - 函数体的三行代码统一缩进4个空格,符合Pine Script的语法规范
内容的提问来源于stack exchange,提问作者Johnny
相关产品推荐
相关产品推荐

