0

I am getting this warning with my user function. Error is:

The function 'foo' should be called on each calculation for consistency. It is recommended to extract the call from this scope

This warning happens for many reasons it seems, one of which is doing a for loop on a series. For example:

foo()=>
    for i = 0 to 5 
        if close[i] > 0 or close[i] < 0
            break

if close < 10
    foo()

This will throw the warning. However, it makes no sense! Because without the function call, there will be no consistency either!

For example, I can do:

if close < 10
    for i = 0 to 5 
        if close[i] > 0 or close[i] < 0
            break

And there won't be any consistency anyways! For loop will only run when close < 10, maybe alaways, maybe never! But for some reason, putting this into a function, now its inconsistent?

Please explain why is it so, and is it ever possible to use for loops on series in functions without such warnings?

EDIT:

Experimenting further, it seems even touching series is a no-no in functions?

foo()=>
    a = close[1]

if close < 10
    foo()

Is enough to give a warning. Is it documented somewhere? Why? Is there no work around?

1 Answers1

0

I can understand your disappointment and I am fully with you. But pine script isn't. It is designed the way - if considered good or not - that it is very limited what you can do with functions (eg.: you cannot modify parameter values etc.).
By built-in functions or series that use the history referencing operator or functionality, your only safe option is to do everything in the global scope, store it in a variable and use your variable in a function. Or make sure you call your function strictly on each bar without a conditional.

The cause of the warning in your tests is the if conditional that makes the calculation inconsistent, not the function itself. If you call a function on each bar from the global scope, there is no problem. But since "if" it can happen that the function won't get called and that's why the message.
You can use for loops with series and history referencing withouth a problem, but only in the global scope.
I suggest you read the execution model description for more details.

elod008
  • 1,227
  • 1
  • 5
  • 15
  • But I can call [] inside a conditional, what difference does it make, calling it inside a function in a conditional, or a conditional? Could you explain, why is it like this? 1. Call [] inside conditional - OK. 2. Call [] inside a function with or without conditional - OK. 3. Call [] inside a function inside a conditional - NOT OK. Why is that so? I have read the link and it doesn't really address my particular question. or maybe I'm not getting it? – Alexei Andronov Oct 10 '22 at 10:43
  • To be precise, to call [] within a conditional is NOT ok. That's exactly the point. The emphasis lies not on the function encapsulation but on the conditional statements that can cause unconsistent. It's perfectly ok to use [] within a function and call that function from global scope. It's not ok to call your function with some [] from within a conditional statement or ternary operation. I don't rely solely on the warning issued by pine script. Since it's just a warning and not an error, it may/may not be 100% correct if the lexer/parsing of pine cannot interpret your code entirely. – elod008 Oct 10 '22 at 12:56
  • elod008 but... it works? all the scripts do [] calls in conditionals all the time? My question is what's the difference calling [] within a condition, or within a function within a condition? – Alexei Andronov Oct 10 '22 at 18:17
  • The difference is absolutely nothing. It works. Yes. But inconsistently. It works until it may just not work. Best example is ta.barssince() that's going to return you false results occasionally, packed inside a conditional or ternary. That's why it's a warning and not a compilation/runtime error, that may/may not be displayed to you. Design decision from tradigview. I don't want to protect it, it's just the way it is and we have to adapt to it. Sum up: [ ] should be always called in the global scope OR from function that's called in the global scope. The problem is the conditionals. – elod008 Oct 10 '22 at 19:03
  • 1
    Okay so here I finally got it, thank you very much. The issue is not the conditional around [], but the fact that if the function never gets called at all, it never gets to see this bar's value at all. If the function gets called, even if the [] in it doesn't get called, the function still saw that value and remembers what was the [] value even if that value is never accessed. global space also always remembers it so if [] is in a conditional it doesn't matter. it only matters is the snapshot of time itself is witnessed by the function at least once. I still dont get my other post's question.. – Alexei Andronov Oct 11 '22 at 10:00
  • Nice approach! I like your explanation, it is very complex and even too complex for me to keep in mind by developing. So, even if not 100.00% correct/complete, I prefer sticking to the simplified rule that I just try to avoid history referencing depending on some conditions so that I don't have to keep in mind that stuff. The more complex your script gets the more difficult it is to keep everything in mind AND the worst part is that you may never even realize that due to the inconsistency your results are incorrect since everything seems to work fine. – elod008 Oct 11 '22 at 11:14