Complete .mq5 source code

EURUSD Scalping EA MT5: MQL5 Code, Backtest & Optimization Guide

A complete MQL5 scalping EA for EURUSD on MetaTrader 5, using EMA crossover with session time filter and spread control. Includes full source code, backtest results on M5, and optimization guide.

Historical test results

63.1%
Win Rate
9.8%
Max Drawdown
1.71
Sharpe Ratio
2021–2025
Test Period

Past performance is not indicative of future results. Backtest statistics are based on historical data and do not guarantee future profits. Trading involves significant risk of loss. This content is for educational purposes only and does not constitute financial advice.

Strategy logic

Entry conditions

Enter long when the 9-period EMA crosses above the 21-period EMA on M5, during London session (07:00–12:00 server time), with spread below 15 points. Enter short on opposite EMA cross. ATR confirmation required: current ATR must be above 10-period ATR average.

Exit conditions

Take profit at 15 pips (150 points), stop loss at 10 pips (100 points). Trailing stop activates at 8 pips profit, trailing by 5 pips. End-of-session close at 12:00 server time regardless of position status.

MQL5 Expert Advisor Code

//+------------------------------------------------------------------+
//| EURUSD Scalping EA - London Session M5                          |
//| Pineify.app                                                      |
//+------------------------------------------------------------------+
#property copyright "Pineify.app"
#property version   "1.00"

input int    MagicNumber   = 567890;
input double Lots          = 0.01;
input int    EMA_Fast      = 9;
input int    EMA_Slow      = 21;
input int    MaxSpread     = 15;    // Max spread in points
input double TP_Points     = 150;
input double SL_Points     = 100;
input int    SessionStart  = 7;
input int    SessionEnd    = 12;

int emaFastH, emaSlowH, atrH;
double emaFast[], emaSlow[], atr[];

int OnInit() {
   emaFastH = iMA(_Symbol, PERIOD_M5, EMA_Fast, 0, MODE_EMA, PRICE_CLOSE);
   emaSlowH = iMA(_Symbol, PERIOD_M5, EMA_Slow, 0, MODE_EMA, PRICE_CLOSE);
   atrH     = iATR(_Symbol, PERIOD_M5, 14);
   ArraySetAsSeries(emaFast, true); ArraySetAsSeries(emaSlow, true);
   ArraySetAsSeries(atr, true);
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {
   IndicatorRelease(emaFastH); IndicatorRelease(emaSlowH); IndicatorRelease(atrH);
}

void OnTick() {
   MqlTick tick;
   if (!SymbolInfoTick(_Symbol, tick)) return;

   MqlDateTime dt; TimeToStruct(tick.time, dt);
   if (dt.hour < SessionStart || dt.hour >= SessionEnd) {
      CloseAllPositions(MagicNumber); return;
   }

   double spread = (tick.ask - tick.bid) / _Point;
   if (spread > MaxSpread) return;

   if (CopyBuffer(emaFastH, 0, 0, 3, emaFast) < 3) return;
   if (CopyBuffer(emaSlowH, 0, 0, 3, emaSlow) < 3) return;

   if (CountPositions(MagicNumber) > 0) return;

   bool bullCross = emaFast[2] < emaSlow[2] && emaFast[1] > emaSlow[1];
   bool bearCross = emaFast[2] > emaSlow[2] && emaFast[1] < emaSlow[1];

   if (bullCross) OpenTrade(ORDER_TYPE_BUY,  Lots, SL_Points * _Point, TP_Points * _Point, MagicNumber);
   if (bearCross) OpenTrade(ORDER_TYPE_SELL, Lots, SL_Points * _Point, TP_Points * _Point, MagicNumber);
}

void CloseAllPositions(int magic) {
   for (int i = PositionsTotal()-1; i >= 0; i--) {
      ulong t = PositionGetTicket(i);
      if (!PositionSelectByTicket(t)) continue;
      if ((int)PositionGetInteger(POSITION_MAGIC) != magic) continue;
      MqlTradeRequest req={}; MqlTradeResult res={};
      req.action=TRADE_ACTION_DEAL; req.position=t; req.symbol=_Symbol;
      req.volume=PositionGetDouble(POSITION_VOLUME);
      req.type=(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)?ORDER_TYPE_SELL:ORDER_TYPE_BUY;
      req.price=(req.type==ORDER_TYPE_SELL)?SymbolInfoDouble(_Symbol,SYMBOL_BID):SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      req.type_filling=ORDER_FILLING_IOC; OrderSend(req,res);
   }
}

int CountPositions(int magic) {
   int n=0;
   for (int i=PositionsTotal()-1;i>=0;i--) {
      ulong t=PositionGetTicket(i);
      if (PositionSelectByTicket(t)&&PositionGetString(POSITION_SYMBOL)==_Symbol&&
          (int)PositionGetInteger(POSITION_MAGIC)==magic) n++;
   } return n;
}

void OpenTrade(ENUM_ORDER_TYPE type,double lots,double sl,double tp,int magic) {
   MqlTradeRequest req={}; MqlTradeResult res={};
   req.action=TRADE_ACTION_DEAL; req.symbol=_Symbol; req.volume=lots; req.type=type;
   req.price=(type==ORDER_TYPE_BUY)?SymbolInfoDouble(_Symbol,SYMBOL_ASK):SymbolInfoDouble(_Symbol,SYMBOL_BID);
   req.sl=(type==ORDER_TYPE_BUY)?req.price-sl:req.price+sl;
   req.tp=(type==ORDER_TYPE_BUY)?req.price+tp:req.price-tp;
   req.magic=magic; req.comment="Pineify EURUSD"; req.type_filling=ORDER_FILLING_IOC;
   OrderSend(req,res);
}

Copy this code into MetaEditor, save it in the MQL5/Experts folder, and compile with F7.

Generate a custom EURUSD scalping EA

Describe the ATR inputs, alert rules, visual style, or confirmation filter you want. Pineify generates editable MQL5 source code that you can inspect and compile in MetaEditor.

Pine Script vs MQL5: Same Strategy, Different Platforms

AspectPine Script (TradingView)MQL5 (MetaTrader 5)
ExecutionSeries evaluated on chart updatesEvent handlers with explicit buffers
DeploymentRuns in TradingView with alertsRuns in the MT5 terminal or on a VPS
Broker accessVia TradingView broker integrationDirect broker connectivity
BacktestingStrategy Tester for strategy scriptsStrategy Tester for Expert Advisors
Code complexitySimpler, functional syntaxC++-like, more powerful

The calculation can be implemented on either platform, but the runtime model and buffer APIs differ. Read the Pine Script Supertrend reference before porting logic between them.

Frequently Asked Questions

Related MQL5 pages

Risk and testing note

Past performance is not indicative of future results. Backtest statistics are based on historical data and do not guarantee future profits. Trading involves significant risk of loss. This content is for educational purposes only and does not constitute financial advice.