The ( ) adapts to price movement based on the of phase as measured by the Hilbert Transform Discriminator. This method features a fast attack average and a slow decay average so that composite average rapidly ratchets behind price changes and holds the average value until the next ratchet occurs. Consider FAMA (Following AMA) as the signal.
Here are some of the options:
Fill /FAMA region (ribbon mode):
The above options (along with the bar colors) allow this to be used as a standalone system.
BTW, John calls , "Mother of all Adaptive Moving Averages", lemme know what you think :)
- , Stocks and Magazine, August 2001
- MAMA: http://www.mesasoftware.com/papers/MAMA....
List of my public indicators: http://bit.ly/1LQaPK8
List of my app-store indicators: http://blog.tradingview.com/?p=970
// // @author LazyBear // // List of my public indicators: http://bit.ly/1LQaPK8 // List of my app-store indicators: http://blog.tradingview.com/?p=970 // study("Ehlers MESA Adaptive Moving Average [LazyBear]", shorttitle="EMAMA_LB", overlay=true, precision=3) src=input(hl2, title="Source") fl=input(.5, title="Fast Limit") sl=input(.05, title="Slow Limit") sp = (4*src + 3*src + 2*src + src) / 10.0 dt = (.0962*sp + .5769*nz(sp) - .5769*nz(sp)- .0962*nz(sp))*(.075*nz(p) + .54) q1 = (.0962*dt + .5769*nz(dt) - .5769*nz(dt)- .0962*nz(dt))*(.075*nz(p) + .54) i1 = nz(dt) jI = (.0962*i1 + .5769*nz(i1) - .5769*nz(i1)- .0962*nz(i1))*(.075*nz(p) + .54) jq = (.0962*q1 + .5769*nz(q1) - .5769*nz(q1)- .0962*nz(q1))*(.075*nz(p) + .54) i2_ = i1 - jq q2_ = q1 + jI i2 = .2*i2_ + .8*nz(i2) q2 = .2*q2_ + .8*nz(q2) re_ = i2*nz(i2) + q2*nz(q2) im_ = i2*nz(q2) - q2*nz(i2) re = .2*re_ + .8*nz(re) im = .2*im_ + .8*nz(im) p1 = iff(im!=0 and re!=0, 360/atan(im/re), nz(p)) p2 = iff(p1 > 1.5*nz(p1), 1.5*nz(p1), iff(p1 < 0.67*nz(p1), 0.67*nz(p1), p1)) p3 = iff(p2<6, 6, iff (p2 > 50, 50, p2)) p = .2*p3 + .8*nz(p3) spp = .33*p + .67*nz(spp) phase = atan(q1 / i1) dphase_ = nz(phase) - phase dphase = iff(dphase_< 1, 1, dphase_) alpha_ = fl / dphase alpha = iff(alpha_ < sl, sl, iff(alpha_ > fl, fl, alpha_)) mama = alpha*src + (1 - alpha)*nz(mama) fama = .5*alpha*mama + (1 - .5*alpha)*nz(fama) pa=input(false, title="Mark crossover points") plotarrow(pa?(cross(mama, fama)?mama<fama?-1:1:na):na, title="Crossover Markers") fr=input(false, title="Fill MAMA/FAMA Region") duml=plot(fr?(mama>fama?mama:fama):na, style=circles, color=gray, linewidth=0, title="DummyL") mamal=plot(mama, title="MAMA", color=red, linewidth=2) famal=plot(fama, title="FAMA", color=green, linewidth=2) fill(duml, mamal, red, transp=70, title="NegativeFill") fill(duml, famal, green, transp=70, title="PositiveFill") ebc=input(false, title="Enable Bar colors") bc=mama>fama?lime:red barcolor(ebc?bc:na)
Unfortunately in the case of MESA - this indicator is simply incorrect and does not work as expected - sadly.
I coded quite a number of indis by now from Ehlers original presentations and books, and at first I could not quite grasp why LB MESA doe not look like anything in Ehlers presentations. You can check how it should look in Ehlers presentation here:
The stringently examining LazyBear's code I figured where the problem - it is actually in the two crucial parts of calculating the period and phase value.
The issue is that TV uses radians in trigonometric functions, whereas Ehlres formula refers to degrees - and the conversion is required for the formula to work properly.
Period, line 21 must
p1 = iff(im!=0 and re!=0, 360/atan(im/re), nz(p))
p1 = iff(im!=0 and re!=0, 2*pi/atan(im/re), nz(p)), where pi = 3.14.5926
Phase, line 30
phase = atan(q1 / i1)
phase = 180/pi * atan(q1 / i1)
Until these corrections are made - the alpha value for MAM on line 34 is always bigger than fl=0.5, and as a result it never changes in the later calculation of the EMA. This MESA simply does not adapt to price changes. The alpha value stays constant 0.5 resulting in plotting the EMA(3) basically all the time. And the FAMA becomes EMA(7) of MAMA.
I believe I spotted similar coding issue in some other Ehlers based indicators, I can't unfortunately recall anymore which ones.
But apart from that I can only wish LazyBear to continue his great work for the community, big respect!
And this is how my version of MESA looks like
The choppiness of MESA is the confirmation of its adaptive nature. On fast price advances, it quickly slides along, whereas in the congested areas it flattens on retracements, because the alpha decreases and the EMA period increases.
Hope this brings big profits to everybody, all the best!
p1 = iff(im!=0 and re!=0, 2*pi/atan(im/re), nz(p))
line 30 (old count again):
phase = 180/pi * atan(q1 / i1)
that should be all.
And I think I've finally figured out what causes earlier submission of my messages = if I paste a piece of code that has an invisible end-of-line character (cr/lf), the form treats it as ENTER button and submits the message. maybe this his something TradingView team can fix?