cody
cody

Reputation: 6941

How to determine the last indicator value

I've got an indicator which draws pivot highs and lows on a chart:

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

How can I determine which of both happened at last (to set that as current indicator value)?

Upvotes: 0

Views: 608

Answers (1)

vitruvius
vitruvius

Reputation: 21121

You can use a variable for this purpose. pivothigh() and pivotlow() return NaN if there is no pivot at that point. So, you can check if the value is not Nan, which means there is a new pivot point.

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

var lastPivot = 0    // Use "var" so it will keep its latest value
ph = pivothigh(high, leftBars, rightBars)
pl = pivotlow(low, leftBars, rightBars)

if (not na(ph))      // New pivot high
    lastPivot := 1
else
    if (not na(pl))  // New pivot low
        lastPivot := -1

// Now, you can check lastPivot's value to see what type of pivot you had last
// lastPivot = 1  -> Pivot high
// lastPivot = -1 -> Pivot low

plot(series=lastPivot, color=color.red, linewidth=2)

enter image description here

As you can see, lastPivot's value alternates between 1 and -1 and its value stays the same until there is a new pivot.

Upvotes: 2

Related Questions