Advertisement
xmd79

Nadaraya-Watson 2 Envelope

Nov 18th, 2023
338
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 1 0
  1. //@version=5
  2. indicator("Nadaraya-Watson 2 Envelope", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back=500)
  3. //------------------------------------------------------------------------------
  4. //Settings
  5. //-----------------------------------------------------------------------------{
  6. h = input.float(8.,'Bandwidth', minval = 0)
  7. h2 = input.float(14.,'Bandwidth2', minval = 0)
  8. mult = input.float(3., minval = 0)
  9. mult2 = input.float(3., minval = 0)
  10. src = input(close, 'Source')
  11.  
  12. var ln = array.new_line(0)
  13. var ln2 = array.new_line(0)
  14.  
  15. repaint = input(true, 'Repainting Smoothing', tooltip = 'Repainting is an effect where the indicators historical output is subject to change over time. Disabling repainting will cause the indicator to output the endpoints of the calculations')
  16.  
  17. //Style
  18. upCss = input.color(color.teal, 'Colors', inline = 'inline1', group = 'Style')
  19. dnCss = input.color(color.red, '', inline = 'inline1', group = 'Style')
  20. upCss2 = input.color(color.blue, 'Colors', inline = 'inline2', group = 'Style')
  21. dnCss2 = input.color(color.orange, '', inline = 'inline2', group = 'Style')
  22.  
  23. //-----------------------------------------------------------------------------}
  24. //Functions
  25. //-----------------------------------------------------------------------------{
  26. //Gaussian window
  27. gauss(x, h) => math.exp(-(math.pow(x, 2)/(h * h * 2)))
  28.  
  29. n = bar_index
  30.  
  31. if barstate.isfirst and repaint
  32. for i = 0 to 249
  33. array.push(ln,line.new(na,na,na,na))
  34. array.push(ln2,line.new(na,na,na,na))
  35.  
  36. //-----------------------------------------------------------------------------}
  37. //End point method
  38. //-----------------------------------------------------------------------------{
  39. var coefs = array.new_float(0)
  40. var den = 0.
  41. var coefs2 = array.new_float(0)
  42. var den2 = 0.
  43.  
  44. if barstate.isfirst and not repaint
  45. for i = 0 to 249
  46. w = gauss(i, h)
  47. coefs.push(w)
  48. w2 = gauss(i, h2)
  49. coefs2.push(w2)
  50. den := coefs.sum()
  51. den2 := coefs2.sum()
  52.  
  53. out = 0.
  54. out2 = 0.
  55.  
  56. if not repaint
  57. for i = 0 to 249
  58. out += src[i] * coefs.get(i)
  59. out2 += src[i] * coefs2.get(i)
  60. out /= den
  61. mae = ta.sma(math.abs(src - out), 249) * mult
  62. upper = out + mae
  63. lower = out - mae
  64.  
  65. out2 /= den2
  66. mae2 = ta.sma(math.abs(src - out2), 249) * mult2
  67. upper2 = out2 + mae2
  68. lower2 = out2 - mae2
  69.  
  70.  
  71. //-----------------------------------------------------------------------------}
  72. //Compute and display NWE
  73. //-----------------------------------------------------------------------------{
  74. float y2 = na
  75. float y1 = na
  76.  
  77. float y22 = na
  78. float y12 = na
  79.  
  80. nwe = array.new<float>(0)
  81. nwe2 = array.new<float>(0)
  82. if barstate.islast and repaint
  83. sae = 0.
  84. sae2 = 0.
  85. //Compute and set NWE point
  86. for i = 0 to math.min(249,n - 1)
  87. sum = 0.
  88. sumw = 0.
  89. sum2 = 0.
  90. sumw2 = 0.
  91. //Compute weighted mean
  92. for j = 0 to math.min(249,n - 1)
  93. w = gauss(i - j, h)
  94. sum += src[j] * w
  95. sumw += w
  96. w2 = gauss(i - j, h2)
  97. sum2 += src[j] * w2
  98. sumw2 += w2
  99.  
  100. y2 := sum / sumw
  101. sae += math.abs(src[i] - y2)
  102. nwe.push(y2)
  103.  
  104. y22 := sum2 / sumw2
  105. sae2 += math.abs(src[i] - y22)
  106. nwe2.push(y22)
  107.  
  108. sae := sae / math.min(249,n - 1) * mult
  109. sae2 := sae2 / math.min(249,n - 1) * mult2
  110. for i = 0 to math.min(249,n - 1)
  111. if i%2 != 0
  112. line.new(n-i+1, y1 + sae, n-i, nwe.get(i) + sae, color = upCss)
  113. line.new(n-i+1, y1 - sae, n-i, nwe.get(i) - sae, color = dnCss)
  114. line.new(n-i+1, y12 + sae2, n-i, nwe2.get(i) + sae2, color = upCss)
  115. line.new(n-i+1, y12 - sae2, n-i, nwe2.get(i) - sae2, color = dnCss)
  116.  
  117. if src[i] > nwe.get(i) + sae and src[i+1] < nwe.get(i) + sae
  118. label.new(n-i, src[i], '▼', color = color(na), style = label.style_label_down, textcolor = dnCss, textalign = text.align_center)
  119. if src[i] < nwe.get(i) - sae and src[i+1] > nwe.get(i) - sae
  120. label.new(n-i, src[i], '▲', color = color(na), style = label.style_label_up, textcolor = upCss, textalign = text.align_center)
  121.  
  122. //if src[i] > nwe2.get(i) + sae2 and src[i+1] < nwe2.get(i) + sae2
  123. // label.new(n-i, src[i], '▼', color = color(na), style = label.style_label_down, textcolor = dnCss, textalign = text.align_center)
  124. //if src[i] < nwe2.get(i) - sae2 and src[i+1] > nwe.get(i) - sae2
  125. // label.new(n-i, src[i], '▲', color = color(na), style = label.style_label_up, textcolor = upCss, textalign = text.align_center)
  126.  
  127. y1 := nwe.get(i)
  128. y12 := nwe2.get(i)
  129.  
  130. //-----------------------------------------------------------------------------}
  131. //Dashboard
  132. //-----------------------------------------------------------------------------{
  133. var tb = table.new(position.top_right, 1, 1
  134. , bgcolor = #1e222d
  135. , border_color = #373a46
  136. , border_width = 1
  137. , frame_color = #373a46
  138. , frame_width = 1)
  139.  
  140. if repaint
  141. tb.cell(0, 0, 'Repainting Mode Enabled', text_color = color.white, text_size = size.small)
  142.  
  143. //-----------------------------------------------------------------------------}
  144. //Plot
  145. //-----------------------------------------------------------------------------}
  146. plot(repaint ? na : out + mae, 'Upper', upCss)
  147. plot(repaint ? na : out - mae, 'Lower', dnCss)
  148. plot(repaint ? na : out2 + mae2, 'Upper', upCss2)
  149. plot(repaint ? na : out2 - mae2, 'Lower', dnCss2)
  150.  
  151. //Crossing Arrows
  152. plotshape(ta.crossunder(close, out - mae) ? low : na, "Crossunder", shape.labelup, location.absolute, color(na), 0 , text = '▲', textcolor = upCss, size = size.tiny)
  153. plotshape(ta.crossover(close, out + mae) ? high : na, "Crossover", shape.labeldown, location.absolute, color(na), 0 , text = '▼', textcolor = dnCss, size = size.tiny)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement