CALobo

string2float v1.0

CALobo Updated   
In the v5 version of PineScript, casting a string to a float - float(x) - does not appear to work, in spite of the reference manual seemingly indicating that it should. The simplest test case that I could generate "float('4750.0')" would result in a "Cannot call 'float' with argument 'x'='4750.0'. An argument of 'literal string' type was used but a 'const float' is expected." Since I was pulling the floats from a string array, I could not find a way to make them into "const floats". So I gave up and wrote a quick and dirty string to float converter function that handles both positive and negative integers and floats and returns the a float. Hopefully it might save someone else an afternoon of insanity.... :-)
Release Notes:
It's been pointed out that I missed the str.tonumber() function in my quest to convert a string to a float.... The update version 2.0 of the script tests out this function - see line 62 where it changes the str2float() function call to the built in function str.tonumber(). I'm happy to say that the revision passes all the tests and that this script/function/library can now be retired. I've left the code here in case some else has a similar problem, or wants to see how the str.tonumber() function call might be implemented...
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?