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

TradingView指标转JSON告警报错求助:NoneType不可迭代

TradingView Pine Script JSON告警Webhook报错解决

我在TradingView制作了自定义指标,通过Python对接了Telegram机器人,现在想将TradingView的告警改为JSON格式并通过Webhook发送,但遇到了问题。

原Pine Script代码

T1 = startPrice
E1 = startPrice + diff * -0.27
E2 = startPrice + diff * -0.414
E3 = startPrice + diff * -0.618
SL = startPrice + diff * -0.786
if Short or Long
    Tmessage = '{"Ticker":"'+syminfo.ticker+'",'+
        '"Position":'+str.tostring(message)+','+
        '"Position":"'+message+'",'+
        '"Entry 1":'+str.tostring(E1)+','+
        '"Target 1":'+str.tostring(T1)+','+
        '"Entry 2":'+str.tostring(E2)+','+
        '"Target 2":'+str.tostring(E1)+','+
        '"Entry 3":'+str.tostring(E3)+','+
        '"Target 3":'+str.tostring(E2)+','+
        '"Stop Lost":'+str.tostring(SL)+
        '}'
    alert('{"Ticker":"TEST"}', alert.freq_once_per_bar_close)

报错信息

Exception Occured : argument of type 'NoneType' is not iterable

解决步骤

  • 移除重复键值对:JSON不允许重复的键,代码中"Position"出现了两次,保留字符串类型的键值对"Position":"'+message+'",删掉前一行重复的"Position":'+str.tostring(message)+','。
  • 初始化message变量:报错提示NoneType不可迭代,说明message未被赋值或值为None。在if分支内先给message赋值为对应的方向字符串,比如message = Short ? "Short" : "Long"。
  • 修正JSON格式:拼接字符串时注意语法,最后一个键值对后不要加逗号,避免格式错误;数值类型不需要加双引号,字符串类型(如Ticker、Position)需用双引号包裹。
  • 发送正确的告警内容:原代码中alert调用的是测试字符串,改为发送拼接好的Tmessage。

修改后的示例代码

// 确保startPrice、diff、Short、Long等变量已正确定义
T1 = startPrice
E1 = startPrice + diff * -0.27
E2 = startPrice + diff * -0.414
E3 = startPrice + diff * -0.618
SL = startPrice + diff * -0.786

if Short or Long
    // 给message赋值为对应的交易方向
    message = Short ? "Short" : "Long"
    // 拼接符合JSON规范的字符串
    Tmessage = '{"Ticker":"'+syminfo.ticker+'",'+
        '"Position":"'+message+'",'+
        '"Entry 1":'+str.tostring(E1)+','+
        '"Target 1":'+str.tostring(T1)+','+
        '"Entry 2":'+str.tostring(E2)+','+
        '"Target 2":'+str.tostring(E1)+','+
        '"Entry 3":'+str.tostring(E3)+','+
        '"Target 3":'+str.tostring(E2)+','+
        '"Stop Lost":'+str.tostring(SL)+
        '}'
    // 发送JSON格式的告警
    alert(Tmessage, alert.freq_once_per_bar_close)

内容的提问来源于stack exchange,提问作者moeen babayi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 21:01:39