Ehler's MAMA Buy and Sell Signals Indicator for ThinkorSwim

BenTen

BenTen

Administrative
Staff
VIP
Warehouse
This is the MAMA and FAMA indicator by John Ehlers. Testing on SPY and few other stocks and it is highly accurate on the certain timeframes and works best with SPY.

It will show MAMA: BEAR as an indication to sell and MAMA: BULL to indicate Bullish move. It was originally made to trade /ES.

The indicator performs worst in times of CHOP. So I would use a choppy indicator to indicate choppiness in the market and avoid taking any trades during that time.

Good Stuff: Overall if you trade(On sell trades) SPY on 2D 5M chart with will end up making roughly ~ $200. Lets not forget its simulation and not real trades



thinkScript Code

Rich (BB code):
# MESA Adaptive Moving Average (MAMA)



# Brian: Made slight changes to display on the price graph to see

# its effectiveness. The first 30 bars will be a simple MA

# 4.7.2016



# hint: <b>Ehler's Mesa Adaptive Moving Average</b> using Ray's clean version

# of the homodyne discriminator.

#

 

# MIT License

# Copyright (c) <2010> <Radford Juang>

#

#Permission is hereby granted, free of charge, to any person obtaining a copy

#of this software and associated documentation files (the "Software"), to deal

#in the Software without restriction, including without limitation the rights

#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

#copies of the Software, and to permit persons to whom the Software is

#furnished to do so, subject to the following conditions:

#

#The above copyright notice and this permission notice shall be included in

#all copies or substantial portions of the Software.

#

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

#THE SOFTWARE.

#

 

script WMA_Smooth {

input price = hl2;

plot smooth = (4 * price

+ 3 * price[1]

+ 2 * price[2]

+ price[3]) / 10;

}

 

script Phase_Accumulation {

# This is Ehler's Phase Accumulation code. It has a full cycle delay.

# However, it computes the correction factor to a very high degree.

#

input price = hl2;

 

rec Smooth;

rec Detrender;

rec Period;

rec Q1;

rec I1;

rec I1p;

rec Q1p;

rec Phase1;

rec Phase;

rec DeltaPhase;

rec DeltaPhase1;

rec InstPeriod1;

rec InstPeriod;

def CorrectionFactor;

 

if barNumber() <= 5

then {

Period = 0;

Smooth = 0;

Detrender = 0;

CorrectionFactor = 0;

Q1 = 0;

I1 = 0;

Q1p = 0;

I1p = 0;

Phase = 0;

Phase1 = 0;

DeltaPhase1 = 0;

DeltaPhase = 0;

InstPeriod = 0;

InstPeriod1 = 0;

} else {

CorrectionFactor = 0.075 * Period[1] + 0.54;

 

# Smooth and detrend my smoothed signal:

Smooth = WMA_Smooth(Price);

Detrender = ( 0.0962 * Smooth

+ 0.5769 * Smooth[2]

- 0.5769 * Smooth[4]

- 0.0962 * Smooth[6] ) * CorrectionFactor;

 

# Compute Quadrature and Phase of Detrended signal:

Q1p = ( 0.0962 * Detrender

+ 0.5769 * Detrender[2]

- 0.5769 * Detrender[4]

- 0.0962 * Detrender[6] ) * CorrectionFactor;

I1p = Detrender[3];

 

# Smooth out Quadrature and Phase:

I1 = 0.15 * I1p + 0.85 * I1p[1];

Q1 = 0.15 * Q1p + 0.85 * Q1p[1];

 

# Determine Phase

if I1 != 0 then {

# Normally, ATAN gives results from -pi/2 to pi/2.

# We need to map this to circular coordinates 0 to 2pi

 

if Q1



    >= 0 and I1 > 0 then { # Quarant 1



Phase1 = ATan(absValue(Q1/I1));

} else if Q1 >= 0 and I1 < 0 then { # Quadrant 2

Phase1 = Double.PI - ATan(absValue(Q1/I1));

} else if Q1 < 0 and I1 < 0 then { # Quadrant 3

Phase1 = Double.PI + ATan(absValue(Q1/I1));

} else { # Quadrant 4

Phase1 = 2*Double.PI - ATan(absValue(Q1/I1));

}

} else if Q1 > 0 then { # I1 == 0, Q1 is positive

Phase1 = Double.PI/2;

} else if Q1 < 0 then { # I1 == 0, Q1 is negative

Phase1 = 3*Double.PI/2;

} else { # I1 and Q1 == 0

Phase1 = 0;

}

 

# Convert phase to degrees

Phase = Phase1 * 180 / Double.PI;

 

if Phase[1] < 90 and Phase > 270 then {

# This occurs when there is a big jump from 360-0

DeltaPhase1 = 360 + Phase[1] - Phase;

} else {

DeltaPhase1 = Phase[1] - Phase;

}

 

# Limit our delta phases between 7 and 60

if DeltaPhase1 < 7

then {

DeltaPhase = 7;

} else if DeltaPhase1 > 60 then {

DeltaPhase = 60;

} else {

DeltaPhase = DeltaPhase1;

}

 

# Determine Instantaneous period:

InstPeriod1 =

-1*(fold i=0 to 40 with v=0 do

if v < 0 then

v

else if v > 360 then

-i

else

v + getValue(DeltaPhase, i, 41)

);

 

if InstPeriod1 <= 0 then {

InstPeriod = InstPeriod[1];

} else {

InstPeriod = InstPeriod1;

}

 

Period = 0.25*InstPeriod + 0.75*Period[1];

}

plot DC = period;

}

 

script Ehler_MAMA {

input price = hl2;

input FastLimit = 0.5;

input SlowLimit = 0.05;

 

rec Period;

rec Period_raw;

rec Period_cap;

rec Period_lim;

 

rec Smooth;

rec Detrender;

rec I1;

rec Q1;

rec jI;

rec jQ;

rec I2;

rec Q2;

rec I2_raw;

rec Q2_raw;

 

rec Phase;

rec DeltaPhase;

rec DeltaPhase_raw;

rec alpha;

rec alpha_raw;

 

rec Re;

rec Im;

rec Re_raw;

rec Im_raw;

 

rec SmoothPeriod;

rec vmama;

rec vfama;

 

def CorrectionFactor = Phase_Accumulation(price).CorrectionFactor;

 

if barNumber() <= 5

then {

Smooth = 0;

Detrender = 0;

 

Period = 0;

Period_raw = 0;

Period_cap = 0;

Period_lim = 0;

I1 = 0;

Q1 = 0;

I2 = 0;

Q2 = 0;

jI = 0;

jQ = 0;

I2_raw = 0;

Q2_raw = 0;

Re = 0;

Im = 0;

Re_raw = 0;

Im_raw = 0;

SmoothPeriod = 0;

Phase = 0;

DeltaPhase = 0;

DeltaPhase_raw = 0;

alpha = 0;

alpha_raw = 0;

vmama = 0;

vfama = 0;

} else {

# Smooth and detrend my smoothed signal:

Smooth = WMA_Smooth(Price);

Detrender = ( 0.0962 * Smooth

+ 0.5769 * Smooth[2]

- 0.5769 * Smooth[4]

- 0.0962 * Smooth[6] ) * CorrectionFactor;

 

Q1 = ( 0.0962 * Detrender

+ 0.5769 * Detrender[2]

- 0.5769 * Detrender[4]

- 0.0962 * Detrender[6] ) * CorrectionFactor;

I1 = Detrender[3];

 

jI = ( 0.0962 * I1

+ 0.5769 * I1[2]

- 0.5769 * I1[4]

- 0.0962 * I1[6] ) * CorrectionFactor;

 

jQ = ( 0.0962 * Q1

+ 0.5769 * Q1[2]

- 0.5769 * Q1[4]

- 0.0962 * Q1[6] ) * CorrectionFactor;

 

# This is the complex conjugate

I2_raw = I1 - jQ;

Q2_raw = Q1 + jI;

 

I2 = 0.2*I2_raw + 0.8*I2_raw[1];

Q2 = 0.2*Q2_raw + 0.8*Q2_raw[1];

 

Re_raw = I2*I2[1] + Q2*Q2[1];

Im_raw = I2*Q2[1] - Q2*I2[1];

 

Re = 0.2*Re_raw + 0.8*Re_raw[1];

Im = 0.2*Im_raw + 0.8*Im_raw[1];

 

# Compute the phase

if Re != 0 and Im != 0 then {

Period_raw = 2*double.PI / atan(Im/Re);

} else {

Period_raw = 0;

}

 

if Period_raw > 1.5*Period_raw[1] then {

Period_cap = 1.5*Period_raw[1];

} else if Period_raw < 0.67 * Period_raw[1] {

Period_cap = 0.67 * Period_raw[1];

} else {

Period_cap = Period_raw;

}

 

if Period_cap < 6 then {

Period_lim = 6;

} else if Period_cap > 50 then {

Period_lim = 50;

} else {

Period_lim = Period_cap;

}

 

Period = 0.2*Period_lim + 0.8*Period_lim[1];

SmoothPeriod = 0.33*Period + 0.67*SmoothPeriod[1];

 

if I1 != 0 then {

Phase = ATan(Q1 / I1);

} else if Q1 > 0 then { # Quadrant 1:

Phase = Double.Pi/2;

} else if Q1 < 0 then { # Quadrant 4:

Phase = -Double.Pi/2;

} else { # Both numerator and denominator are 0.

Phase = 0;

}

 

DeltaPhase_raw = Phase[1] - Phase;

If DeltaPhase_raw < 1 then {

DeltaPhase = 1;

} else {

DeltaPhase = DeltaPhase_raw;

}

 

alpha_raw = FastLimit / DeltaPhase;

If alpha_raw < SlowLimit then {

alpha = SlowLimit;

} else {

alpha = alpha_raw;

}

vmama = alpha*Price + (1 - alpha)*vmama[1];

vfama = 0.5*alpha*vmama + (1 - 0.5*alpha)*vfama[1];

}

 

plot MAMA = vmama;

plot FAMA = vfama;

}

 

declare upper;

input price = hl2;

input FastLimit = 0.5;

input SlowLimit = 0.05;

 

### plot MAMA = Ehler_MAMA(price, FastLimit, SlowLimit).MAMA;

### Plot FAMA = Ehler_MAMA(price, FastLimit, SlowLimit).FAMA;



plot MAMA = compoundValue(20,Ehler_MAMA(price, FastLimit, SlowLimit).MAMA,average(close,3));

plot FAMA = compoundValue(30,Ehler_MAMA(price, FastLimit, SlowLimit).FAMA,average(close,7));

 

### Plot MADIFF = (mama-fama);

### madiff.SetPaintingStrategy(paintingStrategy.HISTOGRAM);

### madiff.AssignValueColor (if madiff >= 0.00 then (color.green) else (color.red));

 

### Plot MALine = madiff;

### maline.AssignValueColor (if madiff >= 0.00 then (color.green) else (color.red));



# End Study

Shareable Link

http://tos.mx/1BjtpK



http://tos.mx/nw7xS1

@jerrydinvestor contributed to this post.
 
Last edited:
Thread starter Similar threads Forum Replies Date
M Questions 1

Top