I have so far found two quite severe bugs in Pine Script v5. And here is another one.
This is not really a question, rather, I think people can benefit from this if they encounter such behavior too, hopefully I can save them some time.
So. Pine Script is supposed to accept float and int in place of bool, converting them to bool based on common programming language logic (0 = false, anything else = true). Any sane programming language is also supposed to store converted variables as their end product. Like, if I have assigned 2 to a bool variable, it should store "true", not "2".
Not so in Pine Script... Here is an example:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AVAndronov
// CONSTS
OVERLAY = false
BUG_CONST = 2.5
//@version=5
indicator("My script", overlay = OVERLAY)
bool bug_input = input.bool(BUG_CONST, "Display Close 1")
bool bug_free_input = input.bool(BUG_CONST ? true : false, "Display Close 2")
bool bug_var = BUG_CONST
//this one will bug out
plot(bug_input ? close : 0, color = color.red)
//these two will work
plot(bug_free_input ? -close : 0, color = color.green)
plot(BUG_CONST ? close : 0, color = color.purple)
if barstate.islast
label.new(bar_index, 0,
str.tostring(bug_input)
+"\n"+str.tostring(bug_input == true ? "true" : "not == true")
+"\n"+str.tostring(bug_input == false ? "== false" : "not == false")
+"\n"+str.tostring(bug_input ? "? true" : ": false")
+"\n"+str.tostring(bug_var)
+"\n"+str.tostring(bug_var == true ? "true" : "not == true")
+"\n"+str.tostring(bug_var == false ? "false" : "not == false")
+"\n"+str.tostring(bug_var ? "? true" : ": false")
+"\n"+str.tostring(bug_free_input) )
The result produced will be:
1. In script inputs window, both checks will be checked.
2. Flat red line, price displayed on green and purple lines.
3. Label:
false
not == true
== false
: false
2.5
not == true
not == false
? true
true
What we can observe here is this:
1. When you assign non-bool to a bool, it doesn't actually store a bool. Instead, it stores whatever you have assigned!
You can see I am using "str.tostring" on "bug_var", a bool variable of type const bool (can be verified by hovering over it in the editor). But it displays 2.5! So it actually stores the float inside. Not a bool.
2. When you assign non-bool to a bool, it behaves like that value sometimes, and sometimes it doesn't
You can see I call "bug_var == true" and it results in false, because bug_var is not equal to true, it's actually equal to 2.5. == false also results in false, it's not equal to false either! However, when I later do bug_var ? :, it results in true, because bug_var evaluates to true when put in place of a bool.
So it's a bool that isn't equal to true, isn't equal to false, but evaluates to true. What a unique bool....
3. When you assign a non-bool to an input bool, it misbehaves in a different manner.
The input interface of tradingview website thinks it is true, however, the code thinks it is false. So when you check this input, it will be false for all intents and purposes, it will be == false, it will evalutate to false in ?: conditionals.
4. At least if you do "FLOAT ? true : false" or "bool(FLOAT)" it will work properly
Note however that it will NOT work if you do FLOAT ? 1 : 0, as that will produce same result as above (it will be considered false everywhere if you assign this to an input bool, and it will be true in checks and false otherwise if you assign this to a variable bool).
Closing words.
Hopefully I'll save some time for other people as I've spend literally an hour thinking WTF is wrong with my code until I figured out its a language bug. Thing is, I don't understand, does TradingView even care?
So far as a novice programmer to Pine Script I've found three severe bugs, two of which can easilly cause dozens of programming hours lost debugging something that isn't even programmer's fault.
- This one
- I found out insane TradingView bug with timeframe.in_seconds()... what to do?
- Pine Script "script has too many local scopes, the limit is 500" - What is / counts as a local scope?
Once I have been offered to contact them via support, but they don't provide answers to non-paid members. Do they even care? Does anyone know how to get an answer from them without paying them money?...