如何为Pinescript V5的全部历史枢轴点添加标签?
Pinescript V5 枢轴点历史标签显示解决方案
问题描述
编写了一段计算枢轴点的Pinescript V5脚本,当前仅显示最后一个枢轴点的标签,希望为所有历史枢轴点添加标签,但取消show_last参数后出现严重文本重叠问题。
原始代码如下:
//@version=5 indicator(title='Pivot Point', shorttitle='Pivot Point', overlay=true) timeframe = input('D', title='Timeframe') width = input.int(2, minval=1) xHigh = request.security(syminfo.ticker,timeframe, high[1], barmerge.gaps_off, barmerge.lookahead_on) xLow = request.security(syminfo.ticker,timeframe, low[1], barmerge.gaps_off, barmerge.lookahead_on) xClose = request.security(syminfo.ticker,timeframe, close[1], barmerge.gaps_off, barmerge.lookahead_on) xOpen = request.security(syminfo.ticker, timeframe, open[1], barmerge.gaps_off, barmerge.lookahead_on) X = close vBC = close vPP = close vTC = close vR1 = close vS1 = close vR2 = close vS2 = close vR3 = close vS3 = close vR4 = close vS4 = close vPP := (xHigh + xLow + xClose) / 3 vBC := (xHigh + xLow) / 2.0 vTC := vPP - vBC + vPP vR1 := vPP + vPP - xLow vS1 := vPP - (xHigh - vPP) vR2 := vPP + xHigh - xLow vS2 := vPP - (xHigh - xLow) vR3 := xHigh + 2 * (vPP - xLow) vS3 := xLow - 2 * (xHigh - vPP) plot(vTC, color=color.new(#ffffff, 0), title='TC', style=plot.style_circles, linewidth=width) plot(vPP, color=color.new(#ffffff, 0), title='PP', style=plot.style_circles, linewidth=width) plot(vBC, color=color.new(#ffffff, 0), title='BC', style=plot.style_circles, linewidth=width) plot(vS1, color=color.new(#ff88a5, 0), title='S1', style=plot.style_circles, linewidth=width) plot(vS2, color=color.new(#ff6309, 0), title='S2', style=plot.style_circles, linewidth=width) plot(vS3, color=color.new(#ff0000, 0), title='S3', style=plot.style_circles, linewidth=width) plot(vR1, color=color.new(#aef6ff, 0), title='R1', style=plot.style_circles, linewidth=width) plot(vR2, color=color.new(#14b6df, 0), title='R2', style=plot.style_circles, linewidth=width) plot(vR3, color=color.new(#6164ff, 0), title='R3', style=plot.style_circles, linewidth=width) plotshape(vPP, color=na, text="Std_PP", textcolor=color.new(#ffffff, 0), location=location.absolute, show_last=1, title="Fibonacci Text, don't change", offset=-1) plotshape(vR1, color=na, text="Std_R1", textcolor=color.new(#aef6ff, 0), location=location.absolute, show_last=1, title="Fibonacci Text, don't change", offset=-1) plotshape(vR2, color=na, text="Std_R2", textcolor=color.new(#14b6df, 0), location=location.absolute, show_last=1, title="Fibonacci Text, don't change", offset=-1) plotshape(vR3, color=na, text="Std_R3", textcolor=color.new(#6164ff, 0), location=location.absolute, show_last=1, title="Fibonacci Text, don't change", offset=-1) plotshape(vS1, color=na, text="Std_S1", textcolor=color.new(#ff88a5, 0), location=location.absolute, show_last=1, title="Fibonacci Text, don't change", offset=-1) plotshape(vS2, color=na, text="Std_S2", textcolor=color.new(#ff6309, 0), location=location.absolute, show_last=1, title="Fibonacci Text, don't change", offset=-1) plotshape(vS3, color=na, text="Std_S3", textcolor=color.new(#ff0000, 0), location=location.absolute, show_last=1, title="Fibonacci Text, don't change", offset=-1)
解决方案
核心思路是仅在目标时间周期切换时生成枢轴点标签,避免每个K线重复绘制导致重叠。使用label.new()替代plotshape,并通过周期切换条件控制标签创建时机。
修改后的完整代码:
//@version=5 indicator(title='Pivot Point', shorttitle='Pivot Point', overlay=true) timeframe = input('D', title='Timeframe') width = input.int(2, minval=1) // 获取目标时间周期前一根K线的高低收数据 xHigh = request.security(syminfo.ticker, timeframe, high[1], barmerge.gaps_off, barmerge.lookahead_on) xLow = request.security(syminfo.ticker, timeframe, low[1], barmerge.gaps_off, barmerge.lookahead_on) xClose = request.security(syminfo.ticker, timeframe, close[1], barmerge.gaps_off, barmerge.lookahead_on) // 计算枢轴点各级别数值 vPP = (xHigh + xLow + xClose) / 3 vBC = (xHigh + xLow) / 2.0 vTC = vPP - vBC + vPP vR1 = vPP + vPP - xLow vS1 = vPP - (xHigh - vPP) vR2 = vPP + xHigh - xLow vS2 = vPP - (xHigh - xLow) vR3 = xHigh + 2 * (vPP - xLow) vS3 = xLow - 2 * (xHigh - vPP) // 绘制枢轴点标记 plot(vTC, color=color.new(#ffffff, 0), title='TC', style=plot.style_circles, linewidth=width) plot(vPP, color=color.new(#ffffff, 0), title='PP', style=plot.style_circles, linewidth=width) plot(vBC, color=color.new(#ffffff, 0), title='BC', style=plot.style_circles, linewidth=width) plot(vS1, color=color.new(#ff88a5, 0), title='S1', style=plot.style_circles, linewidth=width) plot(vS2, color=color.new(#ff6309, 0), title='S2', style=plot.style_circles, linewidth=width) plot(vS3, color=color.new(#ff0000, 0), title='S3', style=plot.style_circles, linewidth=width) plot(vR1, color=color.new(#aef6ff, 0), title='R1', style=plot.style_circles, linewidth=width) plot(vR2, color=color.new(#14b6df, 0), title='R2', style=plot.style_circles, linewidth=width) plot(vR3, color=color.new(#6164ff, 0), title='R3', style=plot.style_circles, linewidth=width) // 检测目标时间周期的K线切换 isNewPeriod = ta.change(timeframe.period) // 仅在新周期开始时创建标签 if isNewPeriod label.new(bar_index, vPP, "Std_PP", color=na, textcolor=color.new(#ffffff, 0), style=label.style_none) label.new(bar_index, vR1, "Std_R1", color=na, textcolor=color.new(#aef6ff, 0), style=label.style_none) label.new(bar_index, vR2, "Std_R2", color=na, textcolor=color.new(#14b6df, 0), style=label.style_none) label.new(bar_index, vR3, "Std_R3", color=na, textcolor=color.new(#6164ff, 0), style=label.style_none) label.new(bar_index, vS1, "Std_S1", color=na, textcolor=color.new(#ff88a5, 0), style=label.style_none) label.new(bar_index, vS2, "Std_S2", color=na, textcolor=color.new(#ff6309, 0), style=label.style_none) label.new(bar_index, vS3, "Std_S3", color=na, textcolor=color.new(#ff0000, 0), style=label.style_none)
关键修改点:
- 移除冗余初始化:删除
X = close等无意义的变量初始化语句,简化代码。 - 周期切换检测:用
ta.change(timeframe.period)判断是否进入新的目标周期,确保仅在周期切换时生成标签。 - 替换plotshape为label.new:
plotshape会在每个K线重复绘制,而label.new仅在满足条件时创建一次标签,彻底解决重叠问题。 - 取消show_last限制:通过条件判断精准控制标签生成时机,无需依赖
show_last参数限制显示数量。
这样修改后,每个新的目标周期(如日线)只会生成一组枢轴点标签,既保留了所有历史枢轴的标记,又不会出现文本重叠。
内容的提问来源于stack exchange,提问作者Jovan
相关产品推荐
相关产品推荐

