Expert advisors (EAs): how automated trading on MetaTrader works, from the file to the fill

An expert advisor is a program that trades a MetaTrader account by itself: it reads the chart, decides, and sends the orders, around the clock, without being asked twice. The technology is mature and cheap. What is hard is knowing whether the thing you are about to run on real money works, and this guide is mostly about that.

What an EA is and where it runs

MetaTrader 4 and 5 each ship with a programming language, MQL4 and MQL5, and a compiler. An expert advisor is a compiled program (an .ex4 or .ex5 file) attached to a chart. Every time a new tick arrives the platform calls the EA's main function, the EA looks at whatever it has been written to look at, and it may place, modify or close orders through the platform's trade functions. It runs inside the terminal on your computer or server, not on the broker's side; the broker sees only the orders.

That has three consequences. The EA stops when the terminal stops, so a laptop that sleeps overnight is not a trading computer. The EA is only as fast as the connection between the terminal and the broker's server. And nothing the EA does is visible to the broker as automation; to the broker an EA's market order looks like any other market order, which is why brokers that "allow EAs" are simply brokers that do not forbid them in their terms.

MQL4 and MQL5 are different languages with different order models. An EA written for MT4 will not run on MT5, and the conversion is a rewrite, not a recompile. A broker that offers only MT5 is no use to someone with a library of MT4 EAs, and the reverse is also true. The MT4 and MT5 pages list which brokers offer which. cTrader has its own equivalent, cBots, written in C#; TradingView's Pine Script can signal but cannot, on its own, place trades at most brokers.

Where EAs come from

  • Written by you. MQL is a C-like language with good documentation and a large library of examples. A simple moving-average crossover EA is a few dozen lines. Anything that handles partial fills, requotes, weekend gaps and broker-specific symbol names properly is a few hundred.
  • The MQL5 Market and CodeBase. MetaQuotes runs a store inside the terminal. Free code in the CodeBase is usually unsupported and of varied quality; paid products in the Market come with a demo version that runs only in the strategy tester, which is exactly where you should start.
  • Commercial vendors. Standalone sellers of EAs, usually with a website, a Myfxbook account and a licence key. This is where most of the money and most of the problems are, and the forex robots guide is about evaluating them.
  • Commissioned. Freelance MQL developers will turn a written set of rules into an EA for a few hundred dollars. Worth it only if you have rules worth coding and the discipline to test what comes back.

Backtesting without fooling yourself

The Strategy Tester in both terminals replays historical prices through an EA and reports the trades it would have made. It is indispensable and easy to misuse. Four things decide whether a backtest means anything.

Tick data quality

MT4's default history is one-minute bars with the ticks inside them invented by interpolation, which produces fills that could never have happened. Any EA that trades inside the bar, which is most of them, needs real tick data: MT5 can download it from the broker, and third-party tools import it into MT4. A backtest at "99% modelling quality" with real ticks is the minimum. One at 90% is a story.

Spread, commission and swap

A backtest run at a fixed one-pip spread with no commission and no overnight charges will flatter any strategy, and flatter a scalping strategy to the point of fiction. Use the broker's actual spread, variable if the data supports it, add the commission, and let the tester apply swaps. A strategy that is profitable at 0.2 pips and losing at 0.9 is not a strategy; it is a bet on the spread.

Out-of-sample testing

If the EA's parameters were tuned on 2019 to 2024 data, a profitable backtest on 2019 to 2024 proves only that the tuning worked. Hold back a period the optimiser never saw, 2025 to date for instance, and run the tuned EA on that. If the result on the unseen period looks like the result on the tuned period, the strategy may have found something. If it collapses, the optimiser found noise. This is called walk-forward or out-of-sample testing and it is the single most useful thing a backtest can do.

Optimisation

The tester will search thousands of parameter combinations and hand back the best. With enough parameters, something will always look wonderful on the past. The warning signs are a strategy whose profit depends on one parameter being exactly 37 rather than 35 or 39, an equity curve that is smooth on the tuned data and jagged on the rest, and a trade count small enough that two or three lucky trades make the whole result. Prefer a parameter set that is merely good across a wide range of neighbours to one that is spectacular at a single point.

Worked example: what curve-fitting looks like

An EA has six parameters. Optimised on EUR/USD H1 from 2020 to 2024 it returns 180% with a maximum drawdown of 12%. Impressive.

Run on 2025, unseen: −9%. Run on GBP/USD 2020 to 2024, same parameters: −22%. Run on EUR/USD 2020 to 2024 with the stop parameter changed from 42 to 48 pips: +11%.

None of this means the EA is fraudulent. It means the optimiser found the six numbers that best described five years of one chart, and those numbers describe nothing else. The honest result from this test is the −9%.

1001502002503002020202120222023202420252026tuned on this data: +180%unseen: −9%
Illustration of the worked example: the optimiser found parameters that fit 2020 to 2024 almost perfectly, and the same parameters lost money on the year it never saw. The out-of-sample result is the honest one.

Forward testing and going live

A backtest that survives the above earns a demo account, not a live one. Run the EA on a demo for at least a month, ideally through a central bank meeting and a non-farm payrolls release, and compare the trades it takes to the ones the backtest said it would take over the same period. Differences come from execution, which a backtest cannot model: slippage, requotes, the broker's minimum stop level, the symbol being named EURUSD.m rather than EURUSD. Then go live with the smallest lot size the account allows and scale only when the live results match the demo.

Demo accounts fill at the broker's quoted price with no slippage and sometimes no spread widening, so a scalping EA that is profitable on demo can still lose live. Treat the demo as a test of the plumbing, and the first small live period as the test of the strategy.

Hosting: the VPS question

A virtual private server is a rented computer in a data centre that runs the terminal continuously. For an EA that trades on a daily chart and places a few orders a week, a home computer that stays on is fine. For anything that trades intraday, a VPS removes the three things that kill EAs: the computer sleeping, the home connection dropping, and the latency between your house and the broker's server, which for a London broker and a Sydney trader is a quarter of a second each way.

Many brokers offer a free or discounted VPS, usually conditional on a minimum deposit or monthly volume, and MetaQuotes sells one from inside the terminal. Third-party providers locate servers in the same data centres as the major brokers (Equinix LD4 in London, NY4 in New Jersey), which brings latency under a few milliseconds. For a scalping EA that matters; for everything else, any stable server will do.

Broker settings that break EAs

SettingWhy it mattersWhat to check
Execution modelMarket execution with slippage in both directions suits EAs; instant execution with requotes makes an EA retry or miss fills.Review page: execution model. Prefer ECN or STP for anything intraday.
Minimum stop levelAn EA that places stops 5 pips away fails on a broker whose minimum is 8.Instrument specification on the platform ("Stops level").
Hedging and FIFOUS brokers under NFA rules forbid hedging and require first-in-first-out closing. EAs that scale in and out break.Any US entity; some brokers also disable hedging on certain accounts.
Symbol names and digitsEURUSD vs EURUSD.m vs EURUSD.ecn; 4-digit vs 5-digit quotes. An EA hard-coded for one fails silently on the other.Market Watch on the platform; test on demo before live.
Swap and triple swapA strategy holding over Wednesday rollover pays three days of financing; an EA tested without swaps will overstate returns.Instrument specification; review page where recorded.
Spread at the hours the EA tradesAsian-session spreads are several times wider than London's; a London-tested EA run at 2am trades a different market.Watch the spread at those hours on demo; some brokers publish historical spreads.
EA restrictions in the termsA few brokers forbid "abusive" strategies, latency arbitrage, or trading within seconds of news. Vaguely worded clauses have been used to void profits.The client agreement; the review notes known restrictions.

What an EA cannot do

It cannot know that the market has changed. An EA built on five years of ranging EUR/USD will keep selling tops in a trend until the account is gone, because nothing in its code knows what a trend is unless the author taught it. It cannot handle what it was not written to handle: a broker outage, a symbol being suspended, a margin requirement doubling before the weekend. And it cannot tell you when to switch it off, which is the decision that separates people who run EAs profitably from people who used to.

The useful mental model is that an EA executes a rule with perfect discipline and no judgement. If the rule is good, that is the best trader you will ever hire. If the rule is bad, it will lose with the same perfect discipline, faster than you would have.

Questions people ask

Are expert advisors legal?

Yes. Automated trading on a retail account is permitted everywhere forex trading is permitted, and every major broker allows EAs. A handful restrict specific strategies in their terms; the review for each broker notes any we have recorded.

Do EAs make money?

Some do, for a while, in the conditions they were built for. The honest statement is that most commercial EAs do not survive a year of live trading, that a well-tested EA on a sound rule can, and that nobody can tell which from a backtest alone. Out-of-sample and forward testing are the only evidence worth having.

Can I run an EA on my phone?

No. The mobile terminals do not run EAs. An EA runs on a Windows computer or server running the desktop terminal, and the phone app can watch the account it trades.

Does the broker know I am using an EA?

Not directly. Orders from an EA look like any other orders. A broker can infer automation from patterns, and a few terms of service ask you to declare it, but there is no flag on the order.

MT4 or MT5 for EAs?

MT4 has the larger library of existing EAs and more brokers. MT5 has a faster, multi-threaded tester with real tick data built in, more order types and better charting. For writing new strategies MT5 is the better tool; for running an existing MT4 EA you need MT4.