Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- study("Kalman MA", shorttitle="Special Design KLMF", overlay=true, max_bars_back = 5000)
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- length = input(10, title='Kalman MA Length', minval=6)
- //lenhull = input(6, title='Hull Length', minval=6)
- flexy = input(0.5,step=0.01,title='Flexibility permissible in %')
- robust = input(true)
- show_klhul = input(true, title='Display Trend-Step Filter ═════════════════')
- show_klhus = input(false, title='Display Trend-Step shading ════════════════')
- phz = length / 2
- phz_hilo = length / 2
- pp = input(close, title='Kalman MA Source')
- value1 = 0.0
- value2 = 0.0
- klmf = 0.0
- value1 := 0.2 * (pp - pp[1]) + 0.8 * nz(value1[1])
- value2 := 0.1 * (high - low) + 0.8 * nz(value2[1])
- lambda = abs(value1 / value2)
- alpha = (-lambda * lambda +
- sqrt(lambda * lambda * lambda * lambda + 16 * lambda * lambda)) / 8
- klmf := alpha * pp + (1 - alpha) * nz(klmf[1])
- acx = wma(wma(klmf, phz / 3) * 3 - wma(klmf, phz / 2) - wma(klmf, phz), phz) //Hull Conversion
- //plot(klmf,linewidth=2, color=color.maroon, title="klmf", editable = true,transp=0)
- value1h = 0.0
- value2h = 0.0
- klmfh = 0.0
- value1h := 0.2 * (high - high[1]) + 0.8 * nz(value1h[1])
- value2h := 0.1 * (high - low) + 0.8 * nz(value2h[1])
- lambdah = abs(value1h / value2h)
- alphah = (-lambdah * lambdah +
- sqrt(lambdah * lambdah * lambdah * lambdah + 16 * lambdah * lambdah)) / 8
- klmfh := alphah * high + (1 - alphah) * nz(klmfh[1])
- ahx = wma(wma(klmfh, phz_hilo / 3) * 3 - wma(klmfh, phz_hilo / 2) - wma(klmfh, phz_hilo), phz_hilo) //Hull Conversion
- //plot(klmfh,linewidth=3, color=color.blue, title="klmf", editable = true)
- value1l = 0.0
- value2l = 0.0
- klmfl = 0.0
- value1l := 0.2 * (low - low[1]) + 0.8 * nz(value1l[1])
- value2l := 0.1 * (high - low) + 0.8 * nz(value2l[1])
- lambdal = abs(value1l / value2l)
- alphal = (-lambdal * lambdal +
- sqrt(lambdal * lambdal * lambdal * lambdal + 16 * lambdal * lambdal)) / 8
- klmfl := alphal * low + (1 - alphal) * nz(klmfl[1])
- alx = wma(wma(klmfl, phz_hilo / 3) * 3 - wma(klmfl, phz_hilo / 2) - wma(klmfl, phz_hilo), phz_hilo) //Hull Conversion
- //plot(klmfl,linewidth=3, color=color.blue, title="klmf", editable = true)
- //=====================================================================
- //==== Setting-Less Trend-Step Filtering ==============
- //=====================================================================
- b = 0.0,sc = 0.0,a = 0.0,bo = 0.0,sco = 0.0,ao = 0.0
- bs = 0.0,scs = 0.0,as = 0.0, bl = 0.0,scl = 0.0,al = 0.0
- //===Kalman MA Close Setting-Less Trend-Step Filtering ================
- n = cum(1) - 1
- sc := robust ? abs(acx - nz(b[1])) / (abs(acx - nz(b[1])) + nz(a[1])) : 1
- src = sc * acx + (1 - sc) * nz(b[1], acx)
- a := cum(abs(src - nz(b[1], src))) / n * (iff(robust, 1, 0) + sc)
- b := src > nz(b[1], src) + a ? src : src < nz(b[1], src) - a ? src : nz(b[1], src)
- //plot(b, color=color.black, linewidth=1, transp=0)
- //===Kalman MA High Setting-Less Trend-Step Filtering ================
- scs := robust ? abs(ahx - nz(bs[1])) / (abs(ahx - nz(bs[1])) + nz(as[1])) : 1
- srcs = scs * ahx + (1 - scs) * nz(bs[1], ahx)
- as := cum(abs(srcs - nz(bs[1], srcs))) / n * (iff(robust, 1, 0) + scs)
- bs := srcs > nz(bs[1], srcs) + as ? srcs :
- srcs < nz(bs[1], srcs) - as ? srcs : nz(bs[1], srcs)
- //plot(bs,color=#ff1100,linewidth=3,transp=0)
- //===Kalman MA Low Setting-Less Trend-Step Filtering ================
- scl := robust ? abs(alx - nz(bl[1])) / (abs(alx - nz(bl[1])) + nz(al[1])) : 1
- srcl = scl * alx + (1 - scl) * nz(bl[1], alx)
- al := cum(abs(srcl - nz(bl[1], srcl))) / n * (iff(robust, 1, 0) + scl)
- bl := srcl > nz(bl[1], srcl) + al ? srcl :
- srcl < nz(bl[1], srcl) - al ? srcl : nz(bl[1], srcl)
- //====================================================================
- osc = 0.0
- osc := change(b) > 0 ? bs : change(b) < 0 ? bl : nz(osc[1])
- clr_ = 0.0
- clr_ := change(osc) > 0 ? 1 : change(osc) < 0 ? -1 : nz(clr_[1])
- css = clr_ == 1 ? color.blue : clr_ == -1 ? color.orange:na
- //plot(show_klhul ? osc : na, color=css, linewidth=2, transp=0)
- bgcolor(show_klhus ? css : na, transp=75)
- clr1_ = 0.0, osc1=0.0
- b_sht = (bs+(bs*flexy/100))
- b_lng = (bl-(bl*flexy/100))
- osc1 := change(bl) > 0 ? b_sht : change(bs) < 0 ?b_lng : nz(osc1[1])
- clr1_ := change(osc1) > 0 ? 1 : change(osc1)< 0 ? -1 : nz(clr1_[1])
- css1 = clr1_ == 1 ? color.navy : color.red
- //bgcolor(show_klhus ? css1 : na, transp=75)
- plot(show_klhul ?b_sht:na, color=css1, style=plot.style_line, linewidth=3, transp=0)
- plot(show_klhul ?osc:na, color=css, linewidth=2, transp=0)
- plot(show_klhul ?b_lng:na, color=css1, style=plot.style_line,linewidth=3, transp=0)
- // //===================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement