如何确定最后一个指标值

How to determine the last indicator value

我有一个指标可以在图表上绘制枢轴高点和低点:

leftBars  = input(3)
rightBars = input(3)
ph = pivothigh(high, leftBars, rightBars)
pl = pivotlow(low, leftBars, rightBars)

如何确定最后发生的两者中的哪一个(将其设置为当前指标值)?

您可以为此目的使用变量。 pivothigh()pivotlow() return NaN 如果在该点没有枢轴。因此,您可以检查该值是否不是 Nan,这意味着有一个新的轴心点。

//@version=4
study("Find last pivot", overlay=false)
leftBars  = input(3)
rightBars = input(3)

var lastPivot = 0    // Use "var" so it will keep its latest value
ph = pivothigh(high, leftBars, rightBars)
pl = pivotlow(low, leftBars, rightBars)

if (not na(ph))      // New pivot high
    lastPivot := 1
else
    if (not na(pl))  // New pivot low
        lastPivot := -1

// Now, you can check lastPivot's value to see what type of pivot you had last
// lastPivot = 1  -> Pivot high
// lastPivot = -1 -> Pivot low

plot(series=lastPivot, color=color.red, linewidth=2)

如您所见,lastPivot 的值在 1 和 -1 之间交替,并且它的值保持不变,直到有一个新的主元。