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.
Backtest Performance
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 (F4 in MT5), save in the Experts folder, and compile with F7.
Generate a Custom EURUSD Scalping EA →
Pineify AI generates syntactically validated MQL5 Expert Advisors from plain English descriptions. Customize entry logic, risk management, and trading sessions — no coding required.
Pine Script vs MQL5: Same Strategy, Different Platforms
| Aspect | Pine Script (TradingView) | MQL5 (MetaTrader 5) |
|---|---|---|
| Execution | Bar-based, backtesting only | Tick-based, live trading |
| Deployment | TradingView alerts | Runs 24/5 on VPS/MT5 |
| Broker access | Via TradingView broker integration | Direct broker connectivity |
| Backtesting | Built-in, no data download needed | Strategy Tester, tick data required |
| Code complexity | Simpler, functional syntax | C++-like, more powerful |
Pineify supports both platforms. Prototype your strategy visually in TradingView Pine Script, then generate the equivalent MQL5 EA for live MT5 trading.