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

如何在Pine Script中检测K线收盘价高于Pivot高点或低于Pivot低点?

问题根因

pivothigh()、pivotlow()两个函数的返回值特性是仅在Pivot点确认的那根K线上返回有效高低价,其余所有K线的返回值都是na,你直接用close > phigh做判断时,除了Pivot点确认的那根K线外,其余所有K线的判断条件都会因为存在na返回false,自然不会生成突破标签。

修复方案

你需要先提取并持久化最近一次确认的Pivot高点、低点数值,再用这个固定值和后续K线的收盘价做突破判断,修正后代码如下:

//@version=4
study("Test", overlay=true)

// Pivot points参数定义
leftbars = input(7)
rightbars = input(7)

// 计算Pivot高低点
phigh = pivothigh(high, leftbars, rightbars)
plow = pivotlow(low, leftbars, rightbars)

// 提取最近一次确认的Pivot高低点数值
latest_ph = valuewhen(phigh, high[rightbars], 0)
latest_pl = valuewhen(plow, low[rightbars], 0)

// 标注Pivot点位置
label1 = phigh ? label.new(bar_index[rightbars], high[rightbars], text=tostring(phigh), style=label.style_labeldown, color=color.white) : na
label2 = plow ? label.new(bar_index[rightbars], low[rightbars], text=tostring(plow), style=label.style_labelup, color=color.white) : na

// 突破判断:用crossover/crossunder仅在首次突破时标注,避免连续符合条件的K线重复打标签
if ta.crossover(close, latest_ph)
    label.new(bar_index, close, text="突破Pivot高点", color=color.green, style=label.style_label_up)

if ta.crossunder(close, latest_pl)
    label.new(bar_index, close, text="跌破Pivot低点", color=color.red, style=label.style_label_down)

补充说明

  • valuewhen(条件, 取值, 序号) 函数的作用是:取最近第N次满足条件时的对应数值,这里我们取最近一次Pivot高点确认时的对应K线高价作为后续判断的基准
  • 如果你需要所有收盘价高于Pivot高点的K线都标注,把ta.crossover()替换成普通的close > latest_ph即可
  • Pivot点本身需要等待rightbars根K线走完才会确认,因此不会出现“提前判断未确认的Pivot点突破”的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 07:48:01