Loading ad...

📉 Mean Reversion EA for MT5 – Code, Logic, and Results

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.

📐 Strategy Logic: Mean Reversion with Bollinger Bands

The EA enters trades when the market price touches the outer Bollinger Bands:

  • Buy when price is below the lower band
  • Sell when price is above the upper band
  • Set Stop Loss = 300 points, Take Profit = 300 points
  • Only one trade at a time (position checked)

🧠 MQL5 Code: MeanReversionEA.mq5

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);
}
  

📊 Backtest Results (USDJPY, H1, 2025/01/01–07/13)

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

🔍 Analysis

  • ✅ EA was profitable — even with a small gain
  • Drawdown is near zero, making this an extremely low-risk system
  • ⚠️ Profit is small — needs more volatility or higher volume

🚀 What’s Next?

To scale this strategy:

  • Try it on more volatile pairs (e.g., GBPJPY, XAUUSD)
  • Increase lot size gradually (while monitoring drawdown)
  • Add RSI or time filters for better entry quality
  • Run optimization for bbPeriod, bbDeviation, SL/TP values

💬 Final Thoughts

This 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!

Published: 2025-07-14
Keisuke Kurosawa
Keisuke Kurosawa : A C++/Python quant with a passion for backtesting and real-time bots, Kaito shares strategies, failures, and breakthroughs in the world of algorithmic trading.
Please log in to add a comment.
No comments yet. Be the first to comment!

Related Articles

Meta Traders

Meta Trader

Authors

Rei Nakamoto
Rei writes what others won’t — deep dives into the psychology of traders, financial culture, trading memes, and sometimes just... thoughts at 2AM after a bad day on MT5.
Sophia Takeda
With a background in macroeconomics, Sophia brings a sharp, data-driven perspective to market movements. She specializes in global equities, central bank policy, and sector trends.
Keisuke Kurosawa
A C++/Python quant with a passion for backtesting and real-time bots, Kaito shares strategies, failures, and breakthroughs in the world of algorithmic trading.