xmd79

Digit Sum - Numerology

Sep 25th, 2021
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // Possible thanks to MichelT: https://www.tradingview.com/script/nnED9HgA-String-to-Number/
  3. // © firerider
  4.  
  5. //@version=4
  6. study("Digit Sum - Numerology")
  7.  
  8. src = input(title="Source", type=input.source, defval=close)
  9. single_digit = input(title="Single Digit On/Off", type=input.bool, defval=true)
  10. date_src_on = input(title="Use date/time for Source (overrides Source above) ", type=input.bool, defval=false)
  11. date_choice = input(title="Date/Time format", defval="Time and Date", options=["Date only", "Time only", "Time and Date"])
  12.  
  13. // some enumerations
  14. DECIMAL_POINT = 0.1
  15. MINUS = -1
  16.  
  17. error() => label.new(0, close[-1]) // a function to throw an error
  18.  
  19. cutLastDigit(str) =>
  20. s = str + ";"
  21. r = str.replace_all(s, "1;", "")
  22. if r != s
  23. [r, 1.]
  24. else
  25. r = str.replace_all(s, "2;", "")
  26. if r != s
  27. [r, 2.]
  28. else
  29. r = str.replace_all(s, "3;", "")
  30. if r != s
  31. [r, 3.]
  32. else
  33. r = str.replace_all(s, "4;", "")
  34. if r != s
  35. [r, 4.]
  36. else
  37. r = str.replace_all(s, "5;", "")
  38. if r != s
  39. [r, 5.]
  40. else
  41. r = str.replace_all(s, "6;", "")
  42. if r != s
  43. [r, 6.]
  44. else
  45. r = str.replace_all(s, "7;", "")
  46. if r != s
  47. [r, 7.]
  48. else
  49. r = str.replace_all(s, "8;", "")
  50. if r != s
  51. [r, 8.]
  52. else
  53. r = str.replace_all(s, "9;", "")
  54. if r != s
  55. [r, 9.]
  56. else
  57. r = str.replace_all(s, "0;", "")
  58. if r != s
  59. [r, 0.]
  60. else
  61. r = str.replace_all(s, ".;", "")
  62. if r != s
  63. [r, DECIMAL_POINT]
  64. else
  65. r = str.replace_all(s, "-;", "")
  66. if r != s
  67. [r, MINUS]
  68. else
  69. error()
  70. [str, -1]
  71.  
  72. strToNum(str) =>
  73. fractional = 0. // fractional part of the number
  74. integer = 0. // integer part of the number
  75.  
  76. s_new = str
  77. position = 0.0 // handled position of the number.
  78. sign = 1 // sign of the number. 1.0 is PLUS and -1.0 is MINUS.
  79. for i = 0 to 1000
  80. [s, digit] = cutLastDigit(s_new) // here we'll cut the digits by one from the string till the string is empty.
  81. if digit == DECIMAL_POINT // DECIMAL_POINT is found. That means the number has decimal part.
  82. order = pow(10, i) // convert the decimal part by shifting the accumulated value to (10^i) positions to right
  83. fractional := integer / order
  84. integer := 0.
  85. position := 0
  86. else
  87. if digit == MINUS // MINUS sing is found.
  88. sign := MINUS
  89. break // I expect that there's nothing after the MINUS ('-') sign.
  90. 0. // just a little workaround to help the 'if' to detect returning type.
  91. else
  92. integer := integer + digit * pow(10, position) // it's just a regular digit.
  93. position := position + 1
  94. if s == ""
  95. break
  96. s_new := s // If we are here, then there are more digits in the string. Let's handle the next one!
  97. sign * (integer + fractional) // We've exited from the loop. Build the returning value.
  98.  
  99. digitSum(source) =>
  100. float sum = 0
  101. digits_str = str.split(str.replace_all(tostring(source),".",""), "")
  102. for i = 0 to array.size(digits_str) - 1
  103. digit = strToNum(array.get(digits_str, i))
  104. sum := sum + digit
  105. sum
  106.  
  107.  
  108. digits_date = tostring(year) + tostring(month) + tostring(dayofmonth )
  109. digits_time = tostring(hour) + tostring(minute) + tostring(second)
  110. date_src = date_choice == "Date only" ? digits_date : date_choice == "Time only" ? digits_time : digits_date + digits_time
  111.  
  112.  
  113. sum = single_digit ? digitSum(digitSum(digitSum(date_src_on ? strToNum(date_src) : src))) : digitSum(date_src_on ? strToNum(date_src) : src)
  114.  
  115.  
  116. plot(sum, style=plot.style_stepline)
  117.  
Advertisement
Add Comment
Please, Sign In to add comment