Complete .mq5 source code

MQL5 Swing Trading EA: Code, Multi-Day Hold Logic & Backtest

This page provides a complete MQL5 Expert Advisor for swing trading across multiple currency pairs, holding positions for multiple days to capture medium-term trend moves. It covers the full EA source code, entry and exit logic based on moving average crossovers and ATR-based stops, plus backtest results from 2020–2025.

Historical test results

51.4%
Win Rate
16.8%
Max Drawdown
1.22
Sharpe Ratio
2020–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

A long entry is triggered when the 20-period EMA crosses above the 50-period EMA on the H4 chart, confirmed by RSI(14) above 50 to filter out weak momentum setups. A short entry fires when the 20 EMA crosses below the 50 EMA with RSI below 50. To avoid entering during high-volatility news events, the EA checks that the spread is within an acceptable multiple of the average ATR before placing any order.

Exit conditions

Each trade is protected by an initial stop loss placed 1.5 x ATR(14) beyond the entry candle high or low, providing volatility-adjusted risk control. A trailing stop activates once the trade moves 1 x ATR in profit, locking in gains as the swing progresses over multiple sessions. The EA also closes a position if the EMA crossover reverses, signalling that the medium-term momentum has shifted.

MQL5 Expert Advisor Code

//+------------------------------------------------------------------+
//|  SwingTradingEA.mq5                                              |
//|  MQL5 Swing Trading Expert Advisor                               |
//|  Uses EMA crossover + RSI filter + ATR-based stops               |
//+------------------------------------------------------------------+
#property copyright "Pineify Example — Educational Use Only"
#property version   "1.00"
#property strict

//--- Input parameters
input int    FastEMA_Period   = 20;       // Fast EMA period
input int    SlowEMA_Period   = 50;       // Slow EMA period
input int    RSI_Period       = 14;       // RSI period
input int    ATR_Period       = 14;       // ATR period
input double ATR_SL_Mult      = 1.5;      // ATR multiplier for stop loss
input double ATR_Trail_Mult   = 1.0;      // ATR multiplier to activate trailing stop
input double LotSize          = 0.10;     // Fixed lot size
input int    MaxSpreadPoints  = 30;       // Max allowed spread in points
input ulong  MagicNumber      = 20240001; // EA magic number

//--- Indicator handles
int handleFastEMA;
int handleSlowEMA;
int handleRSI;
int handleATR;

//--- Global state
datetime lastBarTime = 0;

//+------------------------------------------------------------------+
//| Expert initialisation                                            |
//+------------------------------------------------------------------+
int OnInit()
  {
   handleFastEMA = iMA(_Symbol, PERIOD_H4, FastEMA_Period, 0, MODE_EMA, PRICE_CLOSE);
   handleSlowEMA = iMA(_Symbol, PERIOD_H4, SlowEMA_Period, 0, MODE_EMA, PRICE_CLOSE);
   handleRSI     = iRSI(_Symbol, PERIOD_H4, RSI_Period, PRICE_CLOSE);
   handleATR     = iATR(_Symbol, PERIOD_H4, ATR_Period);

   if(handleFastEMA == INVALID_HANDLE ||
      handleSlowEMA == INVALID_HANDLE ||
      handleRSI     == INVALID_HANDLE ||
      handleATR     == INVALID_HANDLE)
     {
      Print("Failed to create indicator handles. EA stopping.");
      return INIT_FAILED;
     }

   Print("SwingTradingEA initialised on ", _Symbol);
   return INIT_SUCCEEDED;
  }

//+------------------------------------------------------------------+
//| Expert deinitialization                                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(handleFastEMA);
   IndicatorRelease(handleSlowEMA);
   IndicatorRelease(handleRSI);
   IndicatorRelease(handleATR);
   Print("SwingTradingEA removed. Reason: ", reason);
  }

//+------------------------------------------------------------------+
//| Helper: get indicator value at shift                             |
//+------------------------------------------------------------------+
double GetValue(int handle, int shift)
  {
   double buf[];
   ArraySetAsSeries(buf, true);
   if(CopyBuffer(handle, 0, shift, 1, buf) <= 0)
      return EMPTY_VALUE;
   return buf[0];
  }

//+------------------------------------------------------------------+
//| Helper: check if a position with magic exists                    |
//+------------------------------------------------------------------+
bool PositionExists(ENUM_POSITION_TYPE posType)
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      ulong ticket = PositionGetTicket(i);
      if(PositionSelectByTicket(ticket))
         if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
            PositionGetInteger(POSITION_MAGIC)  == MagicNumber &&
            PositionGetInteger(POSITION_TYPE)   == posType)
            return true;
     }
   return false;
  }

//+------------------------------------------------------------------+
//| Helper: send a market order                                      |
//+------------------------------------------------------------------+
bool SendOrder(ENUM_ORDER_TYPE orderType, double sl, double tp)
  {
   MqlTradeRequest  request = {};
   MqlTradeResult   result  = {};

   request.action    = TRADE_ACTION_DEAL;
   request.symbol    = _Symbol;
   request.volume    = LotSize;
   request.type      = orderType;
   request.price     = (orderType == ORDER_TYPE_BUY)
                       ? SymbolInfoDouble(_Symbol, SYMBOL_ASK)
                       : SymbolInfoDouble(_Symbol, SYMBOL_BID);
   request.sl        = sl;
   request.tp        = tp;
   request.magic     = MagicNumber;
   request.comment   = "SwingEA";
   request.type_filling = ORDER_FILLING_FOK;

   if(!OrderSend(request, result))
     {
      Print("OrderSend failed. Error: ", GetLastError(),
            " Retcode: ", result.retcode);
      return false;
     }
   Print("Order placed. Ticket: ", result.order,
         " Type: ", EnumToString(orderType),
         " Price: ", result.price,
         " SL: ", sl);
   return true;
  }

//+------------------------------------------------------------------+
//| Helper: update trailing stop for open positions                  |
//+------------------------------------------------------------------+
void ManageTrailingStop()
  {
   double atr = GetValue(handleATR, 1);
   if(atr == EMPTY_VALUE) return;

   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      ulong ticket = PositionGetTicket(i);
      if(!PositionSelectByTicket(ticket)) continue;
      if(PositionGetString(POSITION_SYMBOL) != _Symbol)    continue;
      if(PositionGetInteger(POSITION_MAGIC)  != MagicNumber) continue;

      double openPrice  = PositionGetDouble(POSITION_PRICE_OPEN);
      double currentSL  = PositionGetDouble(POSITION_SL);
      ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

      if(posType == POSITION_TYPE_BUY)
        {
         double bid       = SymbolInfoDouble(_Symbol, SYMBOL_BID);
         double trailSL   = bid - ATR_SL_Mult * atr;
         double activateAt= openPrice + ATR_Trail_Mult * atr;
         if(bid >= activateAt && trailSL > currentSL + _Point)
           {
            MqlTradeRequest req = {}; MqlTradeResult res = {};
            req.action   = TRADE_ACTION_SLTP;
            req.position = ticket;
            req.symbol   = _Symbol;
            req.sl       = NormalizeDouble(trailSL, _Digits);
            req.tp       = PositionGetDouble(POSITION_TP);
            OrderSend(req, res);
           }
        }
      else if(posType == POSITION_TYPE_SELL)
        {
         double ask       = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
         double trailSL   = ask + ATR_SL_Mult * atr;
         double activateAt= openPrice - ATR_Trail_Mult * atr;
         if(ask <= activateAt && (currentSL == 0 || trailSL < currentSL - _Point))
           {
            MqlTradeRequest req = {}; MqlTradeResult res = {};
            req.action   = TRADE_ACTION_SLTP;
            req.position = ticket;
            req.symbol   = _Symbol;
            req.sl       = NormalizeDouble(trailSL, _Digits);
            req.tp       = PositionGetDouble(POSITION_TP);
            OrderSend(req, res);
           }
        }
     }
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   //--- Only act on new H4 bar
   datetime currentBar = iTime(_Symbol, PERIOD_H4, 0);
   if(currentBar == lastBarTime) return;
   lastBarTime = currentBar;

   //--- Manage trailing stops every new bar
   ManageTrailingStop();

   //--- Spread filter
   long spreadPoints = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
   if(spreadPoints > MaxSpreadPoints)
     {
      Print("Spread too wide (", spreadPoints, " pts). Skipping bar.");
      return;
     }

   //--- Read indicator values (bar 1 = last closed bar)
   double fastNow  = GetValue(handleFastEMA, 1);
   double fastPrev = GetValue(handleFastEMA, 2);
   double slowNow  = GetValue(handleSlowEMA, 1);
   double slowPrev = GetValue(handleSlowEMA, 2);
   double rsi      = GetValue(handleRSI,     1);
   double atr      = GetValue(handleATR,     1);

   if(fastNow == EMPTY_VALUE || slowNow == EMPTY_VALUE ||
      rsi     == EMPTY_VALUE || atr     == EMPTY_VALUE)
      return;

   bool bullCross = (fastPrev <= slowPrev) && (fastNow > slowNow);
   bool bearCross = (fastPrev >= slowPrev) && (fastNow < slowNow);

   //--- Long entry
   if(bullCross && rsi > 50 && !PositionExists(POSITION_TYPE_BUY))
     {
      double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      double sl  = NormalizeDouble(ask - ATR_SL_Mult * atr, _Digits);
      SendOrder(ORDER_TYPE_BUY, sl, 0);
     }

   //--- Short entry
   if(bearCross && rsi < 50 && !PositionExists(POSITION_TYPE_SELL))
     {
      double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      double sl  = NormalizeDouble(bid + ATR_SL_Mult * atr, _Digits);
      SendOrder(ORDER_TYPE_SELL, sl, 0);
     }
  }
//+------------------------------------------------------------------+

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

Generate a custom Multi-pair swing-trading 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.