Pine Editor新手求助:去除高低点指标绘图偏移获取精准点位
解决Pine Editor中无需offset的高低点精准识别问题
要实现不依赖offset参数、精准定位到对应K线的高低点识别,核心是在右侧所需数量的K线走完后,回溯确认目标K线是否符合高低点条件——内置的ta.pivothigh/ta.pivotlow返回值默认出现在最后一根右侧K线上,所以才需要偏移,我们可以通过手动判断区间极值来避开这个问题。
以下是修改后的代码,直接在高低点对应的K线位置生成信号,无需偏移:
//@version=5 indicator("Test_H_L", overlay=true) leftBars = input(3) rightBars=input(3) // 确认当前K线是否是左侧leftBars、右侧rightBars范围内的有效高点 isPivotHigh = ta.highest(high, leftBars + rightBars + 1) == high and ta.barssince(ta.highest(high, leftBars + 1)) == rightBars // 确认当前K线是否是左侧leftBars、右侧rightBars范围内的有效低点 isPivotLow = ta.lowest(low, leftBars + rightBars + 1) == low and ta.barssince(ta.lowest(low, leftBars + 1)) == rightBars // 提取精准的高低点价格(仅在对应K线有值,其余为na) phPrice = isPivotHigh ? high : na plPrice = isPivotLow ? low : na // 绘制高低点,无需offset参数 plot(phPrice, style=plot.style_circles, linewidth=4, color=color.green) plot(plPrice, style=plot.style_circles, linewidth=4, color=color.red) // 示例:存入线数组的用法(无点位偏差) var line[] highLines = array.new_line() if isPivotHigh newLine = line.new(bar_index, phPrice, bar_index + 20, phPrice, color=color.green, width=2) array.push(highLines, newLine)
关键逻辑说明
ta.highest(high, leftBars + rightBars + 1):取包含当前K线在内的「左3+右3+自身」共7根K线的最高值,判断当前K线是否是该区间的最高点ta.barssince(ta.highest(high, leftBars + 1)) == rightBars:确保当前K线左侧3根K线的最高值都低于它,且已经走完右侧3根K线,此时才能确认这是有效高点- 低点判断逻辑与高点完全对称,最终生成的价格变量直接对应高低点所在的K线,用于线数组时不会有偏移导致的点位偏差
内容的提问来源于stack exchange,提问作者Javier Franco Lineros
相关产品推荐
相关产品推荐

