Home › Guides › Trading bots & code
MetaTrader Expert Advisor basics: what an EA really does
An Expert Advisor is a small program that runs inside MetaTrader and can place trades for you. The name sounds clever. Under the hood it's a set of rules — and you can read, test and switch off every one of them.
What an Expert Advisor is
MetaTrader is a trading platform many forex and CFD brokers give their clients. An Expert Advisor (EA) is a program attached to one of its charts.
Every time price moves, the EA runs its rules. Depending on how it's written, it can:
- Mark setups or send you a notification.
- Open trades with a stop and target.
- Move a stop, close a trade, or stop trading for the day.
"Expert" is just the name. An EA isn't an expert at anything. It's a rulebook that can click buttons.
MT4 vs MT5
There are two versions you'll hear about.
| MetaTrader 4 | MetaTrader 5 | |
|---|---|---|
| Language | MQL4 | MQL5 |
| File types | .mq4 (code), .ex4 (ready to run) | .mq5 (code), .ex5 (ready to run) |
| Markets | Mostly forex and CFDs | Forex, CFDs, and on some brokers stocks and futures |
| Strategy Tester | Single-thread, simpler | Multi-thread, more test modes |
| Code moves between them? | No — needs rewriting | No — needs rewriting |
Which one you use depends on your broker. New EAs are usually written for MT5. This page uses MT5 names; MT4 is similar.
The 3 functions every EA has
Inside, an EA is built around events — moments when MetaTrader calls your code:
- OnInit runs once when you attach the EA. It sets things up, such as creating indicators.
- OnTick runs every time a new price arrives. This is where the rules live.
- OnDeinit runs once when you remove the EA. It cleans up.
So an EA is really: "set up, then check the rules on every price, then tidy away." See MQL5 for beginners for a full example, line by line.
Where EAs live
- MetaEditor is the code editor that comes with MetaTrader. Open it from the toolbar or with F4 on Windows.
- EA files go in the MQL5/Experts folder. In MetaTrader, look for File, then Open Data Folder, to find it.
- After you add or compile an EA, it appears in the Navigator panel under Expert Advisors.
Compiling turns your .mq5 code into an .ex5 file MetaTrader can run. MetaEditor has a Compile button. Errors show at the bottom with line numbers.
How to switch an EA on
There are two switches, and both must be on for an EA to trade:
- The Algo Trading button on the main toolbar. This is the master switch for the whole platform. Off means no EA can trade.
- "Allow Algo Trading" in the EA's own settings, on its Common tab, when you drag it onto a chart.
When it's running, the EA's name shows in the chart's top-right corner with an icon that tells you if trading is allowed. Menu names change between versions — look for "Algo Trading" or "Auto Trading".
That master button is also your kill switch. Know where it is before you ever run an EA on real money.
Test it in the Strategy Tester first
MT5's Strategy Tester replays past prices and lets the EA trade them. Open it from the View menu (or Ctrl+R on Windows).
Pick:
- The EA and the symbol.
- The dates. Use a long stretch with different kinds of markets: trends, ranges, news spikes.
- The modelling mode. "Every tick based on real ticks" is the most realistic; "Open prices only" is fast but rough.
- Your deposit and leverage, set to what you'd really use.
Then read the report: the number of trades, the largest Drawdown (the deepest drop from a high point), the longest losing streak, and the trades themselves on the chart.
Two warnings for every test:
- Overfitting: tuning settings until the past looks perfect. The Strategy Tester has an optimizer that makes this very easy. Test the "best" settings on a later period you didn't tune on.
- Look-ahead and fill assumptions: a test that trades on prices it couldn't have known, or ignores Spread and Slippage, looks better than real life.
A backtest is hypothetical. It shows what the rules would have done, not what they'll do. See how to backtest a trading strategy.
Then run it on a demo account
Most MetaTrader brokers offer a demo account with fake money. Run the EA there for weeks before real money.
The demo shows what the tester can't: the platform closing, the internet dropping, a weekend gap. Many people run EAs on a VPS (a rented computer that stays on all day) for this reason. A VPS keeps the EA online; it doesn't make it any better.
See paper trading a bot.
The risk checks every EA needs
Before any EA touches real money — yours or one you bought — check that it has:
- A real stop on every trade, sent to the broker with the order. No stop means one bad move can take a large part of the account.
- Size from the stop: lot size worked out from the stop distance and a fixed risk, like 1%. Our lot size calculator shows the math.
- A daily loss limit that stops new trades for the day.
- Error handling: it checks whether each order actually went through, and logs the reason if not.
- A maximum lot size so a bug can't open a huge trade.
More: trading bot risk controls.
Be careful with EAs for sale. Many promise smooth results and hide how they lose — often by adding to losing trades or running with no stop. If you can't read the code or the rules, you can't know the risk. A seller's backtest is marketing, not proof.
Red flags in a bought EA
- "No stop loss needed" or very wide, hidden stops.
- Martingale — doubling the size after a loss. It looks smooth until one long losing streak.
- Grid or averaging down with no limit. See grid trading bots explained.
- Only a perfect backtest, no demo or live record you can check.
- Locked code (.ex5 only), so you can't see what it does.
Questions
What is an Expert Advisor in MetaTrader?
A program attached to a MetaTrader chart that runs a set of rules on every price update. It can alert you, open and close trades, or manage stops, depending on how it's written.
Are Expert Advisors safe?
An EA is only as safe as its rules. Check for a real stop on every trade, sizing from the stop, a daily loss limit and error handling — and test it in the Strategy Tester and on a demo account first.
Can I use an MT4 EA in MT5?
Not directly. MT4 uses MQL4 and MT5 uses MQL5. The ideas carry over, but the code has to be rewritten for MT5.
How do I turn on an Expert Advisor?
Turn on the Algo Trading button on the toolbar, then tick "Allow Algo Trading" in the EA's settings when you attach it to a chart. Both switches must be on.
Do I need to keep my computer on for an EA to run?
Yes. The EA only runs while MetaTrader is open and connected. Many people use a VPS so the platform stays online, but your stops should still sit at the broker in case anything disconnects.
Want the next step? We'll walk you through it.
Four free lessons by email, one every few days — each with a 10-minute exercise on a real chart.
Send Me the Free Lessons 4 short emails · No spam · Unsubscribe anytimeOr see the whole method, step by step:
See What’s Inside the Course Try everything free first. Decide when you're ready. · Open the free chart tool (works in the TradingView phone app)One chart idea a day, in plain English. Follow along: