# Dual Moving Average Crossover EA — Complete MQL4 Source Code
This EA uses a fast/slow MA crossover to generate buy and sell signals. It includes built-in lot sizing, stop loss, take profit, and a trailing stop option. Perfect for beginners learning MQL4 EA programming.
Full MQL4 Source Code
> ```mql4
> //+------------------------------------------------------------------+
> //| DualMA_Crossover_EA.mq4 |
> //| Copyright 2026, Self-Compiled |
> //| Free EA Download |
> //+------------------------------------------------------------------+
> #property copyright "Self-Compiled"
> #property link ""
> #property version "1.00"
> #property strict
>
> //--- Input Parameters
> input int FastMA_Period = 10; // Fast MA Period
> input int SlowMA_Period = 50; // Slow MA Period
> input int MA_Method = 0; // MA Method: 0=SMA, 1=EMA
> input double LotSize = 0.01; // Fixed Lot Size
> input int StopLoss = 50; // Stop Loss in pips
> input int TakeProfit = 100; // Take Profit in pips
> input bool UseTrailing = true; // Enable Trailing Stop
> input int TrailingStop = 30; // Trailing Stop in pips
> input int TrailingStep = 10; // Trailing Step in pips
> input int MagicNumber = 123456; // Magic Number
>
> //--- Global Variables
> double fastMA, slowMA;
>
> //+------------------------------------------------------------------+
> //| Expert initialization function |
> //+------------------------------------------------------------------+
> int OnInit()
> {
> Print("Dual MA Crossover EA Initialized. FastMA=", FastMA_Period, " SlowMA=", SlowMA_Period);
> return(INIT_SUCCEEDED);
> }
>
> //+------------------------------------------------------------------+
> //| Expert deinitialization function |
> //+------------------------------------------------------------------+
> void OnDeinit(const int reason)
> {
> Print("EA Removed. Reason: ", reason);
> }
>
> //+------------------------------------------------------------------+
> //| Expert tick function |
> //+------------------------------------------------------------------+
> void OnTick()
> {
> // Calculate Moving Averages
> fastMA = iMA(NULL, 0, FastMA_Period, 0, MA_Method, PRICE_CLOSE, 0);
> slowMA = iMA(NULL, 0, SlowMA_Period, 0, MA_Method, PRICE_CLOSE, 0);
>
> double fastMA_prev = iMA(NULL, 0, FastMA_Period, 0, MA_Method, PRICE_CLOSE, 1);
> double slowMA_prev = iMA(NULL, 0, SlowMA_Period, 0, MA_Method, PRICE_CLOSE, 1);
>
> //--- Buy Signal: Fast MA crosses above Slow MA
> if(fastMA_prev <= slowMA_prev && fastMA > slowMA)
> {
> if(CountOrders(OP_BUY) == 0)
> {
> OpenOrder(OP_BUY);
> }
> }
>
> //--- Sell Signal: Fast MA crosses below Slow MA
> if(fastMA_prev >= slowMA_prev && fastMA < slowMA)
> {
> if(CountOrders(OP_SELL) == 0)
> {
> OpenOrder(OP_SELL);
> }
> }
>
> //--- Trailing Stop
> if(UseTrailing)
> {
> TrailStop();
> }
> }
>
> //+------------------------------------------------------------------+
> //| Open Order Function |
> //+------------------------------------------------------------------+
> void OpenOrder(int orderType)
> {
> double price, sl, tp;
> color clr;
>
> if(orderType == OP_BUY)
> {
> price = Ask;
> sl = StopLoss > 0 ? Ask - StopLoss * Point * 10 : 0;
> tp = TakeProfit > 0 ? Ask + TakeProfit * Point * 10 : 0;
> clr = clrGreen;
> }
> else
> {
> price = Bid;
> sl = StopLoss > 0 ? Bid + StopLoss * Point * 10 : 0;
> tp = TakeProfit > 0 ? Bid - TakeProfit * Point * 10 : 0;
> clr = clrRed;
> }
>
> int ticket = OrderSend(Symbol(), orderType, LotSize, price, 3, sl, tp, "DualMA EA", MagicNumber, 0, clr);
>
> if(ticket < 0)
> Print("OrderSend failed. Error: ", GetLastError());
> else
> Print("Order opened: ", ticket);
> }
>
> //+------------------------------------------------------------------+
> //| Count Open Orders |
> //+------------------------------------------------------------------+
> int CountOrders(int type)
> {
> int count = 0;
> for(int i = 0; i < OrdersTotal(); i++)
> {
> if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == type)
> count++;
> }
> return count;
> }
>
> //+------------------------------------------------------------------+
> //| Trailing Stop Function |
> //+------------------------------------------------------------------+
> void TrailStop()
> {
> for(int i = 0; i < OrdersTotal(); i++)
> {
> if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
> {
> if(OrderType() == OP_BUY)
> {
> double newSL = Bid - TrailingStop * Point * 10;
> if(newSL > OrderStopLoss() + TrailingStep * Point * 10 && newSL < Bid)
> OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrGreen);
> }
> else if(OrderType() == OP_SELL)
> {
> double newSL = Ask + TrailingStop * Point * 10;
> if((newSL < OrderStopLoss() - TrailingStep * Point * 10 || OrderStopLoss() == 0) && newSL > Ask)
> OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrRed);
> }
> }
> }
> }
> //+------------------------------------------------------------------+
Parameter Explanation
Parameter Default Description
FastMA_Period 10 Fast moving average period
SlowMA_Period 50 Slow moving average period
MA_Method 0 0=SMA, 1=EMA
LotSize 0.01 Fixed lot size per trade
StopLoss 50 Stop loss in pips
TakeProfit 100 Take profit in pips
UseTrailing true Enable trailing stop
TrailingStop 30 Trailing distance in pips
MagicNumber 123456 Unique EA identifier
How to Compile & Use (MQL4 Tutorial)
Open MetaEditor in MT4 (press F4)
File → New → Expert Advisor → Paste the code above
Click Compile (F7) — check for zero errors
Drag the EA onto a chart, enable AutoTrading
Adjust parameters in the inputs tab
Reference
Self-Compiled MQL4 Source Code (2026)
MQL4 Official Documentation: https://www.mql5.com/en/docs
Want more advanced EAs with RSI divergence filters or multi-timeframe logic? Subscribe to our premium EA collection for battle-tested strategies with full support.