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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 00:42:34