Why does this return "na" as result?
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