How to access the last indicator values

老子叫甜甜 提交于 2021-01-29 10:01:15

问题


I've got an indicator which shows pivot points:

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

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

How can I check if the last ph was higher than the ph before? (I'd like to check for an uptrend or downtrend)


回答1:


You can try the following code (including your original, as you see):

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


来源:https://stackoverflow.com/questions/59736834/how-to-access-the-last-indicator-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!