Addressing the previous value

Viewed 854

I would like to be able at present candle to use the value of the previous PH. Using the function 1 it would address the value of the previous bar which in this case would be the same as the present candle.

Thanks

ph = pivothigh(close, 50, 50)

var phe = 0.0
if ph
    phe := ph

plot(phe, "PH")

enter image description here

1 Answers

You can access the previous values using the built-in change + valuewhen functions and control the depth with occurrence argument as in the example below: 0 - current value, 1 - previous one etc.

//@version=4
study("My Script")

ph = pivothigh(close, 50, 50)

var phe = 0.0
if ph
    phe := ph
    
plot(phe, "PH")

phChanged = valuewhen(change(phe) != 0, phe,  1)
plot(phChanged, color = color.red)

phChangedClose = valuewhen(change(phe) != 0, close[1],  0)
plot(phChangedClose, color = color.orange)

enter image description here

Related