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

TradingView自定义警报无法解析指标变量Basis的问题

问题

我在TradingView中使用「(Hoss) VWAP AD」指标,想要设置自定义警报以获取指标变量“Basis”的数值,但警报执行时该变量无法被解析。当前我的警报消息内容为:{{close}}, {{time}}, {{plot_0}}, {{plot('Basis')}}。请问我哪里操作出错了?

附带指标代码:

//@version=5
indicator('[Hoss] VWAP Deviation', overlay=true)

ls = input(false, title='Log-space', group = "Optional")
type = 'Average Deviation'
length = input(60, group="Strategy Modification")
source = input(close, group="Strategy Modification")
_low = ls == true ? math.log(low) : low
_high = ls == true ? math.log(high) : high
src = ls == true ? math.log(source) : source

//weighted mean
pine_vwmean(x, y) =>
    cw = 0
    cd = 0.0
    w_sum = 0.0
    d_sum = 0.0
    for i = 0 to y - 1 by 1
        cd = x[i]
        cw = volume[i]
        d_sum += cw * cd
        w_sum += cw
        w_sum
    d_sum / w_sum

//weighted standard deviation
pine_vwstdev(x, y, b) =>
    d_sum = 0.0
    w_sum = 0.0
    cd = 0.0
    for i = 0 to y - 1 by 1
        cd = x[i]
        cw = volume[i]
        d_sum += cw * math.pow(cd - b, 2)
        w_sum += cw
        w_sum
    math.sqrt(d_sum / w_sum)

//weighted average deviation
pine_vwavdev(x, y, b) =>
    d_sum = 0.0
    w_sum = 0.0
    cd = 0.0
    for i = 0 to y - 1 by 1
        cd = x[i]
        cw = volume[i]
        d_sum += cw * math.abs(cd - b)
        w_sum += cw
        w_sum
    d_sum / w_sum

vwmean = pine_vwmean(src, length)

//consider using Average Deviation instead of Standard Deviatio if there are values outside of 3rd upper & lower bands within a rolling window
dev = if type == 'Standard Deviation'
    dev = pine_vwstdev(src, length, vwmean)
    dev
else if type == 'Average Deviation'
    dev = pine_vwavdev(src, length, vwmean)
    dev


plot(ls == true ? math.exp(vwmean) : vwmean, color=color.new(#b7b7b7, 60), title='Basis')

fill(plot1=plot(ls == true ? math.exp(vwmean + dev * 2) : vwmean + dev * 2, color=color.new(#B20000, 0), title='Upper dev 2'), plot2=plot(ls == true ? math.exp(vwmean + dev * 3) : vwmean + dev * 3, color=color.new(#FF6666, 0), title='Upper dev 3', display=display.none), color=color.new(#FF4D4D, 80), title='Upper band', transp=0)
fill(plot1=plot(ls == true ? math.exp(vwmean - dev * 3) : vwmean - dev * 3, color=color.new(#00CC00, 0), title='Lower dev 3', display=display.none), plot2=plot(ls == true ? math.exp(vwmean - dev * 2) : vwmean - dev * 2, color=color.new(#008000, 0), title='Lower dev 2'), color=color.new(#006600, 80), title='Lower band', transp=0)

// Input to enable or disable the table visibility
table_visible = input(false, title="Show Table", group="Deviation Cross Monitor")
// Input for the number of candles to look back
table_length = input(300, title="Table Lookback Length", group="Deviation Cross Monitor")

// Custom count function
count_occurrences(cond, length) =>
    count = 0
    for i = 0 to length - 1
        if cond[i]
            count := count + 1
    count

// Count occurrences of prices above Upper dev 2 and below Lower dev 2
upper_dev_2 = vwmean + dev * 2
lower_dev_2 = vwmean - dev * 2
above_upper_dev_2 = count_occurrences(close > upper_dev_2, table_length)
below_lower_dev_2 = count_occurrences(close < lower_dev_2, table_length)

// Create table in the bottom right corner
var table tbl = table.new(position=position.bottom_right, rows=2, columns=2)

if table_visible
    if barstate.islast
        // Update the table headers
        table.cell(tbl, 0, 0, "Above Upper Dev 2", bgcolor=color.gray, text_color=color.white)
        table.cell(tbl, 0, 1, "Below Lower Dev 2", bgcolor=color.gray, text_color=color.white)
        
        // Update the table values
        table.cell(tbl, 1, 0, str.tostring(above_upper_dev_2), bgcolor=color.new(color.green, 90), text_color=color.green)
        table.cell(tbl, 1, 1, str.tostring(below_lower_dev_2), bgcolor=color.new(color.red, 90), text_color=color.red)
else
    table.delete(tbl)

原因分析

  1. TradingView警报模板不支持plot('Basis')这种语法,该写法不符合警报变量的引用规范。
  2. 原指标仅通过plot函数直接绘制了Basis的计算结果,未将其定义为可被警报引用的输出变量,导致无法通过名称直接调用。

解决方案

步骤1:修改指标代码,暴露Basis变量

将原指标中直接绘制Basis的代码拆分为变量定义+绘制,并添加output关键字将变量暴露给警报系统:

// 替换原plot语句为以下代码
basis_value = ls == true ? math.exp(vwmean) : vwmean
plot(basis_value, color=color.new(#b7b7b7, 60), title='Basis')
output(basis_value, title="Basis") // 新增:将变量暴露为可引用序列

步骤2:修改警报消息内容

修改后的警报消息可通过两种方式获取Basis数值:

  • 方式一(推荐):直接引用暴露的变量名称
    {{close}}, {{time}}, {{Basis}}
    
  • 方式二:使用plot索引(原指标中Basis是第一个plot,对应plot_0)
    {{close}}, {{time}}, {{plot_0}}
    

完成以上修改后,警报即可正确解析并获取Basis的数值。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 18:47:20