Advertisement
aaahopper

Kalman MA Bands

Jan 14th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. //@version=4
  2. study("Kalman MA", shorttitle="Special Design KLMF", overlay=true, max_bars_back = 5000)
  3.  
  4. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  5. length = input(10, title='Kalman MA Length', minval=6)
  6. //lenhull = input(6, title='Hull Length', minval=6)
  7. flexy = input(0.5,step=0.01,title='Flexibility permissible in %')
  8. robust = input(true)
  9.  
  10. show_klhul = input(true, title='Display Trend-Step Filter ═════════════════')
  11. show_klhus = input(false, title='Display Trend-Step shading ════════════════')
  12.  
  13. phz = length / 2
  14. phz_hilo = length / 2
  15. pp = input(close, title='Kalman MA Source')
  16. value1 = 0.0
  17. value2 = 0.0
  18. klmf = 0.0
  19. value1 := 0.2 * (pp - pp[1]) + 0.8 * nz(value1[1])
  20. value2 := 0.1 * (high - low) + 0.8 * nz(value2[1])
  21. lambda = abs(value1 / value2)
  22. alpha = (-lambda * lambda +
  23. sqrt(lambda * lambda * lambda * lambda + 16 * lambda * lambda)) / 8
  24. klmf := alpha * pp + (1 - alpha) * nz(klmf[1])
  25. acx = wma(wma(klmf, phz / 3) * 3 - wma(klmf, phz / 2) - wma(klmf, phz), phz) //Hull Conversion
  26. //plot(klmf,linewidth=2, color=color.maroon, title="klmf", editable = true,transp=0)
  27.  
  28. value1h = 0.0
  29. value2h = 0.0
  30. klmfh = 0.0
  31. value1h := 0.2 * (high - high[1]) + 0.8 * nz(value1h[1])
  32. value2h := 0.1 * (high - low) + 0.8 * nz(value2h[1])
  33. lambdah = abs(value1h / value2h)
  34. alphah = (-lambdah * lambdah +
  35. sqrt(lambdah * lambdah * lambdah * lambdah + 16 * lambdah * lambdah)) / 8
  36. klmfh := alphah * high + (1 - alphah) * nz(klmfh[1])
  37. ahx = wma(wma(klmfh, phz_hilo / 3) * 3 - wma(klmfh, phz_hilo / 2) - wma(klmfh, phz_hilo), phz_hilo) //Hull Conversion
  38. //plot(klmfh,linewidth=3, color=color.blue, title="klmf", editable = true)
  39.  
  40. value1l = 0.0
  41. value2l = 0.0
  42. klmfl = 0.0
  43. value1l := 0.2 * (low - low[1]) + 0.8 * nz(value1l[1])
  44. value2l := 0.1 * (high - low) + 0.8 * nz(value2l[1])
  45. lambdal = abs(value1l / value2l)
  46. alphal = (-lambdal * lambdal +
  47. sqrt(lambdal * lambdal * lambdal * lambdal + 16 * lambdal * lambdal)) / 8
  48. klmfl := alphal * low + (1 - alphal) * nz(klmfl[1])
  49. alx = wma(wma(klmfl, phz_hilo / 3) * 3 - wma(klmfl, phz_hilo / 2) - wma(klmfl, phz_hilo), phz_hilo) //Hull Conversion
  50. //plot(klmfl,linewidth=3, color=color.blue, title="klmf", editable = true)
  51.  
  52. //=====================================================================
  53. //==== Setting-Less Trend-Step Filtering ==============
  54. //=====================================================================
  55. b = 0.0,sc = 0.0,a = 0.0,bo = 0.0,sco = 0.0,ao = 0.0
  56. bs = 0.0,scs = 0.0,as = 0.0, bl = 0.0,scl = 0.0,al = 0.0
  57.  
  58. //===Kalman MA Close Setting-Less Trend-Step Filtering ================
  59. n = cum(1) - 1
  60. sc := robust ? abs(acx - nz(b[1])) / (abs(acx - nz(b[1])) + nz(a[1])) : 1
  61. src = sc * acx + (1 - sc) * nz(b[1], acx)
  62. a := cum(abs(src - nz(b[1], src))) / n * (iff(robust, 1, 0) + sc)
  63. b := src > nz(b[1], src) + a ? src : src < nz(b[1], src) - a ? src : nz(b[1], src)
  64. //plot(b, color=color.black, linewidth=1, transp=0)
  65.  
  66. //===Kalman MA High Setting-Less Trend-Step Filtering ================
  67. scs := robust ? abs(ahx - nz(bs[1])) / (abs(ahx - nz(bs[1])) + nz(as[1])) : 1
  68. srcs = scs * ahx + (1 - scs) * nz(bs[1], ahx)
  69. as := cum(abs(srcs - nz(bs[1], srcs))) / n * (iff(robust, 1, 0) + scs)
  70. bs := srcs > nz(bs[1], srcs) + as ? srcs :
  71. srcs < nz(bs[1], srcs) - as ? srcs : nz(bs[1], srcs)
  72. //plot(bs,color=#ff1100,linewidth=3,transp=0)
  73.  
  74. //===Kalman MA Low Setting-Less Trend-Step Filtering ================
  75. scl := robust ? abs(alx - nz(bl[1])) / (abs(alx - nz(bl[1])) + nz(al[1])) : 1
  76. srcl = scl * alx + (1 - scl) * nz(bl[1], alx)
  77. al := cum(abs(srcl - nz(bl[1], srcl))) / n * (iff(robust, 1, 0) + scl)
  78. bl := srcl > nz(bl[1], srcl) + al ? srcl :
  79. srcl < nz(bl[1], srcl) - al ? srcl : nz(bl[1], srcl)
  80.  
  81. //====================================================================
  82.  
  83. osc = 0.0
  84. osc := change(b) > 0 ? bs : change(b) < 0 ? bl : nz(osc[1])
  85. clr_ = 0.0
  86. clr_ := change(osc) > 0 ? 1 : change(osc) < 0 ? -1 : nz(clr_[1])
  87. css = clr_ == 1 ? color.blue : clr_ == -1 ? color.orange:na
  88. //plot(show_klhul ? osc : na, color=css, linewidth=2, transp=0)
  89. bgcolor(show_klhus ? css : na, transp=75)
  90.  
  91. clr1_ = 0.0, osc1=0.0
  92. b_sht = (bs+(bs*flexy/100))
  93. b_lng = (bl-(bl*flexy/100))
  94. osc1 := change(bl) > 0 ? b_sht : change(bs) < 0 ?b_lng : nz(osc1[1])
  95. clr1_ := change(osc1) > 0 ? 1 : change(osc1)< 0 ? -1 : nz(clr1_[1])
  96. css1 = clr1_ == 1 ? color.navy : color.red
  97. //bgcolor(show_klhus ? css1 : na, transp=75)
  98.  
  99. plot(show_klhul ?b_sht:na, color=css1, style=plot.style_line, linewidth=3, transp=0)
  100. plot(show_klhul ?osc:na, color=css, linewidth=2, transp=0)
  101. plot(show_klhul ?b_lng:na, color=css1, style=plot.style_line,linewidth=3, transp=0)
  102. // //===================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement