TradingView策略对接3Commas交易机器人无信号触发问题求助
TradingView对接3Commas信号推送失败修复方案
代码语法错误修复
你的代码存在多处基础语法错误,是信号推送失败的核心原因,按以下规则修改:
- 移除冗余逻辑:删除if块内重复的
strategy.entry、strategy.close调用,仅保留你最后定义Long、ExitLong变量后的调用逻辑,前面的错误调用同时存在会干扰信号输出 - 修正运算符错误:所有参数赋值使用单等号
=,删除代码中多处误写的comment==双等号语法 - 补全缺失参数:
- 给short方向的
strategy.entry、strategy.close补全对应的alert_message参数,格式参考long方向的写法 - 给
strategy.exit止盈止损指令补全对应的离场信号alert_message,否则止盈止损触发时不会推送信号到3Commas - 补全所有缺失的括号,避免语法报错导致策略无法正常运行
- 给short方向的
- 统一信号参数:核对所有信号JSON内的
bot_id、email_token,确保和3Commas后台对应机器人的参数完全一致,当前代码中不同位置的这两个参数存在不匹配的问题
警报配置修正
代码修复完成后,创建TradingView警报时必须满足以下要求:
- 警报消息内容选择alert_message(),不能使用默认的订单注释/默认消息模板,否则代码中配置的信号JSON不会被推送
- 不要使用历史回测触发的信号测试,仅实时行情触发的警报才会实际推送至3Commas
- 确认3Commas机器人已开启TradingView信号接入权限,且机器人绑定的交易所、交易对和TradingView策略的标的完全匹配
修正后的核心代码参考
// === /STRATEGY === Long = '{"message_type": "bot", "bot_id": 你的机器人ID, "email_token": "你的邮箱令牌", "delay_seconds": 1}' ExitLong = '{"message_type": "bot", "bot_id": 你的机器人ID, "email_token": "你的邮箱令牌", "delay_seconds": 0, "action": "close_at_market_price"}' Short = '{"message_type": "bot", "bot_id": 你的机器人ID, "email_token": "你的邮箱令牌", "delay_seconds": 1}' ExitShort = '{"message_type": "bot", "bot_id": 你的机器人ID, "email_token": "你的邮箱令牌", "delay_seconds": 0, "action": "close_at_market_price"}' if ((ebar==0 or tdays<=ebar) and tradeType!="NONE") strategy.entry("long", strategy.long, when=longCond==true and tradeType!="SHORT", comment="Long", alert_message= Long) strategy.entry("short", strategy.short, when=shortCond==true and tradeType!="LONG", comment="Short", alert_message= Short) strategy.close("long", when = shortCond==true and tradeType=="LONG", comment="CloseLong", alert_message= ExitLong) strategy.close("short", when = longCond==true and tradeType=="SHORT", comment="CloseShort", alert_message= ExitShort) strategy.exit("XL", from_entry = "long", profit = TP, loss = SL, alert_message=ExitLong) strategy.exit("XS", from_entry = "short", profit = TP, loss = SL, alert_message=ExitShort)
内容的提问来源于stack exchange,提问作者Jim Miller
相关产品推荐
相关产品推荐

