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

如何在Pine Script中获取指定偏移前单根K线高低值并绘图

Pine Script获取历史K线高低值及绘制方法

核心实现逻辑

Pine Script内置了high、low全局变量对应每根K线的最高价、最低价,搭配偏移标记[]即可直接获取指定位置的历史K线数值,不需要调用额外的专用函数:

  • 你需要的30根之前的K线最高价写法为high[30],最低价写法为low[30]
    注意:Pine Script的偏移规则为正整数代表回溯历史K线,不需要传入-30作为偏移量

如果你要判断该30根前的K线是否为「higher high(比前一根K线最高价更高)」或「lower low(比前一根K线最低价更低)」形态,可直接写判断逻辑:

// 30根前K线是higher high
is_prev30_hh = high[30] > high[31]
// 30根前K线是lower low
is_prev30_ll = low[30] < low[31]

如果你需要的是判断该K线为N周期内的最高/最低,可以直接调用内置函数ta.higherhigh(length)、ta.lowerlow(length),比如ta.higherhigh(30)代表当前K线是过去30根K线的最高价。

完整可运行示例代码

//@version=5
indicator("30根前K线高低值绘制", overlay=true)

// 取30根前K线的高低价
prev30_high = high[30]
prev30_low = low[30]

// 判断形态(可选)
prev30_hh = high[30] > high[31]
prev30_ll = low[30] < low[31]

// 绘制高低价水平线
plot(prev30_high, color=color.green, title="30根前最高价", linewidth=2)
plot(prev30_low, color=color.red, title="30根前最低价", linewidth=2)

// 在对应K线位置标记形态(可选,offset=-30用于对齐30根前的K线位置)
plotshape(prev30_hh, location=location.abovebar, color=color.green, style=shape.triangledown, offset=-30, size=size.small)
plotshape(prev30_ll, location=location.belowbar, color=color.red, style=shape.triangleup, offset=-30, size=size.small)

常见注意点

  • 若K线总数不足30根,返回值会为na,不会报错
  • 如果需要在历史K线对应的位置绘制标记,必须给绘图函数传入offset=-30参数,否则标记会显示在当前K线位置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 08:06:07