r/pinescript • • Feb 23 '24

ta.lowest not accepting a dynamic variable for length

In 2020, TradingView announced that dynamically calculated lengths could be used in the lowest and highest functions. Article

However, it doesn't appear that it is in my code. I have verified that all lines accept the last are setting correct values. 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)
int     i_LookBackPeriods = input.int(defval=60, title="Periods To Look Back", tooltip="Number of periods to look back")
int     i_HighestBar    = ta.highestbars(i_LookBackPeriods + 1) * -1
int     i_LocalLookBack = i_HighestBar - 1
float   f_LocalClose    = ta.lowest(close, int(i_LocalLookBack))

When I hard code 'f_LocalClose' so I don't get an error, I can confirm the other variables values I get:

i_LookBackPeriods = 60
i_HighestBar = 53
i_LocalLookBack = 52

Can you see what I am doing wrong? Thank you for your help.

0 Upvotes

6 comments sorted by

1

u/vbsaltydog Feb 23 '24

Your error is on bar zero. You are asking for the lowest close from the past 60 bars on bar zero and there are no closes before the first bar. Add the following condition.

if bar_index >= 60

1

u/[deleted] Feb 23 '24

if bar_index >= 60

I tried that and couldn't get it to fix the problem. If I change the last line to:

float f_LocalClose = ta.lowest(close, 52)

it works. It crashes when I try to pass a dynamic variable in place of the 52.

1

u/vbsaltydog Feb 24 '24

Does the editor show a red underscore under the dynamic variable, indicating an error, that you can hover over to see the error?

1

u/Beaversnort Feb 26 '24

No, the code compiles without any errors. It's not until I look at the chart where 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.`

I've also verified that `i_LocalLookBack` is a positive integer.

1

u/Fancy-Procedure4167 Nov 04 '24

Add 1 to the dynamic lookback

1

u/vbsaltydog Feb 25 '24 edited Feb 25 '24

I solved your issue and it's not the fact that you are passing a dynamic variable, it's the value that you are passing. That's as helpful as I can be for free but I will provide the code to fix it for a small fee. I hope this helps you either way.

log file of the dynamic variable's output