如何访问最后的指标值

How to access the last indicator values

我有一个显示枢轴点的指标:

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

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

如何检查上次的 ph 值是否高于之前的 ph 值? (我想检查上升趋势或下降趋势)

你可以试试下面的代码(包括你原来的,如你所见):

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

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

//INIT VARIABLES
var int ph_uptrend_flag = 0
var float ph_valid = 0
var float ph_valid_old = 1e99 // use any very high non meaningful number here for initialization only

ph_non_na = nz(ph,0) // stores 0's instead of na's for non-pivot-pointed bars

// re-calculate uptrend flag every time a new pivot comes in, otherwise keep last known value for uptrend flag
if ph_non_na != 0
    ph_valid := ph_non_na
    ph_uptrend_flag := ph_valid > ph_valid_old ? 1 : 0
    ph_valid_old := ph_valid
else
    ph_valid := ph_valid
    ph_valid_old := ph_valid_old

//plot uptrend flag and mark background accordingly
plot(ph_uptrend_flag,title="ph uptrend indication",linewidth=4,color=color.white)

//plot(ph,title="ph value",color=color.lime,style=8) //plot ph values (better used with overlay=true)