Overview
An Expert Advisor (EA) in MQL5 is an automated trading system that runs inside MetaTrader 5. It monitors the market, makes trading decisions, and can execute orders automatically based on your strategy. Unlike scripts, which run once and stop, an EA operates continuously — it reacts to every market tick or event.
EAs are written in the same MQL5 language used for indicators and scripts, but they rely on special event-handling functions to process data and react to market updates.
What You Will Be Able to Do
- Understand the structure and role of an Expert Advisor in MetaTrader 5.
- Recognize how event functions like
OnInit(),OnDeinit(), andOnTick()work together. - Create, compile, and run a simple EA in MetaEditor.
- Observe automated behavior in the Experts log.
Code Example
//+------------------------------------------------------------------+
//| SimpleEA.mq5 |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property version "1.00"
#property strict
// Called once when the EA is launched
int OnInit()
{
Print("SimpleEA initialized successfully.");
return(INIT_SUCCEEDED);
}
// Called on every new market tick
void OnTick()
{
string sym = _Symbol;
double bid = SymbolInfoDouble(sym, SYMBOL_BID);
Print("Tick received for ", sym, ": Bid = ", DoubleToString(bid, _Digits));
}
// Called when the EA is removed or the terminal closes
void OnDeinit(const int reason)
{
Print("SimpleEA stopped.");
}
Execution Steps
- Open MetaEditor → File → New → Expert Advisor (template).
- Name it
SimpleEAand click Finish. - Replace the template with the code above and press F7 to compile.
- In MetaTrader 5, open any chart and attach
SimpleEAto it. - Enable AutoTrading at the top of the terminal if it’s disabled.
- Open the Experts tab to see the initialization, tick, and stop messages appear in real time.
Key Point
An Expert Advisor is event-driven: OnInit() runs once at startup, OnTick() executes on every market update, and OnDeinit() runs when the EA is removed. This structure makes it ideal for continuous strategy execution, order management, and monitoring.
Next Section
→ Next: The OnTick Function


