Coding my first complex indicator, I finally encountered this error for the first time. My code is indeed big with many if, while and for statements. However, trying to figure out how to fix this issue, I noticed the counter is really... misbehaving, so to speak. For example, I barely made any code changes, yet suddenly it told me I have like 518 out of 500 if statements, while I haven't introduced that many for sure. I merely changed one function where all I did was, like, introduce a ?: conditional into a label.new's title.
However, when I started removing or replacing ifs with switches or ?: conditionals i started noticing it go down seemingly in line with what I was doing (remove 2 ifs - counter goes from 508 to 506).
But then I have counted all uses of if, while and for in my code, and it turned out I have 229, 7 and 16?..... This doesn't amount to 500 at all?....
So I decided to investigate.
This post Pine script error-add to chart operation failed, reason: script has too many local scopes:521. the limit is 500 talks about adding 500 "if ticker= something do something" statements, abeit in pine v4. And that would produce an error. I've tried replicating it and it doesn't work!
I've tried up to 1000 of statements like
if true
na
That didn't trigger any error. I've decided that's too simple and pine somehow inlines that or whatever, so I did complex things like
a = true
b = false
if a == b
b := not b
var localvar = 0
b := b or false
else
b := true
And then copied the if else block 1000 times. No error! Note this is a multi-line function, even declaring a var inside, so it should require a local block by definition?
So... what actually counts as a local scope and what does not? Can anyone please clarify?
EDIT:
Code #1 https://pastebin.com/g5qwxp7r
Okay now this code will give 552 local scopes error.
Code #2 https://pastebin.com/p7vU2sQ8
Same code but it doesn't give any errors.
Difference?
I have removed 5 calls of array.push on line 1421! Now can someone explain how removing these 5 lines:
array.push(correctionWickRelativeThresholdReachedOr ? succWR : failWR, wickRelative)
array.push(correctionCloseRelativeThresholdReachedOr ? succCR : failCR, closeRelative)
array.push(reverseRelativeThresholdReachedOr ? succRR : failRR, reverseRelative)
array.push(sidewaysThresholdReachedAnd ? succSW : failSW, sideways)
array.push(atrThresholdReachedAnd ? succWA : failWA, wickAbsolute)
Suddenly removed more than 52 local scopes from my code?....