如何将以下 Higher High 和 Lower Low plotshape 与 pinescript 中的 RSI 线对齐?

How to align the following Higher High and Lower Low plotshape to RSI line in pinescript?

以下脚本是 RSI 和 Higher High 和 Lower Low 脚本的组合。问题是 HH LL 标签与不在 RSI 线上的价格对齐。如何将标签与 RSI 线对齐?它基本上显示了 RSI 的较高高点和较低低点。标签需要贴在相应的 RSI 线上。

//@version=4

study(title="RSI", shorttitle="RSI", format=format.price, precision=2)

// RSI
lenRSI = input(14, minval=1, title="Length")
srcRSI = input(close, "Source", type=input.source)

rsi = rsi(srcRSI, lenRSI)
plot(rsi, "RSI", color=color.gray, linewidth=1)

// Higher High Lower Low
lb = input(5, title="Left Bars", minval=1)
rb = input(5, title="Right Bars", minval=1)

mb = lb + rb + 1

ph = iff(not na(rsi[mb]), iff(highestbars(rsi, mb) == -lb, rsi[lb], na), na)  // Pivot High
pl = iff(not na(rsi[mb]), iff(lowestbars(rsi, mb) == -lb, rsi[lb], na), na)  // Pivot Low

hl = int(na)
hl := iff(ph, 1, iff(pl, -1, na))  // Trend direction
zz = float(na)
zz := iff(ph, ph, iff(pl, pl, na))  // similar to zigzag but may have multiple highs/lows
zz := iff(pl and hl == -1 and valuewhen(hl, hl, 1) == -1 and pl > valuewhen(zz, zz, 1), na, zz)
zz := iff(ph and hl == 1 and valuewhen(hl, hl, 1) == 1 and ph < valuewhen(zz, zz, 1), na, zz)

hl := iff(hl == -1 and valuewhen(hl, hl, 1) == 1 and zz > valuewhen(zz, zz, 1), na, hl)
hl := iff(hl == 1 and valuewhen(hl, hl, 1) == -1 and zz < valuewhen(zz, zz, 1), na, hl)
zz := iff(na(hl), na, zz)

findprevious() =>  // finds previous three points (b, c, d, e)
    ehl = iff(hl == 1, -1, 1)
    loc1 = 0.0
    loc2 = 0.0
    loc3 = 0.0
    loc4 = 0.0
    xx = 0
    for x = 1 to 1000 by 1
        if hl[x] == ehl and not na(zz[x])
            loc1 := zz[x]
            xx := x + 1
            break
    ehl := hl
    for x = xx to 1000 by 1
        if hl[x] == ehl and not na(zz[x])
            loc2 := zz[x]
            xx := x + 1
            break
    ehl := iff(hl == 1, -1, 1)
    for x = xx to 1000 by 1
        if hl[x] == ehl and not na(zz[x])
            loc3 := zz[x]
            xx := x + 1
            break
    ehl := hl
    for x = xx to 1000 by 1
        if hl[x] == ehl and not na(zz[x])
            loc4 := zz[x]
            break
    [loc1, loc2, loc3, loc4]

a = float(na)
b = float(na)
c = float(na)
d = float(na)
e = float(na)
if not na(hl)
    [loc1, loc2, loc3, loc4] = findprevious()
    a := zz
    b := loc1
    c := loc2
    d := loc3
    e := loc4
    e

_hh = zz and a > b and a > c and c > b and c > d
_ll = zz and a < b and a < c and c < b and c < d
_hl = zz and 
   (a >= c and b > c and b > d and d > c and d > e or a < b and a > c and b < d)
_lh = zz and 
   (a <= c and b < c and b < d and d < c and d < e or a > b and a < c and b > d)

plotshape(_hl, text="HL", title="Higher Low", style=shape.labelup, color=color.lime, textcolor=color.black, location=location.belowbar, offset=-lb)
plotshape(_hh, text="HH", title="Higher High", style=shape.labeldown, color=color.lime, textcolor=color.black, location=location.abovebar, offset=-lb)
plotshape(_ll, text="LL", title="Lower Low", style=shape.labelup, color=color.red, textcolor=color.white, location=location.belowbar, offset=-lb)
plotshape(_lh, text="LH", title="Lower High", style=shape.labeldown, color=color.red, textcolor=color.white, location=location.abovebar, offset=-lb)

location.belowbarlocation.abovebar 更改为 location.absolute 和图形显示(例如:如果 _hl 为真,则在 RSI 级别绘制,否则通过)

plotshape(_hl ? rsi :na, text="HL", title="Higher Low", style=shape.labelup, color=color.lime, textcolor=color.black, location=location.absolute, offset=-lb)
plotshape(_hh ? rsi :na, text="HH", title="Higher High", style=shape.labeldown, color=color.lime, textcolor=color.black, location=location.absolute, offset=-lb)
plotshape(_ll ? rsi :na, text="LL", title="Lower Low", style=shape.labelup, color=color.red, textcolor=color.white, location=location.absolute, offset=-lb)
plotshape(_lh ? rsi :na, text="LH", title="Lower High", style=shape.labeldown, color=color.red, textcolor=color.white, location=location.absolute, offset=-lb)