0

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?....

  • Scopes are explained in [Language > Script structure > Code](https://www.tradingview.com/pine-script-docs/en/v5/language/Script_structure.html?highlight=scope#code). Structures or multi-line function declarations always require a local block. A local block must be indented by a tab or four spaces. Each local block defines a distinct local scope. – Bjorn Mistiaen Oct 17 '22 at 10:57
  • @BjornMistiaen Bjorn Mistiaen Thank you. In that case, how can you explain that my code can contain 2000 of if/else statements from my example and no error is triggered? while it triggers for my code that contains only 255 if/when/for statements and about 30 functions? – Alexei Andronov Oct 17 '22 at 11:39
  • 1
    Not sure why. Maybe adding the offending code to your question could help, so we can take a look. – Bjorn Mistiaen Oct 17 '22 at 12:06
  • well the code is several thousand lines long, but I've added a sample. There's nothing special about it really. Just code... – Alexei Andronov Oct 17 '22 at 13:35
  • Your if/else statements are nested. Each statement that begins with whitespace is a local block (as defined in the manual). That way you easily get to 500. – Bjorn Mistiaen Oct 17 '22 at 13:38
  • But if EACH statement would be, first, why does my example with over 4000 nested statements as defined in the manual produce no error,and second, I have about 2 thousand statements defined like that now with my code, and no error. why is that? – Alexei Andronov Oct 18 '22 at 07:57
  • Please share both codes (working and not working) either through a TradingView (private) publishing or a PasteBin link, and add it to your question. That way we can have a look. – Bjorn Mistiaen Oct 18 '22 at 08:24
  • @BjornMistiaen I have. And I have in process confirmed this is indeed a mess.. Please take a look. One code compiles with 552 local scopes error. Another code, where I remove five calls of array.push, suddenly doesn't error at all! So how come removing 5 calls to built-in function somehow removed over 52 local scopes from my code?... – Alexei Andronov Oct 19 '22 at 14:54

0 Answers0