# Adaptive moving average back testing and MT4 indicator code

Recently I found some interesting idea about adaptive look back idea here. I code the idea into my ftap system, and also a MetaTrader 4 indicator. In this article I will introduce the adaptive moving average, compare it with normal moving average, and share the MT4 code. The C++ code in ftap won't be shared though.

## How adaptive look back works

The idea of adaptive look back is, instead of using fixed look back period on indicators such as moving average, MACD, or RSI, volatility is considered in calculating the look back. There are various methods to calculate the adaptive look back. The method I used in this article is inspired by this.

## How adaptive moving average looks

I put a 34 period EMA, and a 34 period adaptive EMA on the same chart in MT4, the data is GBPUSD D1. The TrendFactor in Adaptive EMA is 0.5. The chart is below, red is Adaptive EMA, blue is normal EMA.

The adaptive EMA is not as smooth as normal EMA. There are several bars break the normal EMA but not the adaptive EMA.

## Comparison of moving average cross strategy

I did exponential moving average cross back testing using my ftap system. The goal is not to find any strategy. I only want to see the difference between adaptive look back and fixed look back.

The strategy is the very basic form: when 13 period exponential moving average cross above 34 period, enter long on previous bar's close and exit any short trades. When 13 period moving average cross below 34 period, enter short and exit any long trades. No any filter, no stop loss, no take profit, thus no anything effect the moving average itself. The time frame is H4. 28 major currency pairs and two metals are tested.

Equity curve of adaptive moving average cross:

Equity curve of normal fixed look back moving average cross:

Though very few pairs such as AUDCAD has big difference, which is not shown in the images, most instruments don't have much difference between adaptive moving average cross and the normal fixed one. And most are not profitable because this is the only very basic form of moving average cross.

Below is a snippet of back test report generated by ftap of adaptive moving average cross, GBPUSD H4.

Stats: Deposit: 100000 Balance: 89434 PossibleBestBalance: 253191 Profit: -10.57% CAGR: -1.97% PROM: -36480 ReturnByDrawDown: -115.347 RiskAdjustedReturn: -0.070 RarOfProm: -0.241 Ulcer: 11.850 PromUlcer: -2838.929 Drawdown By Equity: Max Drawdown: 27778.88 Max Drawdown Pips: 2122.0 Max Drawdown percentage: 24.49% Max Drawdown duration: 1213d Drawdown period count: 12 Average Drawdown: 4922.51 Average Drawdown Pips: 363.7 Total Drawdown: 59070.14 Total Drawdown Pips: 4364.0 Drawdown By Balance: Max Drawdown: 25710.04 Max Drawdown Pips: 2122.0 Max Drawdown percentage: 22.73% Max Drawdown duration: 1197d Drawdown period count: 5 Average Drawdown: 8958.88 Average Drawdown Pips: 872.8 Total Drawdown: 44794.38 Total Drawdown Pips: 4364.0 Exit reasons: Hold: 1 Manual: 239 Tested bars: 9447 Period: 20100103-18:00:00--20150814-17:00:00 2048d All: Net profit: -10565.66 P/L of profit: 0.92 Win rate: 0.71 Payoff ratio: 0.37 Gross profit: 125180.89 Gross lose: -135746.55 Net profit pips: -791 P/L of pips: 0.94 Gross profit pips: 12990 Gross lose pips: -13781 Possible profit pips: 16054 Possible profit: 153191.29 Total Number of trades: 240 P/L of trades: 2.48 Number winning trades: 171 Number losing trades: 69 Number breakeven trades: 0 Max consecutive wins: 12 Max consecutive loses: 4 Min Holding Period: 3hr Max Holding Period: 60d Average Holding Period: 7d

Below is a snippet of back test report generated by ftap of normal moving average cross, GBPUSD H4.

Stats: Deposit: 100000 Balance: 91397 PossibleBestBalance: 274671 Profit: -8.60% CAGR: -1.59% PROM: -37414 ReturnByDrawDown: -67.266 RiskAdjustedReturn: -0.052 RarOfProm: -0.226 Ulcer: 14.244 PromUlcer: -2454.369 Drawdown By Equity: Max Drawdown: 35087.45 Max Drawdown Pips: 2628.0 Max Drawdown percentage: 28.93% Max Drawdown duration: 1256d Drawdown period count: 24 Average Drawdown: 3855.23 Average Drawdown Pips: 255.0 Total Drawdown: 92525.54 Total Drawdown Pips: 6120.0 Drawdown By Balance: Max Drawdown: 32796.00 Max Drawdown Pips: 2628.0 Max Drawdown percentage: 27.12% Max Drawdown duration: 1246d Drawdown period count: 10 Average Drawdown: 6527.15 Average Drawdown Pips: 612.2 Total Drawdown: 65271.46 Total Drawdown Pips: 6122.0 Exit reasons: Hold: 1 Manual: 236 Tested bars: 9447 Period: 20100103-18:00:00--20150814-17:00:00 2048d All: Net profit: -8602.14 P/L of profit: 0.94 Win rate: 0.70 Payoff ratio: 0.41 Gross profit: 142045.70 Gross lose: -150647.84 Net profit pips: -371 P/L of pips: 0.97 Gross profit pips: 14249 Gross lose pips: -14620 Possible profit pips: 17681 Possible profit: 174671.49 Total Number of trades: 237 P/L of trades: 2.29 Number winning trades: 165 Number losing trades: 72 Number breakeven trades: 0 Max consecutive wins: 9 Max consecutive loses: 4 Min Holding Period: 3hr Max Holding Period: 49d Average Holding Period: 8d

## What's the advantage of adaptive look back

The adaptive moving average indicator looks not appealing. This is because that I didn't use any filter on the strategy I tested above. Adaptive indicators are sensitive to market condition changes, so it tends to give more signals than fixed look back counterpart, that also means it tends to give more false signals. So an adaptive indicator should not be used alone. It must be used together with other systems, such as trend filters.

There are several adaptive look back calculation methods there. The method in this blog is the least efficient one, and is my least favorite one. The other methods should work better.

I firmly believe adaptive indicators are the future of Forex indicators. The reason is simple. Adaptive indicators are driven by the market, while fixed look back indicators are driven by human.

## Download adaptive moving average indicator for MetaTrader 4

Click here to download the AdaptiveMovingAverage.mq4

The code is completely free. Your any improvement or feedback on the code is welcome.

The parameters:

- Period: the original look back period.
- Shift: the shift.
- Method: the moving average mode. The above three parameters are similar as the fixed look back moving average.
- VolatilityPeriod: the period used to compare the volatility. If it's 0, Period is used.
- TrendFactor: the factor affects how the look back is calculated. It's usually 0~1. A value of 0 makes the indicators same as the fixed look back indicator.

## Discussion