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

如何获取最近50根K线的HH/LL/LH/HL枢轴序列?

识别K线枢轴序列(HH/LL/LH/HL)的Pine Script实现方案

核心思路

要生成4个连续枢轴的类型序列,关键是按时间顺序追踪价格的交替反转点,而非仅提取单周期的最高/最低点。具体逻辑:

  1. 从回溯期最左侧的旧K线开始,先识别第一个极端高低点(HH/LL)
  2. 切换方向,寻找下一个相反方向的极端反转点(LL/HH)
  3. 重复交替方向,直到收集到4个有效枢轴
  4. 将枢轴类型按时间顺序拼接成目标序列

完整实现代码

//@version=5
indicator("HHLL Sequence", "HHLL Seq", true, format.price, max_labels_count=51, max_lines_count=50)

// 输入参数
tgFibLineStyle  = input.string(line.style_dotted, options=[line.style_solid, line.style_dotted, line.style_dashed], title="线条样式", group='指示器', inline="tg2")
lookbackInput   = input.int(50, "回溯K线数量",  minval = 1, maxval = 100)
_isDebugLabelActive=input.bool(true, title="显示调试标签")

// 初始化枢轴追踪变量
var string[] pivotTypes = array.new_string()
var bool lookingForHigh = na  // 标记当前寻找高点/低点
var float lastPivotPrice = na
var int lastPivotBarIndex = na

// 每根新K线重新计算回溯期内的枢轴
if barstate.islast
    array.clear(pivotTypes)
    lookingForHigh = na
    lastPivotPrice = na
    lastPivotBarIndex = na
    
    // 从最旧K线到最新K线遍历
    for i = lookbackInput downto 0
        currentBarIndex = bar_index - i
        currentHigh = high[i]
        currentLow = low[i]
        
        // 第一步:确定第一个枢轴(回溯期内最早的极端点)
        if na(lookingForHigh)
            tempHigh = ta.highest(high, i+1)
            tempLow = ta.lowest(low, i+1)
            
            if currentHigh == tempHigh
                array.push(pivotTypes, "HH")
                lookingForHigh := false
                lastPivotPrice := currentHigh
                lastPivotBarIndex := currentBarIndex
            elif currentLow == tempLow
                array.push(pivotTypes, "LL")
                lookingForHigh := true
                lastPivotPrice := currentLow
                lastPivotBarIndex := currentBarIndex
        
        // 后续步骤:交替寻找反向枢轴
        else if array.size(pivotTypes) < 4
            if not lookingForHigh
                // 当前找低点:需是上一个枢轴后的新低
                if currentLow < ta.lowest(low, bar_index - lastPivotBarIndex)
                    pivotType = currentLow < lastPivotPrice ? "LL" : "HL"
                    array.push(pivotTypes, pivotType)
                    lookingForHigh := true
                    lastPivotPrice := currentLow
                    lastPivotBarIndex := currentBarIndex
            else
                // 当前找高点:需是上一个枢轴后的新高
                if currentHigh > ta.highest(high, bar_index - lastPivotBarIndex)
                    pivotType = currentHigh > lastPivotPrice ? "HH" : "LH"
                    array.push(pivotTypes, pivotType)
                    lookingForHigh := false
                    lastPivotPrice := currentHigh
                    lastPivotBarIndex := currentBarIndex

// 生成并显示枢轴序列
pivotSequence = array.size(pivotTypes) >=4 ? str.join("-", pivotTypes) : "不足4个枢轴"
if _isDebugLabelActive
    debugLabel = label.new(bar_index, close-(close*0.003), 
        text="枢轴序列: " + pivotSequence + "\n已识别枢轴数: " + str.tostring(array.size(pivotTypes)),
        textalign=text.align_left, yloc=yloc.price, color=color.black, textcolor=color.white,
        style=label.style_label_upper_left)
    label.delete(debugLabel[1])

// 绘制第一个枢轴的标签(可扩展绘制所有枢轴)
if array.size(pivotTypes) > 0
    firstPivotType = array.get(pivotTypes, 0)
    firstPivotPrice = firstPivotType=="HH" ? ta.highest(high, lookbackInput) : ta.lowest(low, lookbackInput)
    firstPivotBar = firstPivotType=="HH" ? bar_index - ta.barssince(high==ta.highest(high, lookbackInput)) : bar_index - ta.barssince(low==ta.lowest(low, lookbackInput))
    labelColor = firstPivotType=="HH" ? color.green : color.red
    label.new(firstPivotBar, firstPivotPrice, firstPivotType + " " + str.tostring(firstPivotPrice, "######.#####"), 
        color=labelColor, textcolor=color.white, style=label.style_label_down if firstPivotType=="HH" else label.style_label_up)

关键逻辑说明

  1. 枢轴类型判定规则:
    • 第一个枢轴:回溯期内最早出现的极端高低点(HH/LL)
    • 后续枢轴:必须是上一个枢轴之后的反向极端点,再通过与上一个枢轴的价格对比细分类型:
      • 上一个为HH,当前低点低于HH则标记为LL,否则为HL
      • 上一个为LL,当前高点高于LL则标记为HH,否则为LH
  2. 遍历逻辑:从回溯期最旧的K线开始遍历,确保按时间顺序捕捉价格反转节点
  3. 状态维护:用数组存储已识别的枢轴类型,布尔变量控制下一个要寻找的枢轴方向

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 07:01:15