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

迭代检测高低点(无回溯/范围):PineScript高点识别脚本修正

修正PineScript脚本:正确标记Higher High高点K线

需求与问题

  • 需求:禁止使用pivothigh/highest等内置函数,从指定起始K线开始,通过迭代识别Higher High(当前K线高点 > 前一根高点,且当前K线高点 > 后一根高点),后续需按高点差筛选无效标记。
  • 当前问题:脚本错误标记了高点之后的K线,需改为标记真正的高点K线。

原代码问题

  1. 循环检测到高点时,错误将标记位置设为i+1,导致标记点偏移到高点的下一根K线。
  2. 单个hh变量会被循环迭代覆盖,无法保留所有符合条件的高点标记。
  3. plotshape(hh)的调用方式不符合PineScript语法,无法正确渲染多个标记。

修正代码

//@version=5
indicator("My script", overlay=true)

lineDate = input.time(timestamp("22 Feb 2023 18:00"), title="Line Location")

// 绘制起始参考线
if barstate.islastconfirmedhistory
    line.new(x1=lineDate, y1=high, x2=lineDate, y2=low, extend=extend.both, 
         color=color.orange, width=2, xloc=xloc.bar_time)

// 定位起始K线
start_trigger = time == lineDate
bars_since_start = ta.barssince(start_trigger)
is_post_start = bars_since_start != na  // 标记起始线后的K线

// 初始化布尔数组存储高点标记
var bool[] hh_markers = array.new_bool()

// 迭代处理每一根K线(需至少前后各一根K线数据)
for i = 0 to bar_index
    // 跳过起始线之前的K线
    if not is_post_start[i]
        array.push(hh_markers, false)
        continue
    
    // 边界处理:跳过起始线后第一根和最新一根K线(无足够前后数据)
    if i == 0 or i == bar_index
        array.push(hh_markers, false)
        continue
    
    current_high = high[i]
    prev_high = high[i+1]
    next_high = high[i-1]
    
    // 判断当前K线是否为Higher High
    is_hh = current_high > prev_high and current_high > next_high
    array.push(hh_markers, is_hh)

// 获取当前K线的高点标记状态
current_hh = array.get(hh_markers, bar_index)

// 绘制红色圆点标记高点
plotshape(current_hh, style=shape.circle, location=location.abovebar, color=color.red, size=size.small)

修正说明

  1. 精准标记高点:调整迭代逻辑,直接判断i位置K线的高低关系,确保标记点落在高点K线本身。
  2. 保留所有标记:使用数组hh_markers存储每根K线的标记状态,避免迭代覆盖导致丢失之前的高点。
  3. 边界处理:跳过起始线前、起始线后第一根和最新一根K线,确保判断时有完整的前后K线数据。
  4. 正确渲染标记:通过array.get获取当前K线的标记状态,用plotshape在高点位置绘制红色圆点。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 09:43:34