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

Pine Script 5均线交叉指标报错:无行续接符的行尾问题求助

移动均线交叉标记代码报错修复

问题描述

希望在两条移动平均线交叉时显示叠加标记,但运行代码时报错:end of line w/o line continuation,原代码如下:

//@version=5
indicator("Moving Average Cross", shorttitle="MA Cross", overlay=true, timeframe="", timeframe_gaps=true)

ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "SMMA (RMA)" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     na

show_ma1   = input(true   , "MA №1", inline="MA #1")
ma1_type   = input.string("SMA"  , ""     , inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma1_source = input(close  , ""     , inline="MA #1")
ma1_length = input.int(20     , ""     , inline="MA #1", minval=1)
ma1_color  = input(#f6c309, ""     , inline="MA #1")
ma1 = ma(ma1_source, ma1_length, ma1_type)
plot(show_ma1 ? ma1 : na, color = ma1_color, title="MA №1")

show_ma2   = input(true   , "MA №2", inline="MA #2")
ma2_type   = input.string("SMA"  , ""     , inline="MA #2", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma2_source = input(close  , ""     , inline="MA #2")
ma2_length = input.int(50     , ""     , inline="MA #2", minval=1)
ma2_color  = input(#fb9800, ""     , inline="MA #2")
ma2 = ma(ma2_source, ma2_length, ma2_type)
plot(show_ma2 ? ma2 : na, color = ma2_color, title="MA №2")

longCond = ta.crossover(ma1 > ma2)
shortCond = ta.crossunder(ma1 < ma2)

plotshape(series=longCond, title="Long", style=shape.triangleup, location=location.belowbar, color.green, text="LONG", size=size.normal)
plotshape(series=shortCond, title="Short", style=shape.triangledown, location=location.abovebar, color.red, text="SHORT", size=size.normal)

错误原因

报错核心是ta.crossover()和ta.crossunder()函数的用法错误:

  • 这两个函数需要传入两个序列参数,分别代表交叉的两条线,比如ta.crossover(序列A, 序列B)表示序列A从下往上穿过序列B的时刻。
  • 原代码中传入的是布尔表达式ma1 > ma2,不符合函数参数要求,导致语法解析失败,触发“行未延续”的报错。

修复后的代码

将交叉条件的写法修正为函数要求的格式即可:

//@version=5
indicator("Moving Average Cross", shorttitle="MA Cross", overlay=true, timeframe="", timeframe_gaps=true)

ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "SMMA (RMA)" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     na

show_ma1   = input(true   , "MA №1", inline="MA #1")
ma1_type   = input.string("SMA"  , ""     , inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma1_source = input(close  , ""     , inline="MA #1")
ma1_length = input.int(20     , ""     , inline="MA #1", minval=1)
ma1_color  = input(#f6c309, ""     , inline="MA #1")
ma1 = ma(ma1_source, ma1_length, ma1_type)
plot(show_ma1 ? ma1 : na, color = ma1_color, title="MA №1")

show_ma2   = input(true   , "MA №2", inline="MA #2")
ma2_type   = input.string("SMA"  , ""     , inline="MA #2", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma2_source = input(close  , ""     , inline="MA #2")
ma2_length = input.int(50     , ""     , inline="MA #2", minval=1)
ma2_color  = input(#fb9800, ""     , inline="MA #2")
ma2 = ma(ma2_source, ma2_length, ma2_type)
plot(show_ma2 ? ma2 : na, color = ma2_color, title="MA №2")

// 修正交叉条件的写法
longCond = ta.crossover(ma1, ma2)
shortCond = ta.crossunder(ma1, ma2)

plotshape(series=longCond, title="Long", style=shape.triangleup, location=location.belowbar, color.green, text="LONG", size=size.normal)
plotshape(series=shortCond, title="Short", style=shape.triangledown, location=location.abovebar, color.red, text="SHORT", size=size.normal)

说明

修复后,当短周期均线(MA1)上穿长周期均线(MA2)时,K线下方会显示绿色向上三角标记并标注“LONG”;当短周期均线(MA1)下穿长周期均线(MA2)时,K线上方会显示红色向下三角标记并标注“SHORT”,符合均线交叉信号的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 05:48:18