Advertisement
Guest User

Untitled

a guest
Feb 12th, 2024
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.83 KB | None | 0 0
  1. //@version=5
  2. strategy("Multi Kernel Regression [ChartPrime]", overlay = true, max_lines_count = 500, max_bars_back = 500, max_labels_count = 500)
  3.  
  4. repaint = input.bool(true, "Repaint")
  5.  
  6. kernel = input.string("Laplace", "Kernel Select",
  7. [ "Triangular"
  8. , "Gaussian"
  9. , "Epanechnikov"
  10. , "Logistic"
  11. , "Log Logistic"
  12. , "Cosine"
  13. , "Sinc"
  14. , "Laplace"
  15. , "Quartic"
  16. , "Parabolic"
  17. , "Exponential"
  18. , "Silverman"
  19. , "Cauchy"
  20. , "Tent"
  21. , "Wave"
  22. , "Power"
  23. , "Morters"])
  24.  
  25. bandwidth = input.int(14, 'Bandwidth', 1)
  26. source = input.source(close, 'Source')
  27. deviations = input.float(2.0, 'Deviation', 0, 100, 0.25, inline = "dev")
  28. style = input.string("Solid", "Line Style", ["Solid", "Dotted", "Dashed"])
  29. enable = input.bool(false, "", inline = "dev")
  30. label_size = input.string("Tiny", "Labels", ["Auto", "Tiny", "Small", "Normal", "Large", "Huge"], inline = "label")
  31. lables = input.bool(true, "", inline = "label")
  32. bullish_color = input.color(color.rgb(84, 194, 148), "Colors", inline = "color")
  33. bearish_color = input.color(color.rgb(235, 57, 57), "", inline = "color")
  34. text_color = input.color(color.rgb(8, 12, 20), "", inline = "color")
  35.  
  36. size = switch label_size
  37. "Auto" => size.auto
  38. "Tiny" => size.tiny
  39. "Small" => size.small
  40. "Normal" => size.normal
  41. "Large" => size.large
  42. "Huge" => size.huge
  43.  
  44. line_style = switch style
  45. "Solid" => line.style_solid
  46. "Dotted" => line.style_dotted
  47. "Dashed" => line.style_dashed
  48.  
  49. sq(source) => math.pow(source, 2)
  50.  
  51. gaussian(source, bandwidth) =>
  52. math.exp(-sq(source / bandwidth) / 2) / math.sqrt(2 * math.pi)
  53.  
  54. triangular(source, bandwidth) =>
  55. math.abs(source/bandwidth) <= 1 ? 1 - math.abs(source/bandwidth) : 0.0
  56.  
  57. epanechnikov(source, bandwidth) =>
  58. math.abs(source/bandwidth) <= 1 ? (3/4.) * (1 - sq(source/bandwidth)) : 0.0
  59.  
  60. quartic(source, bandwidth) =>
  61. if math.abs(source/bandwidth) <= 1
  62. 15/16. * math.pow(1 - sq(source/bandwidth), 2)
  63. else
  64. 0.0
  65.  
  66. logistic(source, bandwidth) =>
  67. 1 / (math.exp(source / bandwidth) + 2 + math.exp(-source / bandwidth))
  68.  
  69. cosine(source, bandwidth) =>
  70. math.abs(source/bandwidth) <= 1 ? (math.pi / 4) * math.cos((math.pi / 2) * (source/bandwidth)) : 0.0
  71.  
  72. laplace(source, bandwidth) =>
  73. (1 / (2 * bandwidth)) * math.exp(-math.abs(source/bandwidth))
  74.  
  75. exponential(source, bandwidth) =>
  76. (1 / bandwidth) * math.exp(-math.abs(source/bandwidth))
  77.  
  78.  
  79. silverman(source, bandwidth) =>
  80. if math.abs(source/bandwidth) <= 0.5
  81. 0.5 * math.exp(-(source/bandwidth)/2) * math.sin((source/bandwidth)/2 + math.pi/4)
  82. else
  83. 0.0
  84.  
  85. tent(source, bandwidth) =>
  86. if math.abs(source/bandwidth) <= 1
  87. 1 - math.abs(source/bandwidth)
  88. else
  89. 0.0
  90.  
  91. cauchy(source, bandwidth) =>
  92. 1 / (math.pi * bandwidth * (1 + sq(source / bandwidth)))
  93.  
  94. sinc(source, bandwidth) =>
  95. if source == 0
  96. 1
  97. else
  98. math.sin(math.pi * source / bandwidth) / (math.pi * source / bandwidth)
  99.  
  100. wave(source, bandwidth) =>
  101. if (math.abs(source/bandwidth) <= 1)
  102. (1 - math.abs(source/bandwidth)) * math.cos((math.pi * source) / bandwidth)
  103. else
  104. 0.0
  105.  
  106. parabolic(source, bandwidth) =>
  107. if math.abs(source/bandwidth) <= 1
  108. 1 - math.pow((source/bandwidth), 2)
  109. else
  110. 0.0
  111.  
  112. power(source, bandwidth) =>
  113. if (math.abs(source/bandwidth) <= 1)
  114. math.pow(1 - math.pow(math.abs(source/bandwidth), 3), 3)
  115. else
  116. 0.0
  117.  
  118. loglogistic(source, bandwidth) =>
  119. 1 / math.pow(1 + math.abs(source / bandwidth), 2)
  120.  
  121. morters(source, bandwidth) =>
  122. if math.abs(source / bandwidth) <= math.pi
  123. (1 + math.cos(source / bandwidth)) / (2 * math.pi * bandwidth)
  124. else
  125. 0.0
  126.  
  127. kernel(source, bandwidth, style)=>
  128. switch style
  129. "Triangular" => triangular(source, bandwidth)
  130. "Gaussian" => gaussian(source, bandwidth)
  131. "Epanechnikov" => epanechnikov(source, bandwidth)
  132. "Logistic" => logistic(source, bandwidth)
  133. "Log Logistic" => loglogistic(source, bandwidth)
  134. "Cosine" => cosine(source, bandwidth)
  135. "Sinc" => sinc(source, bandwidth)
  136. "Laplace" => laplace(source, bandwidth)
  137. "Quartic" => quartic(source, bandwidth)
  138. "Parabolic" => parabolic(source, bandwidth)
  139. "Exponential" => exponential(source, bandwidth)
  140. "Silverman" => silverman(source, bandwidth)
  141. "Cauchy" => cauchy(source, bandwidth)
  142. "Tent" => tent(source, bandwidth)
  143. "Wave" => wave(source, bandwidth)
  144. "Power" => power(source, bandwidth)
  145. "Morters" => morters(source, bandwidth)
  146.  
  147. multi_kernel_regression(source, bandwidth, deviations, style, lables, enable, line_style, text_color, bullish_color, bearish_color, size, repaint)=>
  148. var estimate_array = array.new<line>(501, line.new(na, na, na, na))
  149. var dev_upper_array = array.new<line>(501, line.new(na, na, na, na))
  150. var dev_lower_array = array.new<line>(501, line.new(na, na, na, na))
  151. var up_labels = array.new<label>(501, label.new(na, na))
  152. var down_labels = array.new<label>(501, label.new(na, na))
  153.  
  154. float current_price = na
  155. float previous_price = na
  156. float previous_price_delta = na
  157. float std_dev = na
  158. float upper_1 = na
  159. float lower_1 = na
  160. float upper_2 = na
  161. float lower_2 = na
  162. line estimate = na
  163. line dev_upper = na
  164. line dev_lower = na
  165. label bullish = na
  166. label bearish = na
  167.  
  168. if not repaint
  169. float sum = 0.0
  170. float sumw = 0.0
  171. float sumsq = 0.0
  172. for i = 0 to bandwidth - 1
  173. j = math.pow(i, 2) / (math.pow(bandwidth, 2))
  174. weight = kernel(j, 1, style)
  175. sum += source[i] * weight
  176. sumw += weight
  177. mean = sum / sumw
  178. direction = mean - mean[1] > 0
  179. direction_color = direction ? bullish_color : bearish_color
  180. for i = 0 to bandwidth - 1
  181. sumsq += math.pow(source[i] - mean[i], 2)
  182. stdev = math.sqrt(sumsq / (bandwidth - 1)) * deviations
  183. array.unshift(estimate_array, line.new(bar_index, mean, bar_index - 1, mean[1], xloc.bar_index, extend.none, direction_color, line_style, 3))
  184.  
  185. if enable
  186. array.unshift(dev_upper_array, line.new(bar_index, mean + stdev, bar_index - 1, mean[1] + stdev[1], xloc.bar_index, extend.none, direction_color, line_style, 3))
  187. array.unshift(dev_lower_array, line.new(bar_index, mean - stdev, bar_index - 1, mean[1] - stdev[1], xloc.bar_index, extend.none, direction_color, line_style, 3))
  188.  
  189. if lables
  190. if direction and not direction[1]
  191. array.unshift(up_labels, label.new(bar_index, source, "Up", yloc = yloc.belowbar, color = bullish_color, style = label.style_label_up, textcolor = text_color, size = size))
  192. if not direction and direction[1]
  193. array.unshift(down_labels, label.new(bar_index, source, "Down", yloc = yloc.abovebar, color = bearish_color, style = label.style_label_down, textcolor = text_color, size = size))
  194. else
  195. if barstate.isfirst
  196. for i = 500 to 0
  197. array.set(estimate_array, i, line.new(na, na, na, na))
  198. if enable
  199. array.set(dev_upper_array, i, line.new(na, na, na, na))
  200. array.set(dev_lower_array, i, line.new(na, na, na, na))
  201. if lables
  202. array.set(up_labels, i, label.new(na, na))
  203. array.set(down_labels,i, label.new(na, na))
  204.  
  205. if barstate.islast
  206. for i = 0 to math.min(bar_index - 1, 500)
  207. float sum = 0
  208. float sumw = 0
  209. float sumsq = 0
  210.  
  211. for j = 0 to math.min(bar_index - 1, 500)
  212. diff = i - j
  213. weight = kernel(diff, bandwidth, kernel)
  214. sum += source[j] * weight
  215. sumsq += sq(source[j]) * weight
  216. sumw += weight
  217. current_price := sum / sumw
  218. delta = current_price - previous_price
  219.  
  220. if enable
  221. std_dev := math.sqrt(math.max(sumsq / sumw - sq(current_price), 0))
  222. upper_2 := current_price + deviations * std_dev
  223. lower_2 := current_price - deviations * std_dev
  224.  
  225. estimate := array.get(estimate_array, i)
  226.  
  227. if enable
  228. dev_upper := array.get(dev_upper_array, i)
  229. dev_lower := array.get(dev_lower_array, i)
  230.  
  231. line.set_xy1(estimate, bar_index - i + 1, previous_price)
  232. line.set_xy2(estimate, bar_index - i, current_price)
  233. line.set_style(estimate, line_style)
  234. line.set_color(estimate, current_price > previous_price ? bearish_color : bullish_color)
  235. line.set_width(estimate, 3)
  236.  
  237. if enable
  238. line.set_xy1(dev_upper, bar_index - i + 1, upper_1)
  239. line.set_xy2(dev_upper, bar_index - i , upper_2)
  240. line.set_style(dev_upper, line_style)
  241. line.set_color(dev_upper, current_price > previous_price ? bearish_color : bullish_color)
  242. line.set_width(dev_upper, 3)
  243. line.set_xy1(dev_lower, bar_index - i + 1, lower_1)
  244. line.set_xy2(dev_lower, bar_index - i , lower_2)
  245. line.set_style(dev_lower, line_style)
  246. line.set_color(dev_lower, current_price > previous_price ? bearish_color : bullish_color)
  247. line.set_width(dev_lower, 3)
  248.  
  249. if lables
  250. bullish := array.get(up_labels, i)
  251. bearish := array.get(down_labels, i)
  252.  
  253. if delta > 0 and previous_price_delta < 0
  254. label.set_xy(bullish, bar_index - i + 1, source[i])
  255. label.set_text(bullish, 'Up')
  256. label.set_color(bullish, bullish_color)
  257. label.set_textcolor(bullish, text_color)
  258. label.set_textalign(bullish, text.align_center)
  259. label.set_size(bullish, size)
  260. label.set_style(bullish, label.style_label_up)
  261. label.set_yloc(bullish, yloc.belowbar)
  262.  
  263. if delta < 0 and previous_price_delta > 0
  264. label.set_xy(bearish, bar_index - i + 1, source[i])
  265. label.set_text(bearish, 'Down')
  266. label.set_textcolor(bearish, text_color)
  267. label.set_color(bearish, bearish_color)
  268. label.set_textalign(bearish, text.align_center)
  269. label.set_size(bearish, size)
  270. label.set_style(bearish, label.style_label_down)
  271. label.set_yloc(bearish, yloc.abovebar)
  272.  
  273. previous_price := current_price
  274. upper_1 := upper_2
  275. lower_1 := lower_2
  276. previous_price_delta := delta
  277.  
  278. if barstate.isconfirmed
  279. for i = array.size(up_labels) - 1 to 0
  280. label.set_xy(array.get(up_labels, i), na, na)
  281. for i = array.size(down_labels) - 1 to 0
  282. label.set_xy(array.get(down_labels, i), na, na)
  283.  
  284. multi_kernel_regression(source, bandwidth, deviations, kernel, lables, enable, line_style, text_color, bullish_color, bearish_color, size, repaint)
  285.  
  286. // Define entry conditions
  287. longCondition = delta > 0 and previous_price_delta < 0
  288. if (longCondition)
  289. strategy.entry("Long", strategy.long)
  290.  
  291. shortCondition = delta < 0 and previous_price_delta > 0
  292. if (shortCondition)
  293. strategy.entry("Short", strategy.short)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement