Guide · forex · MetaTrader

Building a forex trading bot (EA) for MT5

In forex, automation almost always means MetaTrader, and the bot is called an Expert Advisor. It is a small program that runs inside the platform, reacts to every price tick, and trades your rules through your broker. This guide covers what an EA is, a minimal skeleton to start from, when to reach for a broker API instead, and the forex-specific parts that catch people out. The strategy stays yours. We are showing the machinery.

What an Expert Advisor actually is

An EA is a program written in MQL5 that lives inside MetaTrader 5. The platform calls your code on every price update, and your code decides whether to do anything. Because almost every forex broker offers MetaTrader, an EA is the most portable way to automate a forex strategy, and the platform comes with a strategy tester for backtesting built in.

A minimal EA skeleton

Every EA has the same bones. There is a setup function that runs once, and a tick function that runs on every price update. Your rules go inside the tick function:

#include <Trade/Trade.mqh>
CTrade trade;

int OnInit()
{
   return(INIT_SUCCEEDED);
}

// OnTick runs on every price update. Your rules live here.
void OnTick()
{
   // 1. read the data your rules need (price, indicators, positions)
   // 2. decide, by YOUR rules, whether to act
   // 3. if a rule fires and you are flat, place the order:
   //    trade.Buy(0.10, _Symbol);   // 0.10 lots is an illustration, not advice
}

That is the whole shape of it. Everything else, reading indicators, sizing positions, managing stops and exits, hangs off that tick function. The skeleton is easy. Making it behave correctly in a live market is the work.

MetaTrader, or a broker API?

MetaTrader is the right default for forex, but it is not the only option. A direct broker connection, through cTrader, FIX or a REST API, is worth it when you need lower latency, tighter control over execution, or to run one strategy across several brokers at once. The broker is effectively part of your strategy in forex, so the choice of platform is a real decision, not a detail. We go into that on the forex automation page.

The forex-specific parts that trip people up

  • It runs all week. Forex only pauses at the weekend, so your EA has to run continuously. That means a VPS, and a system that reconnects on its own when the connection drops.
  • Brokers are not identical. Spreads, requotes, execution model and even the symbol names differ from one broker to the next, so an EA that assumes one broker's behaviour can misfire on another.
  • The strategy tester has limits. It is genuinely useful, but the quality of its tick data and its spread modelling shape the result, so a tester run is a starting point, not a promise.
  • Leverage is high. Forex is heavily leveraged, which magnifies both sides, so risk checks belong in the code, before the order, not as an afterthought.

You bring the strategy. We build the EA.

A skeleton that compiles is a long way from a system you would trust with a live account. The distance between them is the risk handling, the broker realities and the reliability that only matter once real money is on the line. If you would rather have that built properly, it is our forex automation and custom trading software work. We build the software to your rules. We do not supply strategies or trading calls, and we make no claim about returns. Forex is highly leveraged and carries real risk, which stays with you.

Common questions

What is an Expert Advisor (EA) in MetaTrader?
An Expert Advisor is an automated trading program that runs inside MetaTrader 4 or 5, written in the MQL language. It reacts on every price update, applies your rules, and places and manages orders through your broker. For most forex traders it is the standard way to automate, because almost every forex broker runs MetaTrader.
MetaTrader or a broker API for forex automation?
MetaTrader is the default, because the broker support is near universal and the strategy tester is built in. A direct broker API, through cTrader or FIX, makes sense when you need lower latency, tighter control, or to run one strategy across several brokers at once. We build both and pick the one your strategy and broker actually need.
Do I need a VPS to run a forex EA?
Usually yes. Forex trades around the clock through the week, so your EA has to run continuously, which is not practical on a laptop that sleeps or loses its connection. A small VPS keeps the EA running and connected all week for a modest monthly cost. It is one of the few pieces of infrastructure a forex EA genuinely needs.

More guides ›

start here

Tell us your market, your broker, and what you want automated.

Send us the requirement. We'll come back with the questions that turn it into a real scope, cost and timeline, usually within one working day.

Book a consultation