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

求助:如何使用table.cell函数展示label.new生成的数值到表格中

如何用table.cell函数将label.new生成的数值显示到表格中

核心思路

要实现需求,需先提取label.new里用到的核心数据(枢轴高点价格、涨跌幅、成交量等),再通过Pine Script的table系列函数创建表格,将数据填充到对应单元格中,可替代或配合原标签显示逻辑。

步骤与代码示例

  1. 初始化表格
    在脚本启动阶段创建表格并设置表头:
// 初始化表格,定位在右下角,设2列6行
var pivotTable = table.new(position.bottom_right, 2, 6, border_width=1)
// 设置表头样式
table.cell(pivotTable, 0, 0, "指标项", text_color=color.white, bg_color=#284aa9)
table.cell(pivotTable, 1, 0, "数值", text_color=color.white, bg_color=#284aa9)
  1. 提取核心数据
    从原有label.new代码中,把要展示的数值单独存为变量:
if not na(pvtHighTemp) and pvtHighTemp > pvtHigh1Temp
    label.delete(tempHigh[1])
    tradedVolume = f_getTradedVolume(profileLengthTemp, proceedTemp, pvtLengthTemp)
    // 提取需展示的核心数据
    pivotPrice = str.tostring(pvtHighTemp, format.mintick)
    priceChange = str.tostring((pvtHighTemp - pvtLow1) * 100 / pvtLow1, '#.##')
    totalVolume = str.tostring(tradedVolume, format.volume)
    avgVolumePerBar = str.tostring(tradedVolume / (profileLengthTemp - 1), format.volume)
    barsSinceLast = str.tostring(profileLengthTemp)
  1. 填充表格单元格
    将提取的数据逐个填入表格对应位置:
// 填充表格内容
    table.cell(pivotTable, 0, 1, "临时枢轴高点")
    table.cell(pivotTable, 1, 1, pivotPrice)
    
    table.cell(pivotTable, 0, 2, "价格涨幅(%)")
    table.cell(pivotTable, 1, 2, "↑ " + priceChange)
    
    table.cell(pivotTable, 0, 3, "总成交量")
    table.cell(pivotTable, 1, 3, totalVolume)
    
    table.cell(pivotTable, 0, 4, "单均成交量")
    table.cell(pivotTable, 1, 4, avgVolumePerBar)
    
    table.cell(pivotTable, 0, 5, "距上次枢轴K线数")
    table.cell(pivotTable, 1, 5, barsSinceLast)
  1. 调整原有标签逻辑(可选)
    如果不需要保留原标签显示,直接注释掉tempHigh := label.new(...)部分代码即可。

完整修改后的代码片段

//@version=5
indicator("Pivot Table Demo", overlay=true)

// 假设已定义以下变量/函数:pvtHighTemp, pvtHigh1Temp, f_getTradedVolume, pvtLow1, profileLengthTemp等

// 初始化表格
var pivotTable = table.new(position.bottom_right, 2, 6, border_width=1)
table.cell(pivotTable, 0, 0, "指标项", text_color=color.white, bg_color=#284aa9)
table.cell(pivotTable, 1, 0, "数值", text_color=color.white, bg_color=#284aa9)

tempHigh = label.new(na, na, "") // 初始化标签变量

if not na(pvtHighTemp) 
    if pvtHighTemp > pvtHigh1Temp
        label.delete(tempHigh[1])
        tradedVolume = f_getTradedVolume(profileLengthTemp, proceedTemp, pvtLengthTemp)
        // 提取核心数据
        pivotPrice = str.tostring(pvtHighTemp, format.mintick)
        priceChange = str.tostring((pvtHighTemp - pvtLow1) * 100 / pvtLow1, '#.##')
        totalVolume = str.tostring(tradedVolume, format.volume)
        avgVolumePerBar = str.tostring(tradedVolume / (profileLengthTemp - 1), format.volume)
        barsSinceLast = str.tostring(profileLengthTemp)
        
        // 填充表格
        table.cell(pivotTable, 0, 1, "临时枢轴高点")
        table.cell(pivotTable, 1, 1, pivotPrice)
        
        table.cell(pivotTable, 0, 2, "价格涨幅(%)")
        table.cell(pivotTable, 1, 2, "↑ " + priceChange)
        
        table.cell(pivotTable, 0, 3, "总成交量")
        table.cell(pivotTable, 1, 3, totalVolume)
        
        table.cell(pivotTable, 0, 4, "单均成交量")
        table.cell(pivotTable, 1, 4, avgVolumePerBar)
        
        table.cell(pivotTable, 0, 5, "距上次枢轴K线数")
        table.cell(pivotTable, 1, 5, barsSinceLast)
        
        // 若不需要标签显示,可注释下面一行
        tempHigh := label.new(bar_index[pvtLengthTemp], pvtHighTemp, '* ' + (pvtPrice ? pivotPrice :  '') + (pvtChange ? (pvtPrice ? ' ↑ %' : '↑ %') + priceChange : '') + (pvtVolume and  nzVolume ? (pvtPrice or pvtChange ? '\n' : '') + totalVolume : ''), xloc.bar_index, yloc.price, #284aa9, label.style_label_down, color.white, (not pvtPrice and not pvtChange and not pvtVolume ? size.tiny : pvtTextSize), text.align_center, 'Temporary Pivot High : ' + pivotPrice + '\n -Price Change : ↑ %' + priceChange + (nzVolume ? '\n -Traded Volume : ' + totalVolume  + ' (' + str.tostring(profileLengthTemp - 1) + ' bars)\n  *Average Volume/Bar : ' + avgVolumePerBar : '') + '\n\nNumber of bars\n since last confirmed Pivot High/Low : ' + barsSinceLast)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 05:15:39