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.
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()
{
static datetime lastProcessedBar = 0;
datetime currentBar = iTime(_Symbol, _Period, 0);
if (currentBar == 0 || currentBar == lastProcessedBar)
return;
double signals[];
ArraySetAsSeries(signals, true);
if (CopyBuffer(signalHandle, SignalBufferIndex, 1, 1, signals) != 1)
return;
lastProcessedBar = currentBar;
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 new-bar guard makes this example evaluate a completed-bar signal once per chart bar instead of re-reading the same signal on every tick. It 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 entry conversion guide and the step-by-step Pine-to-MQL5 guide. Review MQL5 CopyBuffer behavior before wiring a buffer to trade logic.
Related Guides
- MQL5 array out of range error: causes and a safe fix
- What to check in a trading bot before going live: a pre-live checklist for stops, lot size, spread and trade results
- Pine Script ta.rsi and ta.ema vs MQL5: why indicator values differ
- Pine Script input.* in MQL5: inputs, groups and dropdowns
- Pine Script to MQL5 converter: paste a strategy and get an MQL5 draft
- MQL5 Wrong Parameters Count Error: MT4 indicator calls that fail to compile in MT5
- cTrader C# error CS0534: a custom indicator that is missing its
Calculate(int index)method. - MQL5 OnCalculate not found: an indicator whose
OnCalculateis not one of the two valid forms. - MQL5 error 4014: system function is not allowed to call: trading functions in an indicator.
Frequently Asked Questions
Test the Indicator-EA Contract
Document buffer indices, inputs, and signal timing, then validate them in Strategy Tester and on demo. Check plotted history and realtime transitions. CodeFlowOS can produce an MQL5 draft with an optional platform check; read the verification notes, then review the risk policy and each signal yourself.
For a reproducible test, record the timestamp, buffer index, copied value, and expected action for each signal. Compare the EA log with the indicator's Data Window at the same bar. Test at least one no-signal bar, one long signal, one short signal, a missing-history case, and a restart while a position is open. A buffer value that looks right on a historical chart does not prove it was available in real time.
When the signal remains non-empty for several bars, decide whether that represents one event or a persistent state. A transition-based strategy should usually act only when the value changes from “no signal” to a direction; a state-based strategy may need to check whether a position already exists. Keep that rule separate from the buffer read so it can be reviewed independently.
Review related topics on CopyBuffer shifts and readiness, position ownership with magic numbers, MQL5 trade-result handling, and risk-based volume sizing. For a Pine source strategy, compare platform signal differences before treating matching indicator names as proof of matching behavior.
CodeFlowOS can create a first-pass translation when the source logic is in Pine Script. Try the converter, read the conversion workflow, or compare plans; confirm every signal and trade rule in MetaTrader before live use.