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

如何在PineScript的数据窗口中显示if语句内的lres和lsup值?

解决PineScript在数据窗口显示line对象相关值的问题

要在数据窗口显示原代码中lres和lsup的相关值,需注意两个核心点:

  • 原代码中lres、lsup是if barstate.islast块内的局部变量,外部无法访问,必须用全局变量存储值
  • 数据窗口仅显示通过plot()函数输出的系列数据,需指定display=display.data_window参数

方案1:显示line对象的ID(内部标识)

line.new()返回的是line对象的唯一ID数值,若需显示该ID,可按以下方式修改代码:

// 声明全局变量存储line ID
var float line_res_id = na
var float line_sup_id = na

// 原有的线条绘制逻辑
if barstate.islast
    lres = line.new(x1=bar_index, y1=lvalue, x2=lindex, y2=lvalue, color=isBullish ? bearishColor : bullishColor, width=2, style=line.style_dashed, extend=extend.left)
    lsup = line.new(x1=bar_index, y1=llvalue, x2=llindex, y2=llvalue, color=isBullish ? bullishColor : bearishColor, width=2, style=line.style_dashed, extend=extend.left)
    // 将局部变量的ID赋值给全局变量
    line_res_id := lres
    line_sup_id := lsup

// 在数据窗口显示line ID
plot(line_res_id, title="支撑线ID", display=display.data_window)
plot(line_sup_id, title="压力线ID", display=display.data_window)

方案2:显示线条的价格位置(更实用)

通常line ID无实际分析意义,更常用的是显示线条对应的价格值(即代码中的lvalue和llvalue),修改代码如下:

// 声明全局变量存储价格值
var float support_price = na
var float resistance_price = na

// 原有的线条绘制逻辑
if barstate.islast
    lres = line.new(x1=bar_index, y1=lvalue, x2=lindex, y2=lvalue, color=isBullish ? bearishColor : bullishColor, width=2, style=line.style_dashed, extend=extend.left)
    lsup = line.new(x1=bar_index, y1=llvalue, x2=llindex, y2=llvalue, color=isBullish ? bullishColor : bearishColor, width=2, style=line.style_dashed, extend=extend.left)
    // 将线条的价格值赋值给全局变量
    support_price := lvalue
    resistance_price := llvalue

// 在数据窗口显示价格
plot(support_price, title="支撑线价格", display=display.data_window)
plot(resistance_price, title="压力线价格", display=display.data_window)

关键说明

  • var关键字用于声明全局变量,确保变量不会在每根K线重新初始化,仅在首次加载时初始化一次
  • display=display.data_window参数指定plot的内容仅在数据窗口显示,不会在图表上绘制线条
  • 若需要同时在图表和数据窗口显示,可去掉该参数或改为display=display.all

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 18:05:08