tg-me.com/PineCodersSquawkBox/16
Last Update:
💪 #tip
Functions and built-ins used in `if` or ternary (`?`) blocks
An important change to the way conditional statement blocks are evaluated was introduced with v4 of Pine. Many coders are not aware of it or do not understand its implications. This User Manual section explains the change and provides a list of exceptions for functions/built-ins which are NOT affected by the constraints. We'll explain what's happening here, and how to avoid the problems caused by code that does not take the change into account.
This is what's happening:
🔷 1. Starting in Pine v4, both blocks of conditional statements are no longer executed on every bar. By "both blocks", we mean the part executed when the conditional expression evaluates to true
, and the one (if it exists) to be executed when the expression evaluates to false
.
🔷 2. Many functions/built-ins need to execute on every bar to return correct results. Think of a rolling average like sma()
or a function like highest()
. If they miss values along the way, it's easy to see how they won't calculate properly.
This is the PineCoders "If Law":
🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸 🔸🔸🔸
Whenever an if
or ternary's (?
) conditional expression can be evaluated differently bar to bar, all functions used in the conditional statement's blocks not in the list of exceptions need to be pre-evaluated prior to entry in the if
statement, to ensure they are executed on each bar.
🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸🔸
While this can easily be forgotten in the creative excitement of coding your latest idea, you will save yourself lots of pain by understanding and remembering this. This is a major change from previous versions of Pine. It has far-reaching consequences and not structuring code along these lines can have particularly pernicious consequences because the resulting incorrect behavior is sometimes discrete (appearing only here and there) and random.
To avoid problems, you need to be on the lookout for 2 conditions:
🔷 Condition A)
A conditional expression that can only be evaluated with incoming, new bar information (i.e., using series variables like close
). This excludes expressions using values of literal, const, input or simple forms because they do not change during the script's execution, and so when you use them, the same block in the if
statement is guaranteed to execute on every bar. [Read this if you are not familiar with Pine forms and types.]
🔷 Condition B)
When condition A is met, and the if
block(s) contain(s) functions or built-ins NOT in the list of exceptions, i.e., which require evaluation on every bar to return a correct result, then condition B is also met.
In this FAQ & Code entry you will see an example where an apparently inoffensive built-in like vwap
is used in a ternary. vwap
is not in the list of exceptions, and so when condition A is realized, it will require evaluation prior to entry in the if
block. You can flip between 3 modes: #1 where condition A is fulfilled and #2 and #3 where it is not. You will see how the unshielded value ("upVwap2" in the thick line) will produce incorrect results when mode 1 is used.
BY PineCoders Squawk Box
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283
Share with your friend now:
tg-me.com/PineCodersSquawkBox/16