When I use a dynamic value in the variable for the length argument in the ta.lowest(source, length) function it does not work. If I hard code the value it does. In 2020, TradingView announced that dynamically calculated lengths could be used in the lowest function, article . However, I can't get it to work. The code compiles without errors, but on the chart I get the red circle exclamation point with the message:
Study Error: Error on bar 0: Invalid value of the 'length' argument (0) in the lowest function. It must be > 0.
// @version = 5
indicator(title="Fibonacci Retracement", shorttitle="Fib Retrace", overlay = true)
i_LookBackPeriods = input.int(defval=60, title="Periods To Look Back", group="Options")
i_HighestBar = ta.highestbars(i_LookBackPeriods + 1) * -1
i_LocalLookBack = i_HighestBar - 1
f_LocalClose = ta.lowest(close, i_LocalLookBack)
The above code does not work, but if I change i_LocalLookBack to the following it does.
i_LocalLookBack = 52
Can you see what I am doing wrong? Thank you for your help.
The mistake here is two-fold:
The first bar given by the ta.highestbars()
function will be a na value. This is causing the Error on bar 0
error.
The calculated length of the i_LocalLookBack
variable becomes less than zero. This is causing the It must be > 0
error.
The code can be fixed by the following changes:
// @version = 5
indicator(title="Fibonacci Retracement", shorttitle="Fib Retrace", overlay = true)
i_LookBackPeriods = input.int(defval=60, title="Periods To Look Back", group="Options")
i_HighestBar = nz(ta.highestbars(i_LookBackPeriods + 1) * -1)
i_LocalLookBack = i_HighestBar - 1 <= 0 ? 1 : i_HighestBar - 1
f_LocalClose = ta.lowest(close, i_LocalLookBack)
Explanation:
The nz()
function around the ta.highestbars()
function will prevent the Error on bar 0
error.
Making it so that i_HighestBar - 1
becomes 1 whenever it is less than zero, prevents the It must be > 0
error.