TradingView策略对接Alertatron异常:仅触发多单问题求助
解决TradingView策略与Alertatron对接的多空/平仓信号识别问题
核心问题原因
默认的{{strategy.order.alert_message}}仅传递有限的订单信息,未包含Alertatron能识别的开空、平仓指令格式,导致仅多单操作生效。
解决方案步骤
1. 自定义Alertatron指令消息
放弃依赖默认策略alert消息,直接在代码中生成符合Alertatron要求的指令字符串,覆盖开多、开空、平仓三种场景:
- 开多指令:
apinamehere(AVAXUSDT){market(position=5%)} #bot #telegram - 开空指令:
apinamehere(AVAXUSDT){short,market(position=5%)} #bot #telegram(与你手动测试生效的格式保持一致) - 平仓指令:
apinamehere(AVAXUSDT){close,market} #bot #telegram(用于平掉当前仓位)
2. 修改策略代码,添加自定义alert触发逻辑
在原有策略代码基础上,加入alert()函数,在信号触发时发送对应指令:
//@version=5 strategy("Your Strategy Name", overlay=true) // 假设你已定义以下变量:longCondition, redCondition, short_only, long_tp, long_stop, short_tp, short_stop // 开多逻辑 if (longCondition) strategy.entry("Buy Long", strategy.long) // 发送开多指令 alert("apinamehere(AVAXUSDT){market(position=5%)} #bot #telegram", alert.freq_once_per_bar_close) // 开空逻辑 if (redCondition and not short_only) strategy.entry("Sell Short", strategy.short) // 发送开空指令 alert("apinamehere(AVAXUSDT){short,market(position=5%)} #bot #telegram", alert.freq_once_per_bar_close) // 多单止盈止损 strategy.exit("Exit Long", "Buy Long", limit=long_tp, stop=long_stop) // 检测多单平仓信号并发送指令 if (strategy.position_size == 0 and strategy.position_size[1] > 0) alert("apinamehere(AVAXUSDT){close,market} #bot #telegram", alert.freq_once_per_bar_close) // 空单止盈止损 strategy.exit("Exit Short", "Sell Short", limit=short_tp, stop=short_stop) // 检测空单平仓信号并发送指令 if (strategy.position_size == 0 and strategy.position_size[1] < 0) alert("apinamehere(AVAXUSDT){close,market} #bot #telegram", alert.freq_once_per_bar_close)
3. 调整TradingView Alert设置
创建Alert时:
- 选择Condition为「Any alert() function call」
- 消息字段留空(指令已在代码中定义)
- 确保Alert触发频率与代码中的
alert.freq_once_per_bar_close匹配,避免重复发送指令
4. 关键注意事项
- 确认Alertatron的指令格式完全匹配:比如
short参数位置、position=5%写法,必须和你手动测试生效的格式一致 - 策略中的订单ID(如"Buy Long"、"Sell Short")要唯一,避免
strategy.exit无法正确关联订单 - 测试时可先在TradingView的Alert预览中查看消息内容,确认格式正确后再对接Alertatron
内容的提问来源于stack exchange,提问作者David Frisell
相关产品推荐
相关产品推荐

