Daniel
Daniel

Reputation: 51

Why does this return "na" as result?

I've tried working around the barssince function in conjunction with the valuewhen function so that I could know how many bars have passed from when the PL has changed to the previous PL but it always returns either 0 or na.

I've tried putting the formula in the same line as well

barssince(valuewhen(change(pl_ext)!=0, pl_ext, 2))

but this returns me 0 as a result.

Trying to put the actual value of the PL I was looking for seemed to work

barssince(pl_ext == 1.35885)

so I then tried to do the same but with plex2 but as soon as I added the variable it started to return na.

I don't understand why it's not giving me the result I am looking for. I can't wrap my head around it.

Any help would be greatly appreciated.

//@version=4

study("test")

pl = pivotlow(low , 10, 10)

var pl_ext = pl

if (pl)
    pl_ext := pl

plex2 = valuewhen(change(pl_ext)!=0, pl_ext, 2)

plot(barssince(pl_ext == plex2))

Upvotes: 0

Views: 599

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6875

You can just keep track of the bar_index when you hit a pivot low, and when you hit the next one, just substract it from the current bar_index to get the number of bars in between.

//@version=4
study("test")

var int prev_bar_index  = na
var int bar_count       = na

if pivotlow(low , 10, 10)
    bar_count      := bar_index - prev_bar_index
    prev_bar_index := bar_index

plot(bar_count, style=plot.style_stepline)

Which yields image

Upvotes: 2

Related Questions