Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
Trading and crypto

Install and run an Expert Advisor in MetaTrader 4

Install and run an Expert Advisor in MT4: file types, the Experts folder, compiling, attaching it to a chart, AutoTrading, DLL and WebRequest rules, and logs.

CloudSat teamNovember 18, 20267 min read
Abstract illustration of a small glowing gear clicking into place on the corner of a dark chart, with thin lines running from it to a distant switch
On this pageThe files you might receive

An Expert Advisor (EA) is a program that runs inside MetaTrader 4, reads prices as they arrive and can open, modify and close orders on its own. Installing one takes a minute. Getting it to trade, and only when you mean it to, depends on three switches, a few settings and knowing where the terminal reports problems. This guide covers the whole path, from the file you receive to an EA you can stop at any moment.

The files you might receive

File What it is What to do with it
.ex4 A compiled EA, ready to run Copy it into MQL4\Experts
.mq4 Source code Copy it into MQL4\Experts, then compile it
.mqh Shared code included by the EA Copy it into MQL4\Include
.dll A Windows library the EA calls Copy it into MQL4\Libraries, and see the DLL section below
.set Saved input values Copy it into MQL4\Presets, then load it in the EA's settings

A compiled .ex4 file cannot be read, so you are trusting whoever built it. Source code can be read and compiled by you, which is one reason to prefer sellers who provide it.

Install it

  1. In MetaTrader 4, choose File > Open Data Folder. The terminal's data folder opens in Explorer.
  2. Copy the EA into MQL4\Experts, and any companion files into the folders listed above.
  3. Back in the terminal, right-click Expert Advisors in the Navigator (Ctrl+N) and choose Refresh, or restart the terminal.
  4. If you copied a .mq4 file, double-click it in the Navigator to open MetaEditor, then press Compile. The Errors tab at the bottom must report zero errors; warnings are worth reading but do not block it.

The EA now appears in the Navigator under Expert Advisors.

Attach it to a chart

An EA runs on one chart, and each chart holds at most one EA. Open a chart of the symbol and timeframe the EA expects (its documentation should say), then attach it in one of three ways: drag it from the Navigator onto the chart, double-click it, or right-click it and choose Attach to a chart.

A settings window opens with two tabs that matter:

  • Common: the position directions allowed (long, short or both), Allow live trading, which lets this EA send orders, and Allow DLL imports, which lets it call Windows libraries.
  • Inputs: the EA's own parameters. Load and Save read and write .set files, which is the reliable way to reuse exact settings on another chart or another computer.

Click OK. The EA's name and a status icon appear in the top-right corner of the chart. If the icon shows that live trading is disabled, check the switches below.

The three switches

An EA trades only when all three allow it:

Switch Where Scope
Allow Auto Trading Tools > Options > Expert Advisors Default for the whole terminal
AutoTrading button Toolbar The same terminal-wide setting, one click away
Allow live trading The EA's Common tab This EA on this chart

The AutoTrading button is your quickest stop control: one click and no EA in the terminal can send a new order. The same Options tab holds three protective settings worth enabling. They disable automated trading when you switch accounts, switch profiles, or change a chart's symbol or timeframe, which prevents an EA written for a demo account or one symbol from quietly acting on another.

DLLs and web requests

Two settings open an EA to the outside of the terminal, and both are off by default for good reason.

Allow DLL imports lets an EA call code in a Windows library. That code runs with your user's rights and can do anything a program can do, so MetaQuotes labels the option potentially dangerous. Enable it only for EAs whose source and library you trust, and only in that EA's Common tab, not globally.

Allow WebRequest for listed URL lets MQL4 programs reach web addresses you list, for example a news filter or a notification service. Add only the exact addresses the EA documents.

How an EA runs

An EA reacts to ticks: each new price quote for its chart's symbol runs its main function once. Two consequences surprise people:

  • No ticks, no activity. When the market is closed, over the weekend for forex, an EA does nothing, even if its rules mention a time.
  • It runs only while the terminal runs. Close MetaTrader, shut down the PC or let it sleep, and the EA stops. Orders already placed stay at the broker with their stop loss and take profit, but any logic the EA performs, a trailing stop included, waits until the terminal is back.

EAs usually mark their orders with a magic number, an identifier stored with each order, so that several EAs on one account manage only their own trades. If you run two copies of an EA on two charts, give each copy its own magic number unless its documentation says otherwise.

Here is the shape of a minimal EA that trades nothing but shows how the pieces fit, counting its own positions by magic number:

#property strict
input int MagicNumber = 20261118;

int OnInit()
{
   if(!IsTradeAllowed())
      Print("Trading not allowed: check AutoTrading and 'Allow live trading'");
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   int mine = 0;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
         && OrderMagicNumber() == MagicNumber
         && OrderSymbol() == Symbol())
         mine++;
   Comment("Positions held by this EA: ", mine);
}

void OnDeinit(const int reason)
{
   Comment("");
}

Where it reports problems

Open the Terminal window (Ctrl+T). The Experts tab shows what EAs print, including when each one loads and unloads. The Journal tab shows the terminal's side: connection, logins and every trade request with the server's answer. Daily log files for EAs are kept in MQL4\Logs.

When an order fails, MQL4 reports an error code. The common ones:

Code Meaning Usual cause
4109 Trade is not allowed Allow live trading off, or AutoTrading off
130 Invalid stops Stop loss or take profit too close to the price for this symbol
134 Not enough money Free margin too low for the volume
136 / 138 Off quotes / requote Price moved before the order was accepted
146 Trade context busy Another program was sending an order at the same moment

Stop it

To stop an EA from trading but keep it loaded, click AutoTrading off. To remove it, right-click the chart and choose Expert Advisors > Remove, or close the chart. Neither action closes open positions; they stay at the broker until you or another program closes them.

Keep it running

Since an EA lives inside the terminal, an EA meant to trade around the clock needs a terminal that stays on. The MetaTrader 4 one-click app on CloudSat sets up the terminal on a Windows server that keeps running after you close Remote Desktop.

FAQ

Why does my EA not trade?

Check in this order: AutoTrading on, Allow live trading ticked, the Experts tab for messages, the Journal for rejected requests, and whether the market is open. Many EAs also wait for conditions that may not occur for hours.

Can I run two EAs on one chart?

No. Open a second chart of the same symbol and attach the other EA there, with its own magic number.

Do EAs keep working when I close Remote Desktop?

Yes, if you disconnect. Closing the Remote Desktop window leaves your session and the terminal running. Signing out closes the terminal and stops every EA.

Should I test an EA before running it on a live account?

Yes. Run it in the Strategy Tester to see how it behaves on history, then on a demo account to see how it behaves with live prices, and read its logs before trusting it with real money.

Trading involves risk of loss. Nothing in this article is financial advice.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.