'How to find previous lower value
I am trying to find a way to get the value of an indicator when it was previously lower than the present value. For this example, I'm trying to extract the value of the previous lower pivot low.
My first idea was to use the [] function with the formula PL[1] > PL ? na : PL[1] which works well when PL #2 & #3 are created and return the value of PL #1 & #2 respectively, but obviously, on PL #4 it returns na. Changing the formula to PL[1] > PL ? PL[2] : PL[1] would work on PL #4 but I would have to create a long list of conditions in case the following PLs are all lower than the previous one. Furthermore, it would become an issue when PL #5 is created, as at this point I would no longer be interested in PL #2 (as PL #5 is lower than PL #2) but I would like the value of PL #1 to be returned instead.
I've tried then to use arrays and was thinking of using array.indexof() function to get the index of the last time the condition was met and then use the function array.get() to get the float value, but I'm not sure how to express in the array.indexof() function what I'm looking for, as the value argument has to be float.
Maybe I'm thinking of this the wrong way. Any help would be greatly appreciated.
//@version=4
study(title = "test", overlay = true)
pl = pivotlow(low, 40, 40)
var pl_ext = pl
if (pl)
pl_ext := pl
plex1 = valuewhen(change(pl_ext)!=0, pl_ext, 1)
plot(pl_ext, color = color.red, title="Pivot Low")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

