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

Pine Script v5告警消息左侧缩进空格不生效问题咨询

Pine Script v5 告警消息前导缩进丢失解决方案

问题根因

  • Pine Script v5的告警消息解析逻辑会自动过滤每行开头的字面量半角空格、制表符\t,这就是手动拼接在字符串行首的所有缩进字符都被吞掉、最终输出全顶格的原因。
  • 原有测试代码里的"\{"属于多余转义,Pine语法中大括号不需要加反斜杠转义,这个写法反而会输出多余的反斜杠字符。

修复方法

  • 所有需要保留的前导缩进空格,不要直接敲字面量空格,改用Unicode转义字符\u0020(对应标准半角空格)编写,解析器不会过滤转义生成的空格,就能完整保留缩进结构。
  • 不推荐用\t做缩进,不同告警接收端对制表符的宽度解析不统一,用转义空格的兼容性最好,输出格式稳定。

修正后可直接运行的代码

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

//@version=5
indicator("Test message for alert")

// 预定义缩进层级,用\u0020代替字面量空格避免被过滤
indent_2 = "\u0020\u0020"
indent_4 = "\u0020\u0020\u0020\u0020"

alert_msg = "{" + "\n"
alert_msg += indent_2 + "\"name\": \"___\"," + "\n"
alert_msg += indent_2 + "\"secret\": \"___\"," + "\n"
alert_msg += indent_2 + "\"side\": \"buy\"," + "\n"
alert_msg += indent_2 + "\"symbol\": \"___\"," + "\n"
alert_msg += indent_2 + "\"open\": {" + "\n"
alert_msg += indent_4 + "\"amountType\": \"___\"," + "\n"
alert_msg += indent_4 + "\"amount\": \"___\"" + "\n"
alert_msg += indent_2 + "}" + "\n"
alert_msg += "}"

if true
    alert(alert_msg, alert.freq_once_per_bar_close)

plot(0)

运行后告警输出的JSON格式和预期的带缩进结构完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 11:51:22