Skip to content
MT5MT4soonTradingViewsooncTradersoonNinjaTradersoonPythonsoon
MetaTrader & platform basics

iCustom

Also called iCustom(), iCustom function, iCustom handle

iCustom is the MQL5 function an Expert Advisor uses to load a custom indicator and read its values. It returns a handle, and the EA then copies the indicator's buffers to decide when to trade.

If an EA trades from a custom indicator, iCustom is the link between the arrow you see and the order that gets sent. When that link is set up wrongly, the EA does not crash. It trades on the wrong numbers with your real position size.

What iCustom does inside an Expert Advisor

An EA cannot look at a chart. It can only read numbers.

The function gives back a handle, which is a reference number for that running copy of the indicator. The EA then uses a second function, CopyBuffer, to pull values out of one of the indicator's buffers (the number arrays behind each line or arrow).

In code, the two steps look like this:

int h = iCustom(_Symbol, PERIOD_H1, "MyArrows", 14, 2.0);
double buy[];
CopyBuffer(h, 0, 1, 1, buy);

Minimal version, no error checks; the safe version is at the end of this page.

The iCustom arguments are, in order: the symbol, the timeframe, the indicator's file name, then the indicator's inputs in the order it lists them (here a period of 14 and a multiplier of 2.0).

The CopyBuffer line reads buffer 0, starting at bar 1, for one bar. Bar 0 is the candle still forming and bar 1 is the last closed candle. The drawing below shows the chain.

How an EA reads a custom indicator buffer through iCustom and CopyBuffer
Bar 1, the last closed candle, is the usual bar to act on. A value on bar 0 can still disappear.

Some indicators repaint, meaning they redraw closed bars too. If past arrows shift during a visual backtest, reading bar 1 will not protect you; backtest results from that indicator will look better than live trading.

Four ways iCustom feeds your EA the wrong numbers

Swapped buffer index

The figures here are an illustrative example. Your EA runs on a 1-hour EURUSD chart and calls an arrow indicator. Buffer 0 holds buy arrows and buffer 1 holds sell arrows.

On the last closed candle, buffer 0 returns 1.0850 and buffer 1 returns empty. That is a buy signal.

Now suppose the code has the two indexes swapped: it reads buffer 1 as the buy buffer and buffer 0 as the sell buffer. The value 1.0850 in buffer 0 is then treated as a sell arrow, so the EA sells on a buy arrow, against the signal you thought you were following. The drawing below shows both readings of that one bar.

The same closed bar read with correct and swapped iCustom buffer indexes
Nothing in the indicator changed. Only the buffer index in the EA's code decides buy or sell.

Inputs missing or in the wrong order

Settings are passed in the order the indicator lists them. If the indicator expects a period of 14 and then a multiplier of 2.0, and the EA passes only 2.0, the period becomes 2, and the multiplier that was not passed falls back to the indicator's default. Pass 2.0, 14 instead of 14, 2.0 and the period becomes 2 while the multiplier becomes 14. The EA is now trading a different indicator from the one on your chart.

How iCustom inputs fill the indicator's settings in the order the indicator lists them
Inputs are matched by position, not by name. When the 14 is left out, the 2.0 goes into the period slot, so the EA reads a period-2 indicator.

Testing for > 0 instead of EMPTY_VALUE

A bar with no arrow usually holds EMPTY_VALUE, an MQL5 constant equal to the largest number a double can hold, not zero. An EA that tests "value greater than 0" sees a signal on every bar. The rule: test that the value is not EMPTY_VALUE and not 0, since some indicators use 0 as empty.

Testing an indicator buffer for EMPTY_VALUE versus testing value greater than 0
Illustrative values. EMPTY_VALUE is a very large number, so a value greater than 0 test treats every bar with no arrow as a signal. Only the bar with the arrow passes the correct test.

Reading before the indicator has calculated

On the first tick, while the indicator is still calculating, CopyBuffer can return -1 or fewer values than requested. The rule: if the handle is invalid, stop the EA in OnInit; if CopyBuffer returns fewer values than requested, skip the tick and do not trade.

Where you see and set iCustom on your trading platform

You never click iCustom. It lives in the EA's code, so you meet it through three places:

  • The indicator file. It normally sits in the MQL5/Indicators folder inside the platform's data folder. The name in the call is the path from that folder with no .ex5 extension, for example "Examples\\MyArrows" for a file in the Examples subfolder. An indicator can also be embedded in the EA as a resource, so a separate file is not always needed.
  • The EA's inputs. Most EAs expose the indicator's settings as their own inputs and forward them through iCustom. Changing the indicator on the chart changes nothing for the EA.
  • The Experts tab. A failed load shows up there as a "cannot load custom indicator" message. The EA then holds an invalid handle and has nothing to read.

Checks before you let an iCustom EA trade

  • Attach the same indicator to the chart with the same settings the EA uses. Arrows and trades should line up candle for candle.
  • If they do not match, check in this order: inputs, buffer index, bar index.
  • Treat the order in the indicator's Data Window as a first guess only. It lists plots, not buffers: colour-index and calculation-only buffers are not shown, and one plot can use several buffers. Confirm by reading each buffer index on a bar where an arrow is visible and matching the printed value to the arrow's price, or use the indicator's documentation or source.
  • Run a visual backtest and watch a few entries one by one before using real money.
Finding which iCustom buffer index holds the arrow's price
Illustrative values. Only buffer 2 prints the arrow's price, so buffer 2 is the one the EA should read, even though it is not the first buffer.

A safe iCustom pattern you can copy

The snippet below puts this page's rules in one place: the handle is created once and checked, CopyBuffer's return is checked against the count, and the arrow is tested against EMPTY_VALUE and 0 on the last closed candle.

int h = INVALID_HANDLE;

int OnInit()
{
   // Create the handle once, inputs in the indicator's own order
   h = iCustom(_Symbol, PERIOD_H1, "MyArrows", 14, 2.0);
   // Stop here if the indicator failed to load
   if(h == INVALID_HANDLE) return INIT_FAILED;
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
   // Release the indicator when the EA is removed
   IndicatorRelease(h);
}

void OnTick()
{
   double buy[];
   // Read buffer 0 on bar 1; skip the tick if fewer than 1 value came back
   if(CopyBuffer(h, 0, 1, 1, buy) < 1) return;
   // A real arrow is neither EMPTY_VALUE nor 0
   if(buy[0] != EMPTY_VALUE && buy[0] != 0)
   {
      // Buy signal on the last closed candle
   }
}

FAQ

Can iCustom read boxes, lines or text an indicator draws on the chart?

No. It reads buffers only. Zones and labels drawn as chart objects are invisible to it unless the indicator also writes those levels into a buffer.

Do I need the indicator's source code to call it with iCustom?

No. The compiled file is enough. You do need its input list in the right order and to know which buffer holds what.

Why does iCustom code written in the older MQL4 language not work in MQL5?

In MQL4 the function returns the indicator value directly, with the buffer and bar given in the same call. In MQL5 it returns a handle, and the values come from a separate CopyBuffer step.

Does calling iCustom on every tick slow the EA down?

It can. The platform reuses the same indicator copy for identical parameters, so calling iCustom every tick does not create thousands of indicator copies; the cost is wasted work on each tick. Only the buffer copy needs to repeat.

Related terms