Ichimoku in MetaTrader 5 — MQL5 Source Code

Reading the built-in iIchimoku, and computing the lines yourself

Ichimoku in MetaTrader 5
Ichimoku in MetaTrader 5

MetaTrader ships a solid Ichimoku, and because the forward/backward displacement is fiddly to plot by hand, the sensible MQL5 approach is to read the built-in iIchimoku via a handle — then you have all five lines ready for an Expert Advisor. See the explainer for what each buffer means.

What you’ll need

  • MetaTrader 5 with MetaEditor

Reading iIchimoku in an EA

//+------------------------------------------------------------------+
//| AlgoGen Ichimoku reader — for use inside an Expert Advisor       |
//+------------------------------------------------------------------+
input int InpTenkan = 9;
input int InpKijun  = 26;
input int InpSenkouB = 52;

int    ichHandle;

int OnInit()
  {
   ichHandle = iIchimoku(_Symbol, _Period, InpTenkan, InpKijun, InpSenkouB);
   if(ichHandle == INVALID_HANDLE)
      return(INIT_FAILED);
   return(INIT_SUCCEEDED);
  }

// iIchimoku buffer indices:
//   0 = Tenkan, 1 = Kijun, 2 = Senkou Span A, 3 = Senkou Span B, 4 = Chikou
bool ReadIchimoku(double &tenkan, double &kijun,
                  double &spanA, double &spanB, double &chikou)
  {
   double t[], k[], a[], b[], c[];
   if(CopyBuffer(ichHandle, 0, 0, 2, t) < 2) return(false);
   if(CopyBuffer(ichHandle, 1, 0, 2, k) < 2) return(false);
   if(CopyBuffer(ichHandle, 2, 0, 2, a) < 2) return(false);
   if(CopyBuffer(ichHandle, 3, 0, 2, b) < 2) return(false);
   if(CopyBuffer(ichHandle, 4, 0, 2, c) < 2) return(false);
   tenkan = t[1]; kijun = k[1]; spanA = a[1]; spanB = b[1]; chikou = c[1];
   return(true);
  }

The handle-and-CopyBuffer pattern is the idiomatic MT5 way to consume any indicator inside an EA. Note that MT5 already applies the correct displacement to Senkou A/B and Chikou, so the values you read line up with the chart.

The math, if you build your own buffers

Each visible line is the same midpoint from the explainer:

// Tenkan over N bars ending at index i (non-series indexing):
double HighestHigh(const double &high[], int i, int n)
  {
   double hh = high[i];
   for(int j = 1; j < n; j++) if(high[i - j] > hh) hh = high[i - j];
   return hh;
  }
// midpoint = (HighestHigh(n) + LowestLow(n)) / 2, for n = 9, 26, 52.

Senkou A/B are then written into their buffers at index i + InpKijun (forward), and Chikou at i - InpKijun (back) — which is exactly the plumbing iIchimoku does for you, and why the built-in is the pragmatic choice.

Gotchas

  • Buffer order. iIchimoku buffers are Tenkan(0), Kijun(1), Senkou A(2), Senkou B(3), Chikou(4). Mixing these up is the usual bug.
  • Displacement is built in. Don’t re-shift the values you read from iIchimoku — they’re already displaced.
  • MQL4. iIchimoku exists in MQL4 too, with mode constants (MODE_TENKANSEN, etc.) instead of numeric buffers.

Same indicator elsewhere: Python, Pine Script, EasyLanguage, NinjaScript. Then backtest an Ichimoku rule in AlgoGen.


This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.

Sources and further reading

  1. Ichimoku: At a Glance (CMT Association)
  2. Technical indicator functions (MetaQuotes)

Historical research from the Algogen archive. Not investment advice.

Blog

Have a strategy idea?

Describe it in plain English, preview where your rules fire, then decide whether the evidence is worth a backtest.

Keep me postedHow it works