如何访问最后的指标值

时间:2020-01-14 15:23:55

标签: pivot pine-script indicator trend

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

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

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

如何检查最后一个pH值是否高于之前的pH? (我想检查上升趋势或下降趋势)

1 个答案:

答案 0 :(得分:2)

您可以尝试以下代码(如您所见,包括原始代码):

//@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)