iIchimoku in Pine Script: five buffers and the displacement

Short answer

Pine has no Ichimoku function: write each line as (ta.highest + ta.lowest) / 2 over its period. The displacement becomes plot's offset argument, and in MetaTrader it always equals the Kijun period.

iIchimoku returns a handle to MetaTrader's Ichimoku Kinko Hyo with five buffers. Pine Script has no Ichimoku function: each line is the midpoint of the highest high and lowest low over a period, written with ta.highest and ta.lowest, and the forward and backward displacement is the offset argument of plot(). In MetaTrader the displacement is not a separate input; it is always the Kijun period.

What iIchimoku computes

The signature is iIchimoku(symbol, timeframe, tenkan_sen, kijun_sen, senkou_span_b), defaults 9, 26 and 52. With mid(n) = (highest high of n bars + lowest low of n bars) / 2 the stock source (Indicators\Examples\Ichimoku.mq5) fills, for every bar:

  • buffer 0, TENKANSEN_LINE: mid(tenkan_sen);
  • buffer 1, KIJUNSEN_LINE: mid(kijun_sen);
  • buffer 2, SENKOUSPANA_LINE: (Tenkan + Kijun) / 2;
  • buffer 3, SENKOUSPANB_LINE: mid(senkou_span_b);
  • buffer 4, CHIKOUSPAN_LINE: the close.

The buffers hold the values of the bar they were computed on. The displacement is applied when drawing: the cloud plot has PLOT_SHIFT = kijun_sen (drawn ahead) and the Chikou plot has PLOT_SHIFT = −kijun_sen (drawn behind). On the first bars the stock code takes the highest and lowest over however many bars exist, so all lines have values from bar 0.

The Pine Script equivalent

Define the midpoint once as a function and build the four computed lines from it; the Chikou line is simply close. Apply MetaTrader's displacement with offset = kijunLen on the two spans and offset = -kijunLen on the Chikou plot, and shade between the spans with fill().

//@version=6
indicator("iIchimoku port", overlay = true)
tenkanLen = input.int(9, "Tenkan-sen", minval = 1)
kijunLen  = input.int(26, "Kijun-sen", minval = 1)
senkouLen = input.int(52, "Senkou Span B", minval = 1)

mid(len) => (ta.highest(high, len) + ta.lowest(low, len)) / 2

tenkan = mid(tenkanLen)            // TENKANSEN_LINE   (buffer 0)
kijun  = mid(kijunLen)             // KIJUNSEN_LINE    (buffer 1)
spanA  = (tenkan + kijun) / 2      // SENKOUSPANA_LINE (buffer 2)
spanB  = mid(senkouLen)            // SENKOUSPANB_LINE (buffer 3)

plot(tenkan, "Tenkan-sen", color.red)
plot(kijun, "Kijun-sen", color.blue)
// MetaTrader draws the cloud Kijun bars to the right and the Chikou Kijun bars to the left
a = plot(spanA, "Senkou Span A", color.green, offset = kijunLen)
b = plot(spanB, "Senkou Span B", color.maroon, offset = kijunLen)
fill(a, b, spanA > spanB ? color.new(color.green, 85) : color.new(color.maroon, 85))
plot(close, "Chikou Span", color.purple, offset = -kijunLen)   // CHIKOUSPAN_LINE (buffer 4)

The MQL5 indicator this replaces, reading all five buffers and setting the same plot shifts:

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   5
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrGreen
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrMaroon
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrPurple

input int InpTenkan = 9;  // tenkan_sen
input int InpKijun  = 26; // kijun_sen
input int InpSenkou = 52; // senkou_span_b

double TenkanBuffer[], KijunBuffer[], SpanABuffer[], SpanBBuffer[], ChikouBuffer[];
int    ichiHandle;

int OnInit()
  {
   SetIndexBuffer(0, TenkanBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, KijunBuffer,  INDICATOR_DATA);
   SetIndexBuffer(2, SpanABuffer,  INDICATOR_DATA);
   SetIndexBuffer(3, SpanBBuffer,  INDICATOR_DATA);
   SetIndexBuffer(4, ChikouBuffer, INDICATOR_DATA);
   PlotIndexSetInteger(2, PLOT_SHIFT,  InpKijun);   // cloud drawn ahead
   PlotIndexSetInteger(3, PLOT_SHIFT,  InpKijun);
   PlotIndexSetInteger(4, PLOT_SHIFT, -InpKijun);   // lagging span drawn behind
   ichiHandle = iIchimoku(_Symbol, _Period, InpTenkan, InpKijun, InpSenkou);
   if(ichiHandle == INVALID_HANDLE)
      return(INIT_FAILED);
   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],
                const double &open[], const double &high[], const double &low[], const double &close[],
                const long &tick_volume[], const long &volume[], const int &spread[])
  {
   if(BarsCalculated(ichiHandle) < rates_total)
      return(0);
   int to_copy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
   if(CopyBuffer(ichiHandle, TENKANSEN_LINE,   0, to_copy, TenkanBuffer) <= 0) return(0);
   if(CopyBuffer(ichiHandle, KIJUNSEN_LINE,    0, to_copy, KijunBuffer)  <= 0) return(0);
   if(CopyBuffer(ichiHandle, SENKOUSPANA_LINE, 0, to_copy, SpanABuffer)  <= 0) return(0);
   if(CopyBuffer(ichiHandle, SENKOUSPANB_LINE, 0, to_copy, SpanBBuffer)  <= 0) return(0);
   if(CopyBuffer(ichiHandle, CHIKOUSPAN_LINE,  0, to_copy, ChikouBuffer) <= 0) return(0);
   return(rates_total);
  }

Where the two differ

TopicMQL5 iIchimokuPine (hand-written)
Line formulasMidpoints of highest high / lowest lowIdentical with ta.highest / ta.lowest
DisplacementAlways the Kijun period, applied as PLOT_SHIFTAny offset you choose; use the Kijun period to match
TradingView's built-in—Has a separate displacement input and plots with displacement − 1, one bar less than MetaTrader
Buffer valuesUnshifted: span A on bar i is computed from bar iSeries unshifted; only the drawing moves
First barsPartial windows from bar 0na until each window is full
Cloud fillDRAW_FILLING between buffers 2 and 3fill() between two plots

For trading logic, the cloud "under the current bar" is the span computed kijun_sen bars ago: spanA[kijunLen] in Pine, and in MQL5 the buffer value kijun_sen bars back. Reading spanA without the index compares price with the cloud drawn 26 bars in the future.

What the Stratlate converter emits

The MQL5 → Pine engine dispatches iIchimoku on a line selector and expands each line to the ta.highest/ta.lowest midpoint formula, with Chikou as close (PineCatalog, entry iIchimoku). Its selector numbering is MQL4's, where MODE_TENKANSEN is 1, while MQL5 buffer indices start at 0, and the handle-form rewiring only reads literal buffer numbers. In our probes this went wrong: with literal indices 0–4 every buffer after the first received the line before it, and with the named constants (KIJUNSEN_LINE and so on) all five buffers received the Tenkan formula, each with an MQLW0501 approximation warning. The span plot shifts were carried to offset, the negative Chikou shift was not. For a converted MQL5 Ichimoku, replace the lines with the script above and check them on the chart.

Pitfalls

  • Using TradingView's built-in Ichimoku as the reference. It shifts by displacement − 1, so the cloud sits one bar to the left of MetaTrader's.
  • Comparing price with the unshifted span. The cloud under today's bar is spanA[kijunLen] and spanB[kijunLen].
  • Treating Chikou as a future value. It is today's close drawn in the past; a condition such as "Chikou above price" compares close with close[kijunLen].
  • Using the Tenkan period for the shift. MetaTrader shifts by the Kijun period; changing Tenkan never moves the cloud.
  • Expecting values on the first bars in Pine. MetaTrader uses partial windows there, Pine returns na until 52 bars exist for span B.

FAQ

Why is the cloud one bar off compared with TradingView's Ichimoku?

MetaTrader shifts by the Kijun period (26 by default); TradingView's built-in plots with displacement − 1 (25). Use offset = kijunLen to reproduce MetaTrader.

How do I read the cloud value for the current bar in Pine?

Index the span series by the displacement: spanA[kijunLen] and spanB[kijunLen]. Those are the values MetaTrader draws above or below the current candle.

Can I change the displacement independently in MetaTrader?

Not with iIchimoku; the shift is tied to kijun_sen. A custom indicator with its own PLOT_SHIFT input is needed, while in Pine it is just a different offset.

Convert the whole program

Paste an MQL4 or MQL5 indicator or Expert Advisor and get Pine Script v6 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.

Open the converter

Related