基于PineScript的最新HH/HL/LH/LL与当前价格对比方案问询
解决方案:获取最近HH/HL/LH/LL并在最后K线显示差值标签
针对你的需求,核心思路是通过持久化存储关键价格+在最后K线触发计算来实现。下面我会一步步拆解实现逻辑,并给出完整的修改后脚本。
关键实现逻辑
1. 持久化存储最近的形态价格
PineScript里的var关键字可以让变量在脚本运行全程保留值,不会每根K线都重置。我们用它来存储最近一次出现的HH、HL、LH、LL对应的价格:
// 初始化持久化变量,存储最近的形态价格 var float lastHHPrice = na // 最近Higher High的价格 var float lastHLPrice = na // 最近Higher Low的价格 var float lastLHPrice = na // 最近Lower High的价格 var float lastLLPrice = na // 最近Lower Low的价格
2. 实时更新存储的价格
当检测到HH/HL/LH/LL形态出现时,把对应的价格赋值给上面的变量。注意原脚本中higherhigh这类形态对应的实际价格是high[pvtLenR](因为形态标记偏移了-pvtLenR):
// 每次形态出现时更新对应价格 if not na(higherhigh) lastHHPrice := high[pvtLenR] if not na(higherlow) lastHLPrice := low[pvtLenR] if not na(lowerhigh) lastLHPrice := high[pvtLenR] if not na(lowerlow) lastLLPrice := low[pvtLenR]
3. 在最后K线计算差值并创建标签
在barstate.islast(最后一根K线)状态下,获取当前价格,计算与存储值的差值,再用label.new生成差值标签。同时处理na的情况(比如还未出现过某形态时不显示对应标签):
if barstate.islast currentPrice = close // 用最后一根K线收盘价,实时行情下会自动更新为最新价 atrVal = ta.atr(10) // 用ATR调整标签垂直间距,避免重叠 // HH差值标签 if not na(lastHHPrice) diffHH = currentPrice - lastHHPrice label.new(bar_index, high + atrVal*0.5, "HH Diff: " + str.tostring(diffHH, "0.00"), color=color.new(color.green, 30), textcolor=color.white, style=label.style_label_down, size=size.small) // HL差值标签 if not na(lastHLPrice) diffHL = currentPrice - lastHLPrice label.new(bar_index, high + atrVal*0.5 - atrVal, "HL Diff: " + str.tostring(diffHL, "0.00"), color=color.new(color.green, 30), textcolor=color.white, style=label.style_label_down, size=size.small) // LH差值标签 if not na(lastLHPrice) diffLH = currentPrice - lastLHPrice label.new(bar_index, high + atrVal*0.5 - 2*atrVal, "LH Diff: " + str.tostring(diffLH, "0.00"), color=color.new(color.red, 30), textcolor=color.white, style=label.style_label_down, size=size.small) // LL差值标签 if not na(lastLLPrice) diffLL = currentPrice - lastLLPrice label.new(bar_index, high + atrVal*0.5 - 3*atrVal, "LL Diff: " + str.tostring(diffLL, "0.00"), color=color.new(color.red, 30), textcolor=color.white, style=label.style_label_down, size=size.small)
完整修改后的脚本
study(title="Pivot Points High Low (HH/HL/LH/LL) [Anan] ", shorttitle="Pivots HL [Anan]", overlay=true) // - INPUTS ShowPivots = input(true, title="Show Pivot Points") ShowHHLL = input(true, title="Show HH,LL,LH,HL markers on Pivots Points") left = input(5, minval=1, title="Pivot Length Left Hand Side") right = input(5, minval=1, title="Pivot Length Right Hand Side") ShowSRLevels = input(true, title="Show S/R Level Extensions") maxLvlLen = input(0, minval=0, title="Maximum S/R Level Extension Length (0 = Max)") ShowChannel = input(false, title="Show Levels as a Fractal Chaos Channel") // ShowFB = input(true, title="Show Fractal Break Alert Arrows") // Determine pivots pvtLenL = left pvtLenR = right // Get High and Low Pivot Points pvthi_ = pivothigh(high, pvtLenL, pvtLenR) pvtlo_ = pivotlow(low, pvtLenL, pvtLenR) // Force Pivot completion before plotting. pvthi = pvthi_ pvtlo = pvtlo_ // ||-----------------------------------------------------------------------------------------------------|| // ||--- Higher Highs, Lower Highs, Higher Lows, Lower Lows -------------------------------------------|| valuewhen_1 = valuewhen(pvthi, high[pvtLenR], 1) valuewhen_2 = valuewhen(pvthi, high[pvtLenR], 0) higherhigh = na(pvthi)? na : valuewhen_1 < valuewhen_2? pvthi : na valuewhen_3 = valuewhen(pvthi, high[pvtLenR], 1) valuewhen_4 = valuewhen(pvthi, high[pvtLenR], 0) lowerhigh = na(pvthi)? na : valuewhen_3 > valuewhen_4? pvthi : na valuewhen_5 = valuewhen(pvtlo, low[pvtLenR], 1) valuewhen_6 = valuewhen(pvtlo, low[pvtLenR ], 0) higherlow = na(pvtlo)? na : valuewhen_5 < valuewhen_6? pvtlo : na valuewhen_7 = valuewhen(pvtlo, low[pvtLenR], 1) valuewhen_8 = valuewhen(pvtlo, low[pvtLenR ], 0) lowerlow = na(pvtlo)? na : valuewhen_7 > valuewhen_8? pvtlo : na // -------------------------- 新增:存储最近的HH/HL/LH/LL价格 -------------------------- var float lastHHPrice = na var float lastHLPrice = na var float lastLHPrice = na var float lastLLPrice = na // 更新价格变量 if not na(higherhigh) lastHHPrice := high[pvtLenR] if not na(higherlow) lastHLPrice := low[pvtLenR] if not na(lowerhigh) lastLHPrice := high[pvtLenR] if not na(lowerlow) lastLLPrice := low[pvtLenR] // -------------------------- 新增:最后K线显示差值标签 -------------------------- if barstate.islast currentPrice = close atrVal = ta.atr(10) // 用于调整标签垂直间距 // HH差值标签 if not na(lastHHPrice) diffHH = currentPrice - lastHHPrice label.new(bar_index, high + atrVal*0.5, "HH Diff: " + str.tostring(diffHH, "0.00"), color=color.new(color.green, 30), textcolor=color.white, style=label.style_label_down, size=size.small) // HL差值标签 if not na(lastHLPrice) diffHL = currentPrice - lastHLPrice label.new(bar_index, high + atrVal*0.5 - atrVal, "HL Diff: " + str.tostring(diffHL, "0.00"), color=color.new(color.green, 30), textcolor=color.white, style=label.style_label_down, size=size.small) // LH差值标签 if not na(lastLHPrice) diffLH = currentPrice - lastLHPrice label.new(bar_index, high + atrVal*0.5 - 2*atrVal, "LH Diff: " + str.tostring(diffLH, "0.00"), color=color.new(color.red, 30), textcolor=color.white, style=label.style_label_down, size=size.small) // LL差值标签 if not na(lastLLPrice) diffLL = currentPrice - lastLLPrice label.new(bar_index, high + atrVal*0.5 - 3*atrVal, "LL Diff: " + str.tostring(diffLL, "0.00"), color=color.new(color.red, 30), textcolor=color.white, style=label.style_label_down, size=size.small) // -------------------------- 原脚本其他部分 -------------------------- // If selected Display the HH/LL above/below candle. plotshape(ShowHHLL? higherhigh : na, title='HH', style=shape.triangledown, location=location.abovebar, color=color.new(color.green,50), text="HH", offset=-pvtLenR) plotshape(ShowHHLL? higherlow : na, title='HL', style=shape.triangleup, location=location.belowbar, color=color.new(color.green,50), text="HL", offset=-pvtLenR) plotshape(ShowHHLL? lowerhigh : na, title='LH', style=shape.triangledown, location=location.abovebar, color=color.new(color.red,50), text="LH", offset=-pvtLenR) plotshape(ShowHHLL? lowerlow : na, title='LL', style=shape.triangleup, location=location.belowbar, color=color.new(color.red,50), text="LL", offset=-pvtLenR) plot(ShowPivots and not ShowHHLL? pvthi : na, title='High Pivot', style=plot.style_circles, join=false, color=color.green, offset=-pvtLenR, linewidth=3) plot(ShowPivots and not ShowHHLL? pvtlo : na, title='Low Pivot', style=plot.style_circles, join=false, color=color.red, offset=-pvtLenR, linewidth=3) //Count How many candles for current Pivot Level, If new reset. counthi = 0 countlo = 0 counthi := na(pvthi)? nz(counthi[1]) + 1 : 0 countlo := na(pvtlo)? nz(countlo[1]) + 1 : 0 pvthis = 0.0 pvtlos = 0.0 pvthis := na(pvthi)? pvthis[1] : high[pvtLenR] pvtlos := na(pvtlo)? pvtlos[1] : low[pvtLenR] hipc = pvthis!= pvthis[1]? na : color.new(color.red,50) lopc = pvtlos!= pvtlos[1]? na : color.new(color.green,50) // Show Levels if Selected plot(ShowSRLevels and not ShowChannel and (maxLvlLen == 0 or counthi < maxLvlLen)? pvthis : na, color=hipc, linewidth=1, offset=-pvtLenR, title="Top Levels",style=plot.style_circles) plot(ShowSRLevels and not ShowChannel and (maxLvlLen == 0 or countlo < maxLvlLen)? pvtlos : na, color=lopc, linewidth=1, offset=-pvtLenR, title="Bottom Levels",style=plot.style_circles) // Show Levels as a Fractal Chaos Channel plot(ShowSRLevels and ShowChannel? pvthis : na, color=color.green, linewidth=1, style=plot.style_stepline, offset=0, title="Top Chaos Channel", trackprice=false) plot(ShowSRLevels and ShowChannel? pvtlos : na, color=color.red, linewidth=1, style=plot.style_stepline, offset=0, title="Bottom Chaos Channel", trackprice=false) // // plotshape(ShowFB and buy?1:na, title="BUY Arrow", color=color.green, style=shape.labelup,location =location.belowbar) // plotshape(ShowFB and sell?-1:na, title="SELL Arrow", color=color.red, style=shape.labeldown,location =location.abovebar)
额外调整建议
- 如果你需要实时最新价格(而非最后K线收盘价),直接保留
currentPrice = close即可,PineScript在实时行情中会自动更新close为最新报价。 - 标签的位置、颜色、小数位数都可以自定义:比如
str.tostring(diffHH, "0.00")可以改成"0.0"减少小数位,size.small改成size.normal放大标签。 - 如果想要标签显示在K线下方,只需要修改
label.new的y参数为low - atrVal*0.5这类值即可。
内容的提问来源于stack exchange,提问作者nolanjacobson
相关产品推荐
相关产品推荐

