Our partnerSkilling
  • Spreads from 0.1 pips
  • 1,000+ instruments
  • Free demo account

65% of retail investor accounts lose money when trading CFDs with this provider.

Open account
Module 3 · Build your first robot

3.3 The same robot in MQL4

Lesson 9 of 19Reading time: 2 min

MQL4 is older and in many ways simpler than MQL5. Here is the same robot for MetaTrader 4 – complete in one file.

MQL4
#property strict

input int    InpFastPeriod     = 10;
input int    InpSlowPeriod     = 30;
input double InpLots           = 0.10;
input double InpStopLossPips   = 30;
input double InpTakeProfitPips = 60;
input int    InpMagic          = 12345;

datetime lastBarTime = 0;
double   pip;

int OnInit()
{
   pip = (Digits == 3 || Digits == 5) ? 10 * Point : Point;
   return(INIT_SUCCEEDED);
}

int CountPositions()
{
   int count = 0;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
   {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == InpMagic && OrderType() <= OP_SELL) count++;
   }
   return(count);
}

void OnTick()
{
   if(Time[0] == lastBarTime) return;
   lastBarTime = Time[0];

   double fast1 = iMA(NULL, 0, InpFastPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
   double fast2 = iMA(NULL, 0, InpFastPeriod, 0, MODE_EMA, PRICE_CLOSE, 2);
   double slow1 = iMA(NULL, 0, InpSlowPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
   double slow2 = iMA(NULL, 0, InpSlowPeriod, 0, MODE_EMA, PRICE_CLOSE, 2);

   if(CountPositions() > 0) return;

   if(fast2 <= slow2 && fast1 > slow1)
   {
      double sl = NormalizeDouble(Ask - InpStopLossPips * pip, Digits);
      double tp = NormalizeDouble(Ask + InpTakeProfitPips * pip, Digits);
      if(OrderSend(Symbol(), OP_BUY, InpLots, Ask, 20, sl, tp, "MaCross", InpMagic, 0, clrGreen) < 0)
         Print("Buy failed: ", GetLastError());
   }
   else if(fast2 >= slow2 && fast1 < slow1)
   {
      double sl = NormalizeDouble(Bid + InpStopLossPips * pip, Digits);
      double tp = NormalizeDouble(Bid - InpTakeProfitPips * pip, Digits);
      if(OrderSend(Symbol(), OP_SELL, InpLots, Bid, 20, sl, tp, "MaCross", InpMagic, 0, clrRed) < 0)
         Print("Sell failed: ", GetLastError());
   }
}

The most important differences

MQL4MQL5
IndicatorsRead directly with one lineHandle in OnInit + CopyBuffer
PricesAsk, Bid, Close[1], Time[0]SymbolInfoDouble(...), iClose(...), iTime(...)
OrdersOrderSend(...)trade.Buy(...) / trade.Sell(...)
Open tradesOrdersTotal() + OrderSelectPositionsTotal() + PositionGetTicket
SlippageGiven in each order (here 20 points)Set once with SetDeviationInPoints

#property strict

This line makes MetaEditor check the code more strictly – much like MQL5. Always include it. It catches errors that could otherwise go unnoticed.

Where do I put the file?

In MetaTrader 4: File → Open Data Folder → MQL4 → Experts. Compile in MetaEditor (F7), refresh the Navigator, and drag the robot onto a chart. Remember the AutoTrading button and the Allow live trading tick box.

Try it yourself

Compare the MQL4 and MQL5 versions. Find three places where the code is different.

Key takeaways

  • MQL4 reads indicators and prices directly, and uses OrderSend for orders.
  • The logic is the same as in MQL5 – only the way it is written differs.
  • Always use #property strict in MQL4.
Practise what you have learned – without riskTry it on a free demo account with Skilling, with real prices and virtual money.
Open a free demo account

65% of retail investor accounts lose money when trading CFDs with this provider.