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

关于Pine Script中K线高低点出现顺序及时间获取的技术问询

Pine Script:K线内高低点顺序判断与时间获取

核心实现思路

  • 借助逐tick数据跟踪当前K线内的高点、低点及其对应时间戳
  • 通过对比时间戳直接判断高低点出现顺序,以及最后出现的极值类型
  • 无需依赖K线涨跌(红绿),仅通过价格行为和时间戳完成判断

完整脚本实现

//@version=5
indicator("K线内高低点跟踪", overlay=true, shorttitle="HL Tracker")

// 初始化变量:存储当前K线的高点、低点及对应时间戳
var float current_high = na
var float current_low = na
var int high_time = na
var int low_time = na

// 逐tick更新当前K线的极值和时间
if barstate.isnew
    // 新K线启动,重置跟踪变量
    current_high := high
    current_low := low
    high_time := timestamp
    low_time := timestamp
else
    // 当前K线内更新极值与对应时间
    if high > current_high
        current_high := high
        high_time := timestamp
    if low < current_low
        current_low := low
        low_time := timestamp

// 判断最后出现的是高点还是低点
var string last_extremity = na
if barstate.islast
    if high_time > low_time
        last_extremity := "最后出现: 高点"
    else if low_time > high_time
        last_extremity := "最后出现: 低点"
    else
        last_extremity := "高低点同时出现"

// 在图表上标记最后出现的极值
var label last_label = na
if barstate.islast
    label.delete(last_label)
    if high_time > low_time
        last_label := label.new(bar_index, current_high, last_extremity, color=color.red, textcolor=color.white, style=label.style_label_down)
    else if low_time > high_time
        last_label := label.new(bar_index, current_low, last_extremity, color=color.green, textcolor=color.white, style=label.style_label_up)

// 数据窗口输出调试信息
plotchar(high_time, "高点时间戳", "", location.bottom)
plotchar(low_time, "低点时间戳", "", location.top)
plotstring(last_extremity, "最后极值类型", location=location.belowbar)

代码说明

  • barstate.isnew:检测新K线启动,重置跟踪变量避免跨K线干扰
  • timestamp:Pine Script内置变量,返回当前tick的Unix时间戳(单位秒)
  • 逐tick更新逻辑:每次价格创出当前K线新高/新低时,同步更新对应的时间戳
  • 判断逻辑:直接对比high_time和low_time的数值大小,完全脱离K线涨跌颜色的限制

高低点时间的后续对比

脚本中存储的high_time和low_time为整数类型的Unix时间戳,可直接用于各类时间对比场景:

  • 判断高点是否晚于指定时间:high_time > timestamp("2024-05-20T10:30:00")
  • 计算高低点出现的时间差:math.abs(high_time - low_time)
  • 跨K线对比极值时间:使用var变量存储历史K线的高低点时间,实现跨周期的时间逻辑判断

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 20:11:08