6

My script has just recently started to show these lines in console, when I add to chart or save.

"The function 'anonym_function_10' should be called on each calculation for consistency. It is recommended to extract the call from the ternary operator or from the scope."
"The function 'anonym_function_11' should be called on each calculation for consistency. It is recommended to extract the call from the ternary operator or from the scope." 

Need some help understanding this, wheather the code is compromised for accuracy, or this could be a possible problem in the future? What would be a solution to fix this?

// @version=4
f_top_fractal(src) => src[4] < src[2] and src[3] < src[2] and src[2] > src[1] and src[2] > src[0]
f_bot_fractal(src) => src[4] > src[2] and src[3] > src[2] and src[2] < src[1] and src[2] < src[0]
f_fractalize(src) => f_top_fractal(src) ? 1 : f_bot_fractal(src) ? -1 : 0

The last line is the one in question...

cryptobar76
  • 61
  • 1
  • 1
  • 4

3 Answers3

5

If your code is like this, you are likely to get this error.

mav = na(xem[1]) ? sma(src, len) : (xem[1] / len)`

try it like this ...

_sma= sma(src, len)
mav = na(xem[1]) ? _sma : (xem[1] / len)
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
Zeki
  • 51
  • 1
  • So how would i rewrite my code to not get this error? – cryptobar76 Nov 21 '20 at 09:08
  • Thanks for the answer but its not clear why this error occurs based on your answer. Can you explain why? Thanks – LeanMan Jan 26 '21 at 15:30
  • This worked for me. evaluating expression beforehand does work in ternary operator, in this case ```sma(src, len)``` is evaluated and then used in ternary operator. – Kundan May 26 '21 at 20:12
4

Functions involving series variables need to be executed every bar so the function has a complete series history; otherwise series indexes inside the function will be incorrect. So Pine issues a warning for such functions when they are embedded inside a conditional expression, which might cause them not to be executed every bar.

To solve this, either execute the function at global scope, outside any conditional expressions, or redefine the function to accept individual series values as function arguments.

The latter solution would work in the OP's case.

See the explanation at "Execution of Pine functions and historical context inside function blocks" at https://www.tradingview.com/pine-script-docs/en/v4/language/Functions_and_annotations.html

KimB
  • 41
  • 1
-1
f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0

Below is alternative

f_fractalize(_src)=>
    bool rhign = f_top_fractal(_src)
    bool rlow = f_bot_fractal(_src)
    if rhign
        1
    else if  rlow
        -1
    else
        0
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Nov 10 '22 at 00:12