Blog
Convert MT5 Indicator to EA: Complete MQL5 Guide
How to Convert an MT5 Indicator into an Expert Advisor
Quick Answer
An MT5 indicator does not become an Expert Advisor merely by adding a buy or sell call. An indicator calculates and displays values; an EA must define when a signal is confirmed, read the correct indicator buffer, decide how to size and manage trades, submit orders, and handle failures. First identify the indicator's signal buffers and repainting behavior, then implement a separate execution and risk policy in the EA.
Key Facts
| Indicator | Expert Advisor |
|---|---|
| Calculates values in OnCalculate() | Responds to events such as OnTick() |
| Writes values to indicator buffers | Reads built-in/custom indicator values through handles and CopyBuffer() |
| May draw arrows or labels | Must translate a signal into explicit order rules |
| Can show intrabar signals | Must define whether it acts intrabar or on a closed bar |
| Does not necessarily manage positions | Must handle sizing, entries, exits, errors, and account mode |
An arrow on a chart may be a visual representation of a buffer value, a drawing object, or a value that later changes. Before automating it, inspect the indicator source or documentation. Determine which buffer represents buy/sell, what value means “no signal” (often EMPTY_VALUE for plotted buffers), whether the signal repaints, and whether historical and realtime behavior differ.
Write down the indicator contract before coding: input names and defaults, buffer indexes, buffer meaning, timeframe, and whether a value is final at bar close. For example, an arrow buffer might contain a price where an arrow should be drawn rather than a Boolean 1; testing only for a non-empty value may work for that specific output, while another indicator may encode the direction differently. Inspect sample values in Data Window or log copied buffers at known chart bars.
My rule when turning an indicator into an EA is to verify the signal buffer on completed bars before I let any order logic use it. A chart arrow can look convincing in hindsight; I want to know exactly what value the EA could have read at that moment.
Read a Custom Indicator with iCustom()
The EA creates a handle for the custom indicator and copies the documented signal buffer. The exact buffer index and input parameters below are placeholders: replace them with the indicator's real interface.
int signalHandle = INVALID_HANDLE;
input int SignalBufferIndex = 0;
int OnInit()
{
signalHandle = iCustom(_Symbol, _Period, "Examples\\MySignal",
14, 2.0);
if (signalHandle == INVALID_HANDLE)
{
Print("Could not load custom indicator. Error: ", GetLastError());
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
void OnDeinit(const int reason)
{
if (signalHandle != INVALID_HANDLE)
IndicatorRelease(signalHandle);
}
void OnTick()
{
double signals[];
ArraySetAsSeries(signals, true);
if (CopyBuffer(signalHandle, SignalBufferIndex, 1, 1, signals) != 1)
return;
if (signals[0] == EMPTY_VALUE)
return;
Print("A non-empty signal was read from the last closed bar.");
// Apply explicit entry, position, and risk rules here.
}
The code intentionally does not submit an order because “non-empty buffer” does not tell us whether the signal means buy or sell. Some indicators use separate buffers for direction; others encode direction in positive/negative values or use chart objects instead of buffers. Confirm the actual interface rather than guessing.
If the indicator source is available, an even clearer interface is to expose documented output buffers for long and short signals and use one agreed no-signal value. Keep visual plotting separate from signal meaning where practical. If only chart objects are available, the EA needs a documented way to identify the object, its creation time, and its lifecycle; object names or positions can change when the indicator redraws.
Define Execution Rules Separately
Before sending orders, specify:
- Signal timing: act on a forming bar, its first tick, or only after it closes?
- Signal interpretation: which buffer/value means long, short, or no action?
- Duplicate prevention: can repeated ticks or a persistent arrow open multiple trades?
- Position policy: one position per symbol, multiple entries, reversal, or pyramiding?
- Risk and exits: how are volume, stop loss, take profit, and trailing stops set?
- Ownership and errors: how are EA positions identified, and how are rejected/partial orders handled?
Use closed-bar signals when the strategy is intended to wait for confirmation. If the indicator repaints, record signals in realtime and compare them with the historical chart later. A backfilled arrow alone does not prove that the same signal was available at the time.
Before connecting trade execution, compare the EA's buffer reads with the indicator on a set of manually selected bars, including the current bar and prior closed bars. Then test duplicate signals, restart behavior, and order rejection. Only after the signal contract is verified should the EA apply risk-based lot sizing and position rules.
For source Pine Script rather than an MT5 custom indicator, conversion is a separate language and execution-model task; see the Pine Script to MQL5 guide. Review MQL5 CopyBuffer behavior before wiring a buffer to trade logic.
Frequently Asked Questions
Can every MT5 indicator be automated?
Only if its signal can be accessed reliably or reproduced. An indicator that relies on chart objects, external services, or undisclosed/repainting logic may need additional work or may not expose a dependable signal.
Does iCustom() return a signal value?
No. It returns a handle to the custom indicator. The EA uses CopyBuffer() to request values from a specified buffer.
Will an EA trade exactly where an indicator draws its arrow?
Not automatically. The arrow may appear intrabar, be confirmed later, or depend on visual objects rather than a buffer. Define the signal timing and compare timestamps in a test.
Test the Indicator-EA Contract
Document buffer indices, inputs, and signal timing, then validate them in Strategy Tester and on a demo account. Check both plotted history and realtime transitions. CodeFlowOS can help translate Pine Script into compiler-verified MQL5 output; any generated EA still needs an explicit risk policy and signal-by-signal validation.