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

如何修复TradingView Pine Editor中的‘行末无续行符’语法错误?

问题解决:Supertrend指标的“行末无续行符”语法错误

你的代码存在两处关键语法问题,直接导致了报错:

错误1:三元表达式不完整

longFillColor和shortFillColor的赋值使用了三元运算符,但只定义了highlighting为true时的返回值,缺少false分支的内容。Pine Script要求三元表达式必须遵循条件 ? 真值 : 假值的完整结构,缺一不可。

错误2:函数调用后多余的冒号

两个fill()函数调用末尾的冒号是无效语法,Pine Script中函数调用不需要用冒号结尾。

修正后的完整代码

//@version=5
indicator("Supertrend", overlay=true, format=format.price, precision=2)

Periods = input.int(title="ATR Period", defval=10, minval=1)
src = input(close, title="Source")
Multiplier = input.float(title="ATR Multiplier", defval=3.0, minval=0.1, step=0.1)
changeATR = input.bool(title="Change ATR Calculation Method?", defval=true)
showsignals = input.bool(title="Show Buy/Sell Signals?", defval=true)
highlighting = input.bool(title="Highlighter On/Off?", defval=true)

atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2

up = src - (Multiplier * atr)
up1 = ta.valuewhen(ta.close[1] > up[1], up, 1)
up := ta.close[1] > up1 ? math.max(up, up1) : up

dn = src + (Multiplier * atr)
dn1 = ta.valuewhen(ta.close[1] < dn[1], dn, 1)
dn := ta.close[1] < dn1 ? math.min(dn, dn1) : dn

trend = 1
trend := ta.valuewhen(trend[1] == -1 and ta.close > dn1, 1, 1)
trend := ta.valuewhen(trend[1] == 1 and ta.close < up1, -1, 1)

upPlot = plot(trend == 1 ? up : na, title="Up Trend", color=color.new(color.green, 0), linewidth=2)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))

dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", color=color.new(color.red, 0), linewidth=2)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))

mPlot = plot(ta.ohlc4, title="", style=plot.style_circles, linewidth=0)
// 修复三元表达式,补充highlighting为false时的返回值na
longFillColor = highlighting ? (trend == 1 ? color.new(color.green, 80) : color.new(color.white, 0)) : na
shortFillColor = highlighting ? (trend == -1 ? color.new(color.red, 80) : color.new(color.white, 0)) : na
// 移除函数调用后的无效冒号
fill(mPlot, upPlot, title="UpTrend Highlighter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highlighter", color=shortFillColor)

alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")

changeCond = trend != ta.valuewhen(trend[1] != trend[1], trend, 1)
alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")

额外优化建议

如果想简化代码结构,也可以直接在fill函数中判断条件,省去中间变量的定义:

// 替换原来的longFillColor、shortFillColor定义和fill调用
if highlighting
    fill(mPlot, upPlot, title="UpTrend Highlighter", color=trend == 1 ? color.new(color.green, 80) : color.new(color.white, 0))
    fill(mPlot, dnPlot, title="DownTrend Highlighter", color=trend == -1 ? color.new(color.red, 80) : color.new(color.white, 0))

内容的提问来源于stack exchange,提问作者W3C0d3

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:17:31