Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // Possible thanks to MichelT: https://www.tradingview.com/script/nnED9HgA-String-to-Number/
- // © firerider
- //@version=4
- study("Digit Sum - Numerology")
- src = input(title="Source", type=input.source, defval=close)
- single_digit = input(title="Single Digit On/Off", type=input.bool, defval=true)
- date_src_on = input(title="Use date/time for Source (overrides Source above) ", type=input.bool, defval=false)
- date_choice = input(title="Date/Time format", defval="Time and Date", options=["Date only", "Time only", "Time and Date"])
- // some enumerations
- DECIMAL_POINT = 0.1
- MINUS = -1
- error() => label.new(0, close[-1]) // a function to throw an error
- cutLastDigit(str) =>
- s = str + ";"
- r = str.replace_all(s, "1;", "")
- if r != s
- [r, 1.]
- else
- r = str.replace_all(s, "2;", "")
- if r != s
- [r, 2.]
- else
- r = str.replace_all(s, "3;", "")
- if r != s
- [r, 3.]
- else
- r = str.replace_all(s, "4;", "")
- if r != s
- [r, 4.]
- else
- r = str.replace_all(s, "5;", "")
- if r != s
- [r, 5.]
- else
- r = str.replace_all(s, "6;", "")
- if r != s
- [r, 6.]
- else
- r = str.replace_all(s, "7;", "")
- if r != s
- [r, 7.]
- else
- r = str.replace_all(s, "8;", "")
- if r != s
- [r, 8.]
- else
- r = str.replace_all(s, "9;", "")
- if r != s
- [r, 9.]
- else
- r = str.replace_all(s, "0;", "")
- if r != s
- [r, 0.]
- else
- r = str.replace_all(s, ".;", "")
- if r != s
- [r, DECIMAL_POINT]
- else
- r = str.replace_all(s, "-;", "")
- if r != s
- [r, MINUS]
- else
- error()
- [str, -1]
- strToNum(str) =>
- fractional = 0. // fractional part of the number
- integer = 0. // integer part of the number
- s_new = str
- position = 0.0 // handled position of the number.
- sign = 1 // sign of the number. 1.0 is PLUS and -1.0 is MINUS.
- for i = 0 to 1000
- [s, digit] = cutLastDigit(s_new) // here we'll cut the digits by one from the string till the string is empty.
- if digit == DECIMAL_POINT // DECIMAL_POINT is found. That means the number has decimal part.
- order = pow(10, i) // convert the decimal part by shifting the accumulated value to (10^i) positions to right
- fractional := integer / order
- integer := 0.
- position := 0
- else
- if digit == MINUS // MINUS sing is found.
- sign := MINUS
- break // I expect that there's nothing after the MINUS ('-') sign.
- 0. // just a little workaround to help the 'if' to detect returning type.
- else
- integer := integer + digit * pow(10, position) // it's just a regular digit.
- position := position + 1
- if s == ""
- break
- s_new := s // If we are here, then there are more digits in the string. Let's handle the next one!
- sign * (integer + fractional) // We've exited from the loop. Build the returning value.
- digitSum(source) =>
- float sum = 0
- digits_str = str.split(str.replace_all(tostring(source),".",""), "")
- for i = 0 to array.size(digits_str) - 1
- digit = strToNum(array.get(digits_str, i))
- sum := sum + digit
- sum
- digits_date = tostring(year) + tostring(month) + tostring(dayofmonth )
- digits_time = tostring(hour) + tostring(minute) + tostring(second)
- date_src = date_choice == "Date only" ? digits_date : date_choice == "Time only" ? digits_time : digits_date + digits_time
- sum = single_digit ? digitSum(digitSum(digitSum(date_src_on ? strToNum(date_src) : src))) : digitSum(date_src_on ? strToNum(date_src) : src)
- plot(sum, style=plot.style_stepline)
Advertisement
Add Comment
Please, Sign In to add comment