You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TradingView Pine Script报错求助:第13行'sma = 0.0'匹配错误

解决Pine Script报错:Mismatched input 'sma' expecting 'end of line without line continuation'

报错核心原因

  1. 自定义函数名sma和Pine Script内置的sma()函数重名,同时函数内的变量名也叫sma,引发命名冲突
  2. 自定义函数的代码块未缩进,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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 05:29:56