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

Pine Script编译报错:意外输入'alertMsg',预期无续行换行

Pine Script编译错误「意外输入'alertMsg',预期无续行换行」修复方案

错误根源分析

  • 函数作用域混乱:checkForAlert()函数定义后,后续的交叉判断逻辑未放在函数内部,导致alertMsg变量作用域错误,函数结构不完整引发语法报错。
  • 字符串格式化参数不匹配:str.format中使用两个占位符,但仅传入一个参数,会触发运行时错误。
  • 重复创建输入项:多次调用input.price(defval=0),冗余且不符合代码规范。
  • 返回语句缩进错误:alertMsg作为函数返回值,未缩进在checkForAlert()函数作用域内。

修正后的完整代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// slippery

//@version=5
indicator(title= "Street Bounce", overlay=true)

// Calculate the 8-bar and 20-bar Exponential Moving Average (EMA)
fastAverage = ta.ema(close, 8)
slowAverage = ta.ema(close, 20)
price1 = input.price(defval = 0, title="Price Level")

//plot
plot(fastAverage, color=color.yellow, title="Fast EMA")
plot(slowAverage, color=color.blue, title="Slow EMA")

// check any condition you needed to fire
checkForAlert()=>
    alertMsg = ""
    //check for price cross of 20 EMA
    cross1 = ta.crossover(price1, slowAverage)
    if (cross1)
        alertMsg += str.format("Price crossed above 20 EMA for {0}!\n", syminfo.ticker)

    cross2 = ta.crossunder(price1, slowAverage)
    if (cross2)
        alertMsg += str.format("Price crossed below 20 EMA for {0}!\n", syminfo.ticker)

    //any other checks needed
    alertMsg

fireAlert(ticker, freq = alert.freq_once_per_bar)=>
    msg = request.security(ticker, timeframe.period, checkForAlert())
    if str.length(msg) > 0
        alert(msg, freq)

fireAlert("AUDUSD")
fireAlert("EURUSD")
fireAlert("GBPUSD")

// Plot cross over point
plotshape(cross1 ? low : na, color=color.red, style=shape.triangleup, text = "BUY", textcolor = color.red, location=location.belowbar)
plotshape(cross2 ? high : na, color=color.green, style=shape.triangledown, text = "SELL", textcolor = color.green, location=location.abovebar)

关键修改点

  • 调整函数作用域:将交叉判断逻辑全部移入checkForAlert()函数内部,确保alertMsg变量在函数作用域内有效,解决核心语法报错。
  • 修正字符串格式化:匹配占位符与参数数量,明确提示信息内容。
  • 复用输入变量:用已定义的price1替代重复的input.price调用,统一价格输入项。
  • 修正返回语句缩进:将alertMsg缩进至checkForAlert()函数内部,作为函数返回值。
  • 调整标记代码位置:将K线买卖标记代码移至全局作用域,确保cross1和cross2变量可正常访问。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 09:43:24