MichelT

String to Number

A small method to translate strings to numbers.
For example, we have a string:

s = "321"

To check if the last symbol is "1", we might just try to remove (or replace with an empty string) the symbol "1". If the string changed, then there was the symbol "1" in the string:

s2 = str .replace_all (s, "1", "")

now s2 is "32", so

s == s2 returns false.

But we can't find out what the position of the symbol "1" was and if there was only one symbol "1".

To be sure, we just add a special character (I use ';') to the end of the string, and then try to remove not just "1", but the string "1;":

s = "321;"
s2 = str .replace_all (s, "1;", "")

Now, if s != s2, then we might be sure, that last symbol of the string was "1". And then we might check for all digits and get information about what digit we have and at what position. Same for additional symbols: decimal point and minus.
開源腳本

秉持真正的TradingView精神,該腳本的作者將其開源發佈,因此交易者可以理解和驗證它。為作者加油!您可以免費使用它,但是在發佈中重複使用此程式碼受網站規則的約束。您可以把它加入到常用以在圖表上使用它。

想在圖表上使用此腳本?

評論

Phenomenal work mate, this really should be an inbuilt function but lucky for the Pine community you're here!
+2 回覆
Beautiful work, I don't congratulate just everyone but this is as close to witchcraft as it gets in a good way.
+2 回覆
Quansium Quansium
@Quansium, can you take a look into why this doesn't work with Strategy parameters, specifically "strategy.commission.percent"? I didn't spend much time trying to figure it out but in the few minutes I did it didn't work.
回覆
MichelT Quansium
@Quansium, Could you elaborate what exactly doesn't work? `strategy.commission.percent` is just a regular string "percent", which might be printed in a label, so there's no anything to parse as a number.
回覆
Quansium MichelT
@MichelT, i'm more inclined into algo trading, I want to calculate some advanced backtest results so it is all custom, I need fees to be added into the calculation. I can't use TV fees because they're strings. So I had to have a separate input where the user entered the fees. If I can convert the TV fees string into a number, I don't need the input repeated.
回覆
MichelT Quansium
@Quansium, Could you share some code, because I'm not quite understand the case. Maybe, you could send my a link to the code example as a private message.
回覆
Quansium MichelT
@MichelT, I will, it is rather simple, I just didn't want to have it posted on comments as it includes your code which will make my example long enough to take considerable space on here.
回覆
MichelT Quansium
@Quansium, thanks, I really appreciate it.
回覆
Thanks for sharing this, really great work!! I wonder if you can help me solve the following problem. I use your great code to get the length from a string and it works for sma, but not ema.
// This works fine
float result = 0
int length = 0
data_source = close
length := int(strToNum("20"))
result := sma(data_source, length)
plot(series=result ? result : na, color=color.blue, linewidth=2, transp=0, title='Baseline')

// This fails compilation
float result = 0
int length = 0
data_source = close
length := int(strToNum("20"))
result := ema(data_source, length)
plot(series=result ? result : na, color=color.blue, linewidth=2, transp=0, title='Baseline')

The error message is:
Add to Chart operation failed, reason: line 142: Cannot call 'ema' with arguments (series, series); available overloads: ema(series, integer) => series

Looking at the documentation, both the sma and ema have the same method signature.
ARGUMENTS
source (series) Series of values to process.
length (integer) Number of bars (length).

I can't see why one would work and the other not.
回覆