MetaTrader trading bot: MT4/MT5 Expert Advisors and code

MetaTrader 4 and 5 are the dominant retail forex and CFD platforms, and their automation layer — the Expert Advisor, or EA — is how most retail forex bots are built. An EA is a compiled MQL program that runs inside the terminal, reacting to every price tick with your trading logic. Unlike a Python ccxt bot that talks to a crypto exchange, an MT bot lives inside the broker’s own platform, which changes how you host it, test it and reason about spreads. This guide covers the EA model, the MQL5 OnTick loop, the built-in Strategy Tester, and the forex realities that decide whether an EA survives.

On this page
  1. What an EA is
  2. The OnTick loop
  3. The Strategy Tester
  4. VPS hosting
  5. Forex-specific risks
  6. Getting started
  7. FAQ

What a MetaTrader Expert Advisor is

An Expert Advisor is a program written in MQL4 (MT4) or MQL5 (MT5) that runs inside the MetaTrader terminal and can open, modify and close orders automatically. It is the MT equivalent of a ccxt crypto bot, but instead of calling an exchange REST API it uses the terminal’s built-in order functions, and it is tied to your forex broker’s feed and execution.

The MQL5 OnTick loop

An EA’s core is the OnTick() function, called on every incoming price tick. You compute your signal there and act on a change of state — exactly the same discipline as a crypto bot’s candle loop, just tick-driven.

mql5 · SmaCross.mq5input int FastP = 20, SlowP = 50;

void OnTick()
{
   double fast = iMA(_Symbol, _Period, FastP, 0, MODE_SMA, PRICE_CLOSE);
   double slow = iMA(_Symbol, _Period, SlowP, 0, MODE_SMA, PRICE_CLOSE);
   if(fast > slow && PositionsTotal() == 0)
   {
      MqlTradeRequest  req;  MqlTradeResult res;
      req.action = TRADE_ACTION_DEAL;  req.symbol = _Symbol;
      req.volume = 0.01;  req.type = ORDER_TYPE_BUY;        // micro lot
      req.price  = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      OrderSend(req, res);
   }
}

The Strategy Tester

MetaTrader ships a built-in Strategy Tester that backtests an EA on historical tick or bar data, with a visual mode and an optimiser. It is convenient, but the same backtesting rules apply: use quality tick data, account for spread, and validate out-of-sample — the optimiser will happily over-fit an EA to history.

price tick OnTick() logic OrderSend → broker
Every tick calls OnTick(); a state change triggers OrderSend to the broker — the EA lives inside the terminal.

VPS hosting

An EA only trades while the terminal is running, so you host MetaTrader on a Windows VPS near the broker’s server for 24/5 uptime and low latency — the forex parallel to the bot hosting guide. Many brokers offer a free VPS above a balance threshold.

Forex-specific risks

Spread, slippage and the dealing desk

An EA backtested on the bid line ignores the spread you actually pay, and on news the spread widens and slippage spikes. Some brokers also requote or run a dealing desk that fills you worse than the tester assumes. Always test with realistic spread and slippage, and prefer a true ECN broker for EA trading.

Getting started

Write the EA, backtest it in the Strategy Tester with quality data and realistic spread, then run it on a demo account for weeks before going live on micro lots. Size every trade from a stop with the position calculator.

Not financial advice. This content is educational. Automated and algorithmic trading carries a real risk of financial loss. Never trade money you cannot afford to lose. Review the SEC investor.gov and CFTC resources before trading.

Frequently asked questions

What is a MetaTrader Expert Advisor?

An Expert Advisor (EA) is a program written in MQL4 or MQL5 that runs inside the MetaTrader terminal and opens, modifies and closes orders automatically. It is the forex equivalent of a crypto ccxt bot, but instead of calling an exchange API it uses the terminal’s built-in order functions tied to your broker’s feed.

How do I backtest a MetaTrader bot?

MetaTrader includes a built-in Strategy Tester that runs an EA on historical tick or bar data with a visual mode and an optimiser. Use quality tick data, account for the spread you actually pay, and validate out-of-sample, because the optimiser will happily over-fit an EA to past data.

Do I need a VPS for a MetaTrader bot?

Effectively yes, because an EA only trades while the terminal is running. Most traders host MetaTrader on a Windows VPS near the broker’s server for 24/5 uptime and low latency; many brokers offer a free VPS above a minimum balance.

Why do MetaTrader EAs fail in live trading?

The most common reason is ignoring real trading costs — an EA backtested on the bid line skips the spread, and during news the spread widens and slippage spikes. Some brokers also requote or fill worse than the tester assumes, so testing with realistic spread and using a true ECN broker matters.

MB

Mustafa Bilgic

Algorithmic trading practitioner · Founder, AITradingBot.us

Mustafa builds and backtests automated trading systems and writes about them without the hype. Every tool on this site is free and runs entirely in your browser.