3.3 The same robot in MQL4
MQL4 is older and in many ways simpler than MQL5. Here is the same robot for MetaTrader 4 – complete in one file.
#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
| MQL4 | MQL5 | |
|---|---|---|
| Indicators | Read directly with one line | Handle in OnInit + CopyBuffer |
| Prices | Ask, Bid, Close[1], Time[0] | SymbolInfoDouble(...), iClose(...), iTime(...) |
| Orders | OrderSend(...) | trade.Buy(...) / trade.Sell(...) |
| Open trades | OrdersTotal() + OrderSelect | PositionsTotal() + PositionGetTicket |
| Slippage | Given 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.
65% of retail investor accounts lose money when trading CFDs with this provider.