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

TradingView策略对接capitalize.ai:重复告警与无执行问题排查

TradingView Webhook 对接 Capitalize.ai 问题排查

问题描述

已完成TradingView(简称TV)自动化策略开发,计划通过capitalize.ai(简称cap.ai)API对接Avatrade与Interactive Brokers执行交易。目前仅在TV向cap.ai发送Webhook告警环节出现问题:

  • 每次告警触发时生成10条重复告警(5条SELL、5条BUY)
  • cap.ai无法响应格式与URL均正确的告警/Webhook

策略核心代码(Pine Script)

// Alert Messages long
ANDstartLongTradeMessage = input.string(title = "Long Entry Webhook Message", defval = "", group = "Long Webhook Messages")
ANDendLongTradeMessage = input.string(title = "Close Long Webhook Message", defval = "", group = "Long Webhook Messages")

// Alert Messages short
ANDstartShortTradeMessage = input.string(title = "Short Entry Webhook Message", defval = "", group = "Short Webhook Messages")
ANDendShortTradeMessage = input.string(title = "Close Short Webhook Message", defval = "", group = "Short Webhook Messages")

// ------------------- Trading signals and Alerts  -----------------  See section on alerts above ANDTradelogic
// All orders have the same but dynamic text, that will show the direction of order
alertsyntax = '{{strategy.order.action}}  - - - Machine Learning: Logistic Regression (ME v.6 - DynTP & SL) (Close, , 3, 2, 0.0009, 1000, None, 5, 7, 1, 2023, 1, 1, 2029, 6, 0.01): 
     order {{strategy.order.action}} @ {{strategy.order.contracts}} filled on {{ticker}}. New strategy position is {{strategy.position_size}}' 
// Alerts in TV GUI will work with this message: {{ticker}} - {{strategy.order.action}} {{strategy.order.contracts}} @ {{strategy.order.price}}. New strategy position is {{strategy.position_size}} {{strategy.order.alert_message}}


// -------------------------------------------------------------------------

// ----------------   ANDTradelogic Fct   ------------------------------  Bruges nu til handler for at minimere antal trades 

ANDTradelogic(close) =>
    if ANDstartLongTrade
        strategy.entry('Long', strategy.long) //
        if use_TP 
            strategy.exit("Exit Long, TP", from_entry="Long", limit=high * (1+(tpPercent/100)))
        alert(message=alertsyntax, freq=alert.freq_all)   //, freq=alert.freq_once_per_bar_close)   
        alert(ANDendShortTradeMessage, freq = alert.freq_once_per_bar)
        alert(ANDstartLongTradeMessage, freq = alert.freq_once_per_bar)

    if ANDstartShortTrade
        strategy.entry('Short', strategy.short)
        if use_TP 
            strategy.exit("Exit Short, TP", from_entry="Short", limit=low * (1-(tpPercent/100)))
        alert(message=alertsyntax, freq=alert.freq_all) //, freq=alert.freq_once_per_bar_close)   
        alert(ANDendLongTradeMessage, freq = alert.freq_once_per_bar)
        alert(ANDstartShortTradeMessage, freq = alert.freq_once_per_bar)

    // Final decision of when to trade
    if ANDendShortTrade
        strategy.close('Long', 'Long')
        alert(message=alertsyntax) //, freq=alert.freq_once_per_bar_close)   
        alert(ANDendShortTradeMessage, freq = alert.freq_once_per_bar)

    if ANDendLongTrade
        strategy.close('Short', 'Short')
        alert(message=alertsyntax) //, freq=alert.freq_once_per_bar_close)   
        alert(ANDendLongTradeMessage, freq = alert.freq_once_per_bar)


if window()  // if in timewindow of trading/backtest

    // Run ANDTradelogic Fct.
    ANDTradelogic (close)

核心需求

需要cap.ai从TV策略代码中动态获取交易方向(BUY/SELL)与随时间变化的交易数量,硬编码方案无法满足需求。该策略在TV回测中表现正常,可双向交易且数量动态变化(如BUY x、Short 2x、buy 2x、short 2x等)。


问题排查与修复方案

1. 重复告警的核心原因

代码中存在多处冗余的alert()调用,且频率设置错误:

  • 每个交易信号分支(如ANDstartLongTrade)同时调用2-3个alert函数,单次信号触发会生成多条告警
  • 使用alert.freq_all会在订单的每一次状态变化(提交、成交、修改等)时重复发送告警,进一步放大重复量
  • 多alert调用会重复渲染strategy.order变量,导致cap.ai接收到混淆的方向与数量信息

2. Cap.ai无法响应的可能原因

  • 格式不匹配:当前alertsyntax为纯文本,未封装为cap.ai要求的结构化JSON格式,导致API无法解析有效指令
  • 重复信号冲突:短时间内大量矛盾的BUY/SELL信号会让cap.ai无法识别有效指令
  • 变量时机问题:在strategy.entry/strategy.close调用后立即引用strategy.order,可能此时变量未完成赋值,传递无效数据

3. 具体修复步骤

(1)合并告警逻辑,移除冗余调用

每个交易分支仅保留一次结构化告警发送,示例修改如下:

// 定义符合cap.ai要求的结构化JSON格式
capWebhookMessage = '{"action":"{{strategy.order.action}}","quantity":{{strategy.order.contracts}},"ticker":"{{ticker}}"}'

ANDTradelogic(close) =>
    if ANDstartLongTrade
        strategy.entry('Long', strategy.long)
        if use_TP 
            strategy.exit("Exit Long, TP", from_entry="Long", limit=high * (1+(tpPercent/100)))
        // 仅发送一次结构化告警,使用bar close时触发的频率
        alert(message=capWebhookMessage, freq=alert.freq_once_per_bar_close)

    if ANDstartShortTrade
        strategy.entry('Short', strategy.short)
        if use_TP 
            strategy.exit("Exit Short, TP", from_entry="Short", limit=low * (1-(tpPercent/100)))
        alert(message=capWebhookMessage, freq=alert.freq_once_per_bar_close)

    if ANDendShortTrade
        strategy.close('Long', 'Long')
        alert(message=capWebhookMessage, freq=alert.freq_once_per_bar_close)

    if ANDendLongTrade
        strategy.close('Short', 'Short')
        alert(message=capWebhookMessage, freq=alert.freq_once_per_bar_close)

(2)修正告警频率

使用alert.freq_once_per_bar_close替代alert.freq_all,确保单根K线仅发送一次告警,避免订单状态变化导致的重复发送。

(3)匹配cap.ai格式规范

确认cap.ai要求的字段名、数据类型与你传递的内容一致:

  • 确保action字段为cap.ai识别的BUY/SELL(注意大小写)
  • quantity字段需为纯数字,避免添加多余字符
  • 所有字段需封装为合法JSON格式

(4)单信号测试验证

在TV中手动触发单个交易信号,检查Webhook的发送数量与内容,确认无误后再对接cap.ai。

内容的提问来源于stack exchange,提问作者Morten Enevoldsen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 11:57:21