How to build an Expert Advisor.

A practical, step-by-step guide to turning a trading strategy into an automated Expert Advisor for MetaTrader 4 and 5, with an MQL5 example you can read.

Sculptural head crowned by a glowing slab of market-green light

To build an Expert Advisor, you turn a trading strategy into precise rules, code those rules in MQL using MetaTrader's MetaEditor, then backtest, optimize and forward-test the result before running it on a live account. The steps below walk through the whole path, from a strategy on paper to an automated EA on your chart.

What is an Expert Advisor?

An Expert Advisor (EA) is an automated program that trades a strategy for you on MetaTrader 4 or 5. It reads the market on every price tick and executes your entries, exits, risk and position sizing exactly to the rules you define, around the clock, without hesitation or emotion. EAs are written in MQL, the native language of MetaTrader: MQL4 for MT4 and MQL5 for MT5.

Before you start

Building an EA is mostly a translation job: from a strategy you understand into rules a computer can follow. Before writing any code, make sure you have:

How to build an Expert Advisor, step by step

  1. Define your strategy as rules

    Write down the exact conditions for an entry, an exit, a stop-loss and a take-profit, plus how much to risk per trade. If you cannot express it as unambiguous if-then rules, an EA cannot trade it. This step decides everything that follows.

  2. Set up MetaEditor

    Open MetaEditor from your MetaTrader terminal and create a new Expert Advisor with the MQL Wizard. It generates a skeleton with the event handlers every EA uses: OnInit (runs once at start), OnTick (runs on every price update) and OnDeinit (runs at shutdown).

  3. Structure the EA

    Declare your inputs (lot size, indicator periods, risk settings) so they can be changed without touching the code. Create indicator handles in OnInit, and put your decision logic in OnTick. Keep the trading logic separate from the order-sending code so it stays readable and testable.

  4. Code the entries and exits

    Translate your rules into conditions on price and indicators, then place and close orders when they trigger. The MQL5 skeleton below shows a simple moving-average crossover: buy when the fast EMA crosses above the slow EMA, close when it crosses back below.

  5. Add risk and money management

    This is where most EAs live or die. Add a stop-loss and take-profit, size positions from a fixed risk percentage rather than a fixed lot, and cap exposure. A profitable signal with poor risk control still blows up an account.

  6. Backtest in the Strategy Tester

    Run the EA in MetaTrader's Strategy Tester over your historical data. Look past the equity curve at drawdown, number of trades, and behaviour across different market conditions. Be honest about spread, slippage and commission, or the results will flatter you. See how to backtest a trading strategy for the full method.

  7. Optimize, forward-test, then go live

    Tune parameters with the optimizer, but resist curve-fitting: a setting that only works on one slice of history will not survive live markets. Validate with walk-forward testing, then forward-test on a demo account before committing real capital.

An MQL5 Expert Advisor skeleton

Here is a minimal, readable MT5 Expert Advisor: an EMA crossover that opens one position and closes it on the opposite cross. It is a starting point to learn the structure, not a strategy to trade as-is.

EMA crossover EA · MQL5
#include <Trade/Trade.mqh>
CTrade trade;

input double LotSize = 0.10;   // fixed lot size
input int    FastMA  = 12;     // fast EMA period
input int    SlowMA  = 26;     // slow EMA period

int fastHandle, slowHandle;

int OnInit()
{
   fastHandle = iMA(_Symbol, _Period, FastMA, 0, MODE_EMA, PRICE_CLOSE);
   slowHandle = iMA(_Symbol, _Period, SlowMA, 0, MODE_EMA, PRICE_CLOSE);
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   double fast[], slow[];
   if(CopyBuffer(fastHandle, 0, 0, 2, fast) < 2) return;
   if(CopyBuffer(slowHandle, 0, 0, 2, slow) < 2) return;

   bool crossUp   = fast[1] <= slow[1] && fast[0] > slow[0];
   bool crossDown = fast[1] >= slow[1] && fast[0] < slow[0];

   if(crossUp && PositionsTotal() == 0)
      trade.Buy(LotSize);

   if(crossDown && PositionsTotal() > 0)
      trade.PositionClose(_Symbol);
}

A production EA adds a stop-loss and take-profit, risk-based position sizing, filters to avoid trading in bad conditions, and careful handling of order errors and partial fills. That gap between a skeleton and a robust system is exactly where most of the real work sits.

Common mistakes to avoid

Prefer to have it built?

Building a robust EA by hand takes solid MQL skills and a lot of testing. If you would rather hand over a strategy and get back a tested, documented robot with source code included, that is exactly our Expert Advisor development service. You can also see our MQL5 programming work or explore the full trading systems hub.

Frequently asked

What is an Expert Advisor?

An Expert Advisor (EA) is an automated program that trades a strategy for you on MetaTrader 4 or 5, executing entries, exits, risk and position sizing exactly to your rules, around the clock.

Which language are Expert Advisors written in?

Expert Advisors are written in MQL, the native language of MetaTrader: MQL4 for MetaTrader 4 and MQL5 for MetaTrader 5. Both are coded in the MetaEditor IDE that ships with the platform.

Do I need to know how to code to build an Expert Advisor?

To build a robust EA by hand you need working MQL knowledge. Visual and no-code tools exist for simple strategies, but anything with real risk management, multiple conditions or several instruments is far easier and safer to code, or to have a developer build.

How long does it take to build an Expert Advisor?

A single-signal EA can be a few days of work; a multi-instrument system with advanced risk management and thorough backtesting takes longer. Most of the time goes into testing and validation, not the first version of the code.

Can I build an Expert Advisor without programming?

Yes, for simple rules, using visual strategy builders that generate MQL. They hit limits quickly on complex logic and custom risk rules, so many traders start there and then move to custom-coded EAs as their strategy grows.

Skip the learning curve. Ship the robot.

Bring your strategy. We turn it into a tested Expert Advisor, source code included.

EA development Book a call