← All Insights
Algo & EA

Building your first EA: structure before strategy

Before a single entry rule, an expert advisor needs an order manager, a risk module and a kill switch. Skip them and the strategy never gets a fair test.

16 Feb 2026 · 11 min read · PG Mama

Most first expert advisors are written in the wrong order. The author starts with the idea — a crossover, a breakout, a pattern — codes it in an afternoon, attaches it to a chart, and watches it do something unexpected within a week. The strategy usually is not the problem. The scaffolding around it is missing.

An EA is not a strategy with a start button. It is a small piece of unattended software with money attached, running on an unreliable connection against a server that can reject, requote or disconnect at any moment. Treat it that way from the first line.

The five modules, in build order

1. Order manager

Everything that touches a position goes through one place: opening, modifying, closing, and reading current state. Never scatter order calls through your logic.

This module owns the awkward realities:

  • Retry on recoverable errors — requotes and price-changed errors should retry a bounded number of times, then give up cleanly.
  • Broker constraints — minimum stop distance, lot step, minimum and maximum volume. Read these from the symbol, never hardcode them.
  • Magic numbers — so your EA touches only its own trades, and two EAs on one account never fight.
  • Slippage tolerance — explicit, not default.

2. Risk module

Nothing calculates lot size except this module. It takes the stop distance and account equity and returns a size. It should refuse to trade at all when a limit is breached.

It holds four limits:

LimitPurpose
Risk per tradeCaps a single bad trade
Max concurrent positionsCaps correlated exposure
Daily loss limitCaps a bad session
Max total drawdownStops the EA entirely

Write this before the entry logic. If the risk module is added afterwards, it always ends up bolted on and easy to bypass.

3. Kill switch

One boolean, checked at the top of every tick, that stops all new orders. It should trip on:

  • Daily or total loss limit reached
  • Spread above a defined maximum
  • A high-impact news window, if you filter for it
  • Connection loss or trade context busy
  • Manual override via an input parameter

This is the single most valuable thing in an EA that runs unattended. Every serious algo failure story ends with someone wishing they could have stopped it remotely.

4. State persistence

Your EA will restart. VPS reboots, terminal updates, platform crashes. On restart it must reconstruct what it was doing by reading open positions and any values written to persistent storage — not by assuming it starts fresh.

An EA that forgets it already has a position open will happily open a second one. This is one of the most common ways a small bug becomes a large loss.

5. Logging

Log every decision with a timestamp, including decisions not to trade and why. When something goes wrong at 2am, the log is the only witness.

Write it to file, not just to the terminal's Experts tab, which does not survive a restart.

An unattended system is only as good as its ability to stop itself.

Only now: the strategy

With the scaffolding in place, the strategy layer becomes small — usually a function that returns "buy", "sell" or "nothing", plus a stop level. That is it. It does not calculate lots, does not send orders, does not check limits. It answers one question, and the infrastructure handles the consequences.

The benefit is testability. When results look wrong you can isolate whether the idea was wrong or the plumbing was. In a single tangled file, you never can.

Mistakes that show up again and again

Trading every tick

Unless the strategy is genuinely tick-based, act once per completed bar. Otherwise a signal fires, unfires and refires as the bar forms.

Ignoring the return value

Order functions fail. Not checking whether the order actually opened leads to an EA convinced it holds a position it does not.

Hardcoding symbol properties

Pip value, digits, contract size and stop level all differ between symbols and brokers. Read them at runtime. This is the number one reason an EA that works on EURUSD behaves strangely on XAUUSD.

No spread filter

Without one, the EA will happily enter during the widest spread of the week.

Testing only on the default model

Covered in detail in why your backtest lied to you — but in short, default tester settings flatter almost every EA.

Before it touches real money

  1. Compile clean. Zero warnings, not just zero errors.
  2. Test each module alone — force an order rejection, force a limit breach, restart mid-trade.
  3. Backtest with realistic assumptions, then walk forward.
  4. Demo for at least two months on the broker you will actually use.
  5. Live at minimum size. Compare live fills against backtest fills for the same signals — that gap is your true execution cost.
  6. Then, slowly, scale.

Most of the work in a reliable EA is not the idea. It is the handling of everything that can go wrong around the idea — and that is precisely the part beginners skip, because it is invisible when things are working.

For the risk arithmetic that belongs in module two, read position sizing is the only edge you fully control.

Education note

This article covers software architecture for educational purposes. It is not investment advice, and nothing here is a recommendation to trade any strategy or to use automated trading. Automated systems can fail and can lose money rapidly; test thoroughly and never risk capital you cannot afford to lose.