Pinescript 2策略Webhook注释传输问题及版本适配咨询
问题描述
我有一个基于Pine Script 2编写的策略指标,尝试转成Pine Script 5后精度大幅下降,显示效果基本没法用,所以保留了Pine Script 2代码。现在想通过strategy.entry("long", strategy.long, comment= "Long TSLA")配置Webhook,但注释信息传不过去,只能收到指标名称,拿不到交易执行的注释。想咨询:
- 操作上的失误点在哪里?
- Pine Script 2或3是否支持通过代码把
strategy.entry指令发送到警报? - 转成Pine Script 3能不能解决这个问题?
附Pine Script 2代码:
//@version=2 strategy("Test 141 Vers 2 - GetTrendStrategy", overlay=true) tim=input('28') out1 = security(tickerid, tim, open) out2 = security(tickerid, tim, close) tickid = tickerid plot(out1,color=red, linewidth = 3, transp = 0) plot(out2,color=green, linewidth = 3, transp = 0) long_com = "LL_Alert Long {{ticker}}" short_com = "SS_Alert Short {{ticker}}" // Long longCondition = crossover(security(tickerid, tim, close),security(tickerid, tim, open)) if (longCondition) //strategy.entry("LL_Alert Long {tickerid}", strategy.long) strategy.entry("Long", strategy.long) // , comment="Enter Long" //alertcondition(longCondition, title='LL_Alert', message='LL_Alert Long {tickerid}') alertcondition(longCondition, title="Long Entry Alert", message="Long Entry Triggered! Price: {{close}} Time: {{time}}") plotshape(longCondition, style=shape.triangleup, location=location.belowbar, color=green, transp = 0, size = size.normal) // Short shortCondition = crossunder(security(tickerid, tim, close),security(tickerid, tim, open)) if (shortCondition) //strategy.entry("SS_Alert Short {tickerid}", strategy.short) strategy.entry("Short", strategy.short) //alertcondition(shortCondition, title='SS_Alert', message='SS_Alert Short {tickerid}') alertcondition(shortCondition, title="Short Entry Alert", message="Short Entry Triggered! Price: {{close}} Time: {{time}}") plotshape(shortCondition, style=shape.triangledown, location=location.abovebar, color=red, transp = 0, size = size.normal)
问题解答
一、操作失误点
- Webhook逻辑误解:Pine Script 2中
strategy.entry的comment参数仅用于图表标注,不会自动同步到Webhook或警报消息。Webhook内容需要通过TradingView的警报设置模板配置,而非依赖代码里的comment参数。 - 警报触发方式错误:当前代码用
alertcondition触发警报,但如果要传递strategy.entry的交易信号到Webhook,需要在策略的警报设置中选择「当策略发出订单时触发」,而非基于alertcondition的条件触发。
二、Pine Script 2/3对strategy.entry警报的支持
- Pine Script 2:支持通过策略警报触发Webhook,但
strategy.entry本身不会自动触发警报,需在策略设置里开启「警报触发」选项。同时无法直接在代码中指定Webhook内容,必须在警报设置界面手动配置消息模板,或结合alertcondition传递自定义参数。 - Pine Script 3:逻辑和V2类似,
strategy.entry的comment依然是图表标注用途,但V3的警报系统支持更多变量占位符,能更灵活地传递交易相关信息,比如可以引用{{strategy.order.comment}}来获取代码中设置的注释内容。
三、转Pine Script 3能否解决问题?
转Pine Script 3不能直接解决Webhook注释传递的核心问题,但能解决你之前V5转换时的精度下降问题:
- 精度修复:V2转V5时精度下降通常是因为
security函数的合并逻辑变化(V5默认启用缺口和前瞻合并,V2默认关闭),而V3的security函数逻辑和V2更接近,能避免精度丢失。 - Webhook优化:V3的警报模板支持
{{strategy.order.comment}}占位符,只要在strategy.entry中设置了comment参数,就能在Webhook中传递该注释内容,比V2更灵活。
代码调整建议(Pine Script 2适配Webhook)
如果继续使用V2,要让Webhook收到自定义注释,需做以下调整:
- 在
strategy.entry中补充comment参数:if (longCondition) strategy.entry("Long", strategy.long, comment=long_com) if (shortCondition) strategy.entry("Short", strategy.short, comment=short_com) - 打开TradingView的警报设置:
- 触发条件选择「策略测试器发出订单时」
- 消息模板填写
{{strategy.order.comment}},这样Webhook就能收到你设置的long_com/short_com内容
内容的提问来源于stack exchange,提问作者Max
相关产品推荐
相关产品推荐

