xmd79

BPNS

Dec 26th, 2022
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 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. // © peacefulLizard50262
  3.  
  4. //@version=5
  5. indicator("BPNS", explicit_plot_zorder = true)
  6.  
  7. bes(float source = close, float alpha = 0.7) =>
  8. var float smoothed = na
  9. smoothed := na(smoothed) ? source : alpha * source + (1 - alpha) * nz(smoothed[1])
  10.  
  11. sma(source, length) =>
  12. // Initialize the moving average to the first element of the source
  13. float average = source[0]
  14. // Initialize the count to 1
  15. int count = 1
  16.  
  17. // Calculate the moving average using the Welford method
  18. for i = 1 to length
  19. count := count + 1
  20. delta = source[i] - average
  21. average := average + delta / count
  22.  
  23. // Return the moving average
  24. average
  25.  
  26. ssmma(src, len, smooth)=>
  27. smma = 0.0
  28. avg = sma(src, len)
  29. smma := na(smma[1]) ? avg : bes((smma[1] * (len - 1) + src) / len, smooth)
  30.  
  31. smma(src, len)=>
  32. smma = 0.0
  33. avg = sma(src, len)
  34. smma := na(smma[1]) ? avg : (smma[1] * (len - 1) + src) / len
  35.  
  36. ema(src, len, smoothing, a) => //{
  37. alpha = a / (len + 1)
  38. sum = 0.0
  39. sum := na(sum[1]) ? src : alpha * bes(src, smoothing) + (1 - alpha) * nz(bes(sum[1], smoothing))
  40.  
  41. dema(src, length, smooth, a)=>
  42. e1 = ema(src, length, smooth, a)
  43. e2 = ema(e1, length, smooth, a)
  44. dema = 2 * e1 - e2
  45.  
  46. tema(src, length, smooth, a) =>
  47. e1 = ema(src, length, smooth, a)
  48. e2 = ema(e1, length, smooth, a)
  49. e3 = ema(e2, length, smooth, a)
  50. tema = 3 * (e1 - e2) + e3
  51.  
  52. wma(src, len) =>
  53. ta.wma(src, len)
  54. //// dont look for too long...
  55.  
  56. hma(src, len)=>
  57. wma(2 * wma(src, int(len/2)) - wma(src, len), int(math.sqrt(len/2)))
  58.  
  59. thma(src, len) =>
  60. wma1 = wma(src, len)
  61. wma2 = wma(src, int(len/2))
  62. wma3 = 2 * wma2 - wma1
  63. hma = wma(wma3, int(math.sqrt(len/2)))
  64. 3 * (wma1 - wma2) + hma
  65.  
  66. ehma(src, len, smoothing, a)=>
  67. ema(2 * ema(src, int(len/2), smoothing, a) - ema(src, len, smoothing, a), int(math.sqrt(len/2)), smoothing, a)
  68.  
  69. gma(values, length) =>
  70. // Define the standard deviation of the Gaussian distribution
  71. stddev = length / 4
  72. // Generate an array of indices from 1 to length
  73. indices = ta.range(1, length)
  74. // Calculate the Gaussian weights for each value in the array
  75. weights = math.exp(-0.5 * (math.pow((indices - length), 2) / math.pow(stddev, 2)))
  76. // Calculate the weighted sum of the values
  77. sum = math.sum(values * weights, length)
  78. // Return the moving average
  79. sum / math.sum(weights, length)
  80.  
  81. rms(source, length)=>
  82. math.sqrt(math.sum(math.pow(source, 2), length)/length)
  83.  
  84. max(source, outlier_level, dev_lookback)=>
  85. var float max = na
  86. src = array.new<float>()
  87. stdev = math.abs((source - bes(source, 0.1))/ta.stdev(source, dev_lookback))
  88. array.push(src, stdev < outlier_level ? source : -1.7976931348623157e+308)
  89. max := math.max(nz(max[1]), array.get(src, 0))
  90.  
  91. min(source, outlier_level, dev_lookback) =>
  92. var float min = na
  93. src = array.new<float>()
  94. stdev = math.abs((source - bes(source, 0.1))/ta.stdev(source, dev_lookback))
  95. array.push(src, stdev < outlier_level ? source : 1.7976931348623157e+308)
  96. min := math.min(nz(min[1]), array.get(src, 0))
  97.  
  98. min_max(src, outlier_level, dev_lookback) =>
  99. (src - min(src, outlier_level, dev_lookback))/(max(src, outlier_level, dev_lookback) - min(src, outlier_level, dev_lookback)) * 100
  100.  
  101. source = input.source(close, "Source", group = "Source")
  102. moving = input.string("BES", "MA Style", ["BES","SSMMA","SMMA","SMA","RMS","EMA","DEMA","TEMA","WMA","HMA","EHMA","THMA","GMA"], group = "Source")
  103. blur = input.float(1, "Global Smoothing", 0.005, 1, 0.005, group = "Source")
  104. bound = input.float(70, "Bounds", 50, 100, group = "Source")
  105.  
  106. lf_brown = input.float(0.15, "BES Low Pass Filter Cutoff", 0.001, 1, 0.001, tooltip = "Adjusts BES", group = "Browns")
  107. hf_brown = input.float(0.01, "BES High Pass Filter Cutoff", 0.001, 1, 0.001, tooltip = "Adjusts BES", group = "Browns")
  108.  
  109. lf_len = input.int(200, "Low Pass Filter Length", 1, tooltip = "Inputs for the MA's.", group = "MA")
  110. hf_len = input.int(12, "High Pass Filter Length", 1, tooltip = "Inputs for the MA's.", group = "MA")
  111. lf_alpha = input.float(2, "Low Pass Filter Alpha", 0.05, 10, 0.05, tooltip = "Inputs for the MA's.", group = "MA")
  112. hf_alpha = input.float(2, "High Pass Filter Alpha", 0.05, 10, 0.05, tooltip = "Inputs for the MA's.", group = "MA")
  113. lf_smooth = input.float(0.7, "Low Pass Filter Smoothing", 0.005, 1, 0.005, tooltip = "Inputs for the MA's.", group = "MA")
  114. hf_smooth = input.float(0.7, "High Pass Filter Smoothing", 0.005, 1, 0.005, tooltip = "Inputs for the MA's.", group = "MA")
  115. outlier_level = input.float(2, "Outlier Level", 0.01, 100, 0.01, group = "Outlier Detection")
  116. dev_lookback = input.int(30, "Outlier Length", 2, group = "Outlier Detection")
  117.  
  118. enable = input.bool(false, "Enable Global Mean", group = "Mean")
  119. mean = input.string("BES", "MA Style", ["BES","SSMMA","SMMA","SMA","RMS","EMA","DEMA","TEMA","WMA","HMA","EHMA","THMA","GMA"], group = "Mean")
  120. mean_brn = input.float(0.01, "Global Smoothing Browns Alpha", group = "Mean")
  121. mean_len = input.int(14, "Global Smoothing MA Length", group = "Mean")
  122. mean_alp = input.float(2, "Low Pass Filter Alpha", 0.05, 10, 0.05, group = "Mean")
  123. mean_smo = input.float(0.7, "Low Pass Filter Smoothing", 0.005, 1, 0.005), group = "Mean"
  124.  
  125. ma(src, len, smooth, alpha, brown, moving)=>
  126. switch moving
  127. "BES" => bes(src, brown)
  128. "SSMMA" => ssmma(src, len, smooth)
  129. "SMMA" => smma(src, len)
  130. "SMA" => sma(src, len)
  131. "RMS" => rms(src, len)
  132. "EMA" => ema(src, len, smooth, alpha)
  133. "DEMA" => dema(src, len, smooth, alpha)
  134. "TEMA" => tema(src, len, smooth, alpha)
  135. "WMA" => wma(src, len)
  136. "HMA" => hma(src, len)
  137. "EHMA" => ehma(src, len, smooth, alpha)
  138. "THMA" => thma(src, len)
  139. "GMA" => gma(src, len)
  140. "RMS" => rms(src, len)
  141.  
  142. band_pass = bes(min_max(ma(source - ma(source, hf_len, hf_smooth, hf_alpha, hf_brown, moving), lf_len, lf_smooth, lf_alpha, lf_brown, moving), outlier_level, dev_lookback), blur)
  143. g_mean = ma(band_pass, mean_len, mean_smo, mean_alp, mean_brn, mean)
  144.  
  145. plot(band_pass > 100 ? 100 : band_pass < 0 ? 0 : band_pass, "Band Pass", color.blue)
  146. plot(enable ? g_mean : na, "Global Mean", color.orange)
  147.  
  148. _100 = hline(bound, "Upper Bound", linestyle = hline.style_solid)
  149. _50 = hline(50, "50", color.new(color.gray, 50))
  150. _0 = hline(100 - bound, "Lower Bound", linestyle = hline.style_solid)
  151.  
  152. fill(_100, _0, color.new(color.navy, 90))
Advertisement
Add Comment
Please, Sign In to add comment