This post shares a simple but effective Mean Reversion Expert Advisor (EA) for MetaTrader 5 (MT5), using Bollinger Bands to detect price extremes. I’ll walk through the strategy, show you the MQL5 code, and present the actual backtest results from 2025 using USDJPY on the H1 timeframe.
The EA enters trades when the market price touches the outer Bollinger Bands:
Below is the complete working version of the EA. You can copy, paste, and compile it in MetaEditor:
#include <Trade\\Trade.mqh>
CTrade trade;
input int bbPeriod = 20;
input double bbDeviation = 2.0;
input double lotSize = 0.1;
input int slippage = 5;
input double stopLoss = 300;
input double takeProfit = 300;
input ENUM_TIMEFRAMES tf = PERIOD_CURRENT;
int bbHandle;
int OnInit()
{
Print("Mean Reversion EA initialized");
bbHandle = iBands(_Symbol, tf, bbPeriod, bbDeviation, 0, PRICE_CLOSE);
if (bbHandle == INVALID_HANDLE)
{
Print("Failed to create Bollinger Bands handle");
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
void OnTick()
{
if (PositionSelect(_Symbol)) return;
double upper[], middle[], lower[];
if (CopyBuffer(bbHandle, 0, 0, 2, upper) <= 0 ||
CopyBuffer(bbHandle, 1, 0, 2, middle) <= 0 ||
CopyBuffer(bbHandle, 2, 0, 2, lower) <= 0)
{
Print("Failed to get Bollinger Band values");
return;
}
double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
if (price < lower[0])
{
double sl = price - stopLoss * _Point;
double tp = price + takeProfit * _Point;
trade.Buy(lotSize, _Symbol, price, sl, tp, "MeanReversionBuy");
}
else if (price > upper[0])
{
double sl = price + stopLoss * _Point;
double tp = price - takeProfit * _Point;
trade.Sell(lotSize, _Symbol, price, sl, tp, "MeanReversionSell");
}
}
void OnDeinit(const int reason)
{
Print("EA deinitialized");
IndicatorRelease(bbHandle);
}
Backtested on USDJPY (H1), using OANDA demo data.
| Metric | Value |
|---|---|
| Total Net Profit | ¥2.26 |
| Gross Profit | ¥64.20 |
| Gross Loss | ¥-61.94 |
| Profit Factor | > 1.00 |
| Balance Drawdown (Abs) | ¥0.00 |
| Equity Drawdown (Abs) | ¥0.19 |
| Total Trades | 26 |
| AHPR / GHPR | ~1.000 |
To scale this strategy:
bbPeriod, bbDeviation, SL/TP valuesThis EA is a great base for a mean reversion portfolio. It’s already profitable with minimal drawdown, and it offers a strong foundation to build from.
If you’d like the optimized version or want to follow my next EAs, stay tuned!