问题描述
利用此脚本,我需要将当前价格与最新的HH / HL / LH / LL进行比较,并为每个差异创建标签。 if barstate.islast
需要将最后一个柱状图与之前的HH / HL / LH / LL进行比较,并且不确定如何在脚本的最后一次执行时访问它们的值。为此创建解决方案的最佳方法是什么?
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,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,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,0)
higherhigh = na(pvthi) ? na : valuewhen_1 < valuewhen_2 ? pvthi : na
valuewhen_3 = valuewhen(pvthi,1)
valuewhen_4 = valuewhen(pvthi,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,1)
valuewhen_8 = valuewhen(pvtlo,0)
lowerlow = na(pvtlo) ? na : valuewhen_7 > valuewhen_8 ? pvtlo : na
// 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,text="HL",offset=-pvtLenR)
plotshape(ShowHHLL ? lowerhigh : na,title='LH',color=color.new(color.red,text="LH",offset=-pvtLenR)
plotshape(ShowHHLL ? lowerlow : na,title='LL',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',color=color.red,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,title="Top Levels",style=plot.style_circles)
plot(ShowSRLevels and not ShowChannel and (maxLvlLen == 0 or countlo < maxLvlLen) ? pvtlos : na,color=lopc,title="Bottom Levels",style=plot.style_circles)
// Show Levels as a Fractal Chaos Channel
plot(ShowSRLevels and ShowChannel ? pvthis : na,style=plot.style_stepline,offset=0,title="Top Chaos Channel",trackprice=false)
plot(ShowSRLevels and ShowChannel ? pvtlos : na,title="Bottom Chaos Channel",trackprice=false)
// //
plotshape(ShowFB and buy?1:na,title="BUY Arrow",style=shape.labelup,location =location.belowbar)
plotshape(ShowFB and sell?-1:na,title="SELL Arrow",style=shape.labeldown,location =location.abovebar)
解决方法
您可以使用内置的fixnan
函数用最后一个非null值连续填充变量。
float fixedHH = fixnan(higherhigh)
// add offset = -pvtLenR to move the plot to the left and match the HH points.
plot(fixedHH)
然后将该变量与当前价格进行比较,计算差额等。
bool lowerThanHH = close < fixedHH
float closeHHDiff = abs(fixedHH - close)
最后将差异传递给label.new
函数:
if barstate.islast
label.new(bar_index,high + 3*tr,tostring(closeHHDiff),xloc.bar_index,color = color.gray,style = label.style_label_down)
要不重复相同的代码,您可以像上面的示例一样创建一个函数并将其应用于HL,LL或LH,但这是另一个问题。