如何将多入场/离场的Pine Script策略信号发送至3Commas机器人?
多信号Supertrend策略对接3Commas机器人警报解决方案需求
我正在学习Pine Script(进度较慢),目前使用一个多入场、多离场的Supertrend策略脚本,希望创建警报以将买卖信号发送至3Commas机器人。找到的相关教程仅适用于单信号场景,特此寻求针对该多信号脚本的解决方案。
原策略脚本
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © AliNotty // with thanks to MarketShree //@version=5 strategy('Supertrend Strategy', overlay=true) atrPeriod1 = input(7, 'ATR Length-1') factor1 = input(1.5, 'Factor-1') atrPeriod2 = input(10, 'ATR Length-2') factor2 = input(2, 'Factor-2') atrPeriod3 = input(20, 'ATR Length-3') factor3 = input(3, 'Factor-3') [superTrend1, direction1] = ta.supertrend(factor1, atrPeriod1) [superTrend2, direction2] = ta.supertrend(factor2, atrPeriod2) [superTrend3, direction3] = ta.supertrend(factor3, atrPeriod3) if ta.change(direction1) < 0 strategy.entry('LONG', strategy.long) if ta.change(direction1) > 0 strategy.entry('SHORT', strategy.short) if ta.change(direction2) < 0 strategy.entry('LONG', strategy.long) if ta.change(direction2) > 0 strategy.entry('SHORT', strategy.short) if ta.change(direction3) < 0 strategy.entry('LONG', strategy.long) if ta.change(direction3) > 0 strategy.entry('SHORT', strategy.short) colResistance = direction1 == 1 and direction1 == direction1[1] ? color.new(color.red, 0) : color.new(color.red, 100) colSupport = direction1 == -1 and direction1 == direction1[1] ? color.new(color.green, 0) : color.new(color.green, 100) plot(superTrend1, color=colResistance, linewidth=2) plot(superTrend1, color=colSupport, linewidth=2) colResistance1 = direction2 == 1 and direction2 == direction2[1] ? color.new(color.red, 0) : color.new(color.red, 100) colSupport1 = direction2 == -1 and direction2 == direction2[1] ? color.new(color.green, 0) : color.new(color.green, 100) plot(superTrend2, color=colResistance, linewidth=2) plot(superTrend2, color=colSupport, linewidth=2) colResistance2 = direction3 == 1 and direction3 == direction3[1] ? color.new(color.red, 0) : color.new(color.red, 100) colSupport2 = direction3 == -1 and direction3 == direction3[1] ? color.new(color.green, 0) : color.new(color.green, 100) plot(superTrend3, color=colResistance1, linewidth=2) plot(superTrend3, color=colSupport1, linewidth=2)
解决方案:添加多信号警报支持
修改后的脚本(新增警报逻辑)
在原脚本末尾添加以下代码,为每个Supertrend信号单独创建可识别的警报条件:
// 定义各信号触发条件 st1_long = ta.change(direction1) < 0 st1_short = ta.change(direction1) > 0 st2_long = ta.change(direction2) < 0 st2_short = ta.change(direction2) > 0 st3_long = ta.change(direction3) < 0 st3_short = ta.change(direction3) > 0 // 创建警报规则 alertcondition(st1_long, title="Supertrend1 多单信号", message="buy") alertcondition(st1_short, title="Supertrend1 空单信号", message="sell") alertcondition(st2_long, title="Supertrend2 多单信号", message="buy") alertcondition(st2_short, title="Supertrend2 空单信号", message="sell") alertcondition(st3_long, title="Supertrend3 多单信号", message="buy") alertcondition(st3_short, title="Supertrend3 空单信号", message="sell")
关键说明
- 信号映射:每个警报条件完全对应原脚本中的
strategy.entry触发逻辑,确保信号一致性。 - 消息格式:使用
buy/sell作为消息内容,符合3Commas机器人的基础触发规则;若需要区分是哪个Supertrend触发的信号,可将消息改为buy_st1/sell_st2等自定义格式,后续在3Commas中配置对应规则即可。 - 警报设置:
- 在TradingView中打开修改后的脚本,点击「添加警报」。
- 选择对应的警报条件(如「Supertrend1 多单信号」),警报类型选择「Webhook URL」。
- 填入3Commas提供的Webhook地址,消息框直接使用alertcondition中定义的内容(或自定义)。
- 重复上述步骤为所有6个信号设置警报,或选择「任何条件」一次性触发所有信号(需确保消息能被3Commas正确识别)。
内容的提问来源于stack exchange,提问作者AliNotty
相关产品推荐
相关产品推荐

