# EMA and MAMA Indicators Based Trading Strategy

Author: ChaoZhang, Date: 2023-10-31 14:20:56
Tags: ## Overview

This strategy is based on the EMA (Exponential Moving Average) and MAMA (MESA Adaptive Moving Average) indicators to determine the trend direction and generate trading signals according to their crossovers. EMA is often used to judge the market trend direction, while MAMA can more accurately capture market turning points. Using both can improve the performance of the strategy.

## Strategy Logic

1. Calculate the fast EMA and slow EMA, which reflect the short-term and long-term trends of the market respectively.

2. Calculate the MAMA and FAMA lines, which are adaptive moving averages.

3. When the fast EMA crosses above the slow EMA, a buy signal is generated.

4. When the fast EMA crosses below the slow EMA, a sell signal is generated.

5. When MAMA crosses above FAMA, a buy signal is generated.

6. When MAMA crosses below FAMA, a sell signal is generated.

7. Crossovers of MAMA and FAMA can be used to confirm EMA signals or provide early detection of trend turns.

Specifically, the strategy first calculates the fast EMA (fl) and slow EMA (sl), reflecting short-term and long-term trends respectively.

Then it calculates MAMA and FAMA based on John Ehlers’ formula:

1. Calculate the Hilbert Transform of price and extract phase information of the signal.

2. Calculate the instantaneous frequency p based on the phase information.

3. Calculate the weighting factor α based on the p value.

4. Calculate MAMA and FAMA based on α.

Finally, trading signals are generated based on EMA and MAMA/FAMA crossovers:

• Long when EMA crosses above
• Short when EMA crosses below
• Long when MAMA crosses above FAMA
• Short when MAMA crosses below FAMA

This strategy combines the advantages of EMA and MAMA indicators to improve the accuracy of trading signals.

• Effectively smooth price data and reduce noise
• Track trends well with some lag
• Flexible parameters to adjust sensitivity

• Adaptive parameters, no need for manual period setting
• Fast response to capture trend turns early
• Accurately identify support and resistance

• EMA determines the overall trend
• MAMA verifies signals and detects turns early
• Improved accuracy and win rate of signals

## Risk Analysis

The main risks of this strategy:

• EMA and MAMA are lagging indicators, entry signals may have some delay and slippage
• Frequent crossovers in ranging markets causing whipsaws
• Improper parameter settings lead to missing trends or fake signals

Solutions:

• Use stop loss to control loss
• Choose reasonable parameters, avoid being too sensitive
• Combine with other indicators to confirm signals

## Optimization Directions

The strategy can be optimized in the following aspects:

• Optimize EMA periods based on symbol characteristics
• Fine tune MAMA alpha sensitivity to better capture turns
• Add other filters like MACD, RSI to avoid fake signals
• Add stop loss to control risks
• Backtest to find optimal parameters
• Automate take profit for maximized profit

## Summary

This strategy integrates the strengths of EMA and MAMA indicators to follow the trend and capture turns timely. With parameter optimization and risk control, it can achieve improved win rate and profitability. But users should still apply caution based on personal risk preference.

```/*backtest
start: 2023-09-30 00:00:00
end: 2023-10-30 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
strategy("EMAMA strategy", overlay=true)
//This entire strategy is courtesy of LazyBear for programming the original EMAMA system, I simply added a strategy element to everything to round things out.

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)

longCondition = crossover(mama, fama)
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(mama, fama)
if (shortCondition)
strategy.entry("My Short Entry Id", strategy.short)
```

More