Ichimoku Cloud: TradingView (Pine Script) to MT5 (MQL5)
Short answer
Each of the five lines is a midpoint of the highest high and lowest low over a window; use MetaTrader's iIchimoku or port the arithmetic into buffers. Care is needed with the 26-bar displacement: the stored value belongs to the bar it was computed on, and only the drawing moves.
The Ichimoku Cloud is five lines built from midpoints of the highest high and lowest low over three windows, two of them drawn 26 bars into the future and one 26 bars into the past. MetaTrader 5 ships it as iIchimoku, so you can either read the native handle or port the arithmetic into your own buffers; both are shown below. The arithmetic ports in a few lines. The displacement is what needs care, because on both platforms the stored value belongs to the bar it was computed on and only the drawing moves.
How the Ichimoku Cloud is calculated
Defaults 9 / 26 / 52 with a displacement of 26. Writing mid(n) for (highest high over n bars + lowest low over n bars) / 2:
- Tenkan-sen = mid(9)
- Kijun-sen = mid(26)
- Senkou Span A = (Tenkan + Kijun) / 2, drawn 26 bars ahead
- Senkou Span B = mid(52), drawn 26 bars ahead
- Chikou Span = close, drawn 26 bars back
The cloud (kumo) is the area between the two Senkou spans, coloured by which one is on top. "Drawn 26 bars ahead" means the value computed on bar t is displayed at t + 26; the series itself is not shifted. So the cloud that sits above or below the current bar was computed 26 bars ago and is already final, while the cloud at the right edge of the chart is being recomputed on every tick.
One convention differs between the platforms. TradingView's built-in Ichimoku draws the spans with offset = displacement - 1 (25 bars with the default input) and the Chikou with -displacement + 1; MetaTrader's Ichimoku shifts by exactly the Kijun period, 26. The Pine version below takes the shift as an explicit input, so set it to 26 to match MetaTrader or 25 to match the TradingView built-in.
Pine Script v6 version
An original implementation. The midpoint helper is called three times with different lengths; ta.highest and ta.lowest have no carried state, so this is safe. The fill colour switches with the relative position of the two spans.
//@version=6
indicator("Ichimoku Cloud", shorttitle="Ichimoku", overlay=true)
tenkanLen = input.int(9, "Tenkan-sen length", minval=1)
kijunLen = input.int(26, "Kijun-sen length", minval=1)
senkouLen = input.int(52, "Senkou Span B length", minval=1)
shift = input.int(26, "Displacement (bars)", minval=1)
// Midpoint of the highest high and lowest low over len bars.
midpoint(len) => math.avg(ta.highest(high, len), ta.lowest(low, len))
tenkan = midpoint(tenkanLen)
kijun = midpoint(kijunLen)
senkouA = math.avg(tenkan, kijun)
senkouB = midpoint(senkouLen)
plot(tenkan, "Tenkan-sen", color=color.blue)
plot(kijun, "Kijun-sen", color=color.red)
plot(close, "Chikou Span", color=color.green, offset=-shift)
pA = plot(senkouA, "Senkou Span A", color=color.teal, offset=shift)
pB = plot(senkouB, "Senkou Span B", color=color.maroon, offset=shift)
fill(pA, pB, color=senkouA >= senkouB ? color.new(color.teal, 85) : color.new(color.maroon, 85), title="Kumo")To use the cloud in logic rather than on the chart, index the span series, not the plot: senkouA[shift] is the Span A value displayed above the current bar, and close > math.max(senkouA[shift], senkouB[shift]) is "price above the cloud". The Chikou test "lagging span above price" is close > close[shift].
MQL5 version
First the native route. iIchimoku(symbol, period, tenkan, kijun, senkou) returns a handle with five buffers, addressed by the constants TENKANSEN_LINE, KIJUNSEN_LINE, SENKOUSPANA_LINE, SENKOUSPANB_LINE and CHIKOUSPAN_LINE. The buffers hold the undisplaced values, and CopyBuffer indexes newest-first. The cloud displayed around the last closed bar (index 1) was computed on bar 1 + kijun, which is the index you must read. This Expert Advisor compiles and prints the position of the last closed bar relative to the cloud once per bar:
//+------------------------------------------------------------------+
//| Reads the terminal's own Ichimoku through a handle and reports |
//| where the last closed bar sits relative to the cloud. |
//+------------------------------------------------------------------+
input int InpTenkan = 9; // Tenkan-sen length
input int InpKijun = 26; // Kijun-sen length (also the displacement)
input int InpSenkou = 52; // Senkou Span B length
int handle = INVALID_HANDLE;
int OnInit()
{
handle = iIchimoku(_Symbol, _Period, InpTenkan, InpKijun, InpSenkou);
return (handle == INVALID_HANDLE) ? INIT_FAILED : INIT_SUCCEEDED;
}
void OnDeinit(const int reason)
{
if(handle != INVALID_HANDLE)
IndicatorRelease(handle);
}
void OnTick()
{
static datetime lastBar = 0;
datetime bar = iTime(_Symbol, _Period, 0);
if(bar == lastBar)
return; // once per new bar
lastBar = bar;
// The span buffers hold the value computed ON a bar; the terminal draws
// them InpKijun bars later. The cloud drawn around the last closed bar
// (index 1) was therefore computed on bar 1 + InpKijun.
double spanA[1], spanB[1], tenkan[1], kijun[1];
if(CopyBuffer(handle, SENKOUSPANA_LINE, 1 + InpKijun, 1, spanA) != 1
|| CopyBuffer(handle, SENKOUSPANB_LINE, 1 + InpKijun, 1, spanB) != 1
|| CopyBuffer(handle, TENKANSEN_LINE, 1, 1, tenkan) != 1
|| CopyBuffer(handle, KIJUNSEN_LINE, 1, 1, kijun) != 1)
return; // data not ready yet
double c = iClose(_Symbol, _Period, 1);
double top = MathMax(spanA[0], spanB[0]);
double bot = MathMin(spanA[0], spanB[0]);
string where = (c > top) ? "above" : (c < bot) ? "below" : "inside";
// Chikou check: close[1] drawn InpKijun bars back, against the close there.
bool chikouBullish = c > iClose(_Symbol, _Period, 1 + InpKijun);
PrintFormat("close %s the cloud tenkan=%.5f kijun=%.5f chikou %s",
where, tenkan[0], kijun[0], chikouBullish ? "above price" : "below price");
}Now the buffer port, for when you want Pine-compatible warm-up behaviour, a displacement other than the Kijun period, or the whole thing inside one indicator. Seven buffers: five lines, plus a pair for the DRAW_FILLING plot that paints the kumo in two colours. The OnCalculate arrays are oldest-first, so the window for bar i runs backwards from i; PLOT_SHIFT does the displacement exactly as Pine's offset= does.
//+------------------------------------------------------------------+
//| Ichimoku Cloud: buffers hold the value computed on each bar; |
//| PLOT_SHIFT moves the spans forward and the Chikou back on screen. |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots 6
#property indicator_label1 "Tenkan-sen"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_label2 "Kijun-sen"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_label3 "Chikou Span"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrGreen
#property indicator_label4 "Senkou Span A"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrTeal
#property indicator_label5 "Senkou Span B"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrMaroon
#property indicator_label6 "Kumo"
#property indicator_type6 DRAW_FILLING
#property indicator_color6 clrPaleTurquoise,clrMistyRose
input int InpTenkan = 9; // Tenkan-sen length
input int InpKijun = 26; // Kijun-sen length
input int InpSenkou = 52; // Senkou Span B length
input int InpShift = 26; // Displacement (bars)
double TenkanBuf[], KijunBuf[], ChikouBuf[], SpanABuf[], SpanBBuf[];
double KumoABuf[], KumoBBuf[]; // the filling plot needs its own pair
int OnInit()
{
if(InpTenkan < 1 || InpKijun < 1 || InpSenkou < 1 || InpShift < 1)
return INIT_PARAMETERS_INCORRECT;
SetIndexBuffer(0, TenkanBuf, INDICATOR_DATA);
SetIndexBuffer(1, KijunBuf, INDICATOR_DATA);
SetIndexBuffer(2, ChikouBuf, INDICATOR_DATA);
SetIndexBuffer(3, SpanABuf, INDICATOR_DATA);
SetIndexBuffer(4, SpanBBuf, INDICATOR_DATA);
SetIndexBuffer(5, KumoABuf, INDICATOR_DATA);
SetIndexBuffer(6, KumoBBuf, INDICATOR_DATA);
for(int p = 0; p < 6; p++)
PlotIndexSetDouble(p, PLOT_EMPTY_VALUE, EMPTY_VALUE);
// Displacement. Pine: plot(x, offset = shift) / plot(close, offset = -shift).
PlotIndexSetInteger(2, PLOT_SHIFT, -InpShift);
PlotIndexSetInteger(3, PLOT_SHIFT, InpShift);
PlotIndexSetInteger(4, PLOT_SHIFT, InpShift);
PlotIndexSetInteger(5, PLOT_SHIFT, InpShift);
// Warm-up: each line starts when its longest window is full.
int spanABegin = MathMax(InpTenkan, InpKijun) - 1;
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpTenkan - 1);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, InpKijun - 1);
PlotIndexSetInteger(3, PLOT_DRAW_BEGIN, spanABegin);
PlotIndexSetInteger(4, PLOT_DRAW_BEGIN, InpSenkou - 1);
PlotIndexSetInteger(5, PLOT_DRAW_BEGIN, MathMax(spanABegin, InpSenkou - 1));
IndicatorSetString(INDICATOR_SHORTNAME, "Ichimoku(" + IntegerToString(InpTenkan) + ","
+ IntegerToString(InpKijun) + "," + IntegerToString(InpSenkou) + ")");
return INIT_SUCCEEDED;
}
// Midpoint of the highest high and lowest low over len bars ending at i.
double Midpoint(const double &high[], const double &low[], const int i, const int len)
{
double hh = high[i], ll = low[i];
for(int k = 1; k < len; k++)
{
if(high[i - k] > hh) hh = high[i - k];
if(low[i - k] < ll) ll = low[i - k];
}
return (hh + ll) / 2.0;
}
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[])
{
int start = (prev_calculated > 0) ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
ChikouBuf[i] = close[i];
TenkanBuf[i] = (i >= InpTenkan - 1) ? Midpoint(high, low, i, InpTenkan) : EMPTY_VALUE;
KijunBuf[i] = (i >= InpKijun - 1) ? Midpoint(high, low, i, InpKijun) : EMPTY_VALUE;
SpanABuf[i] = (TenkanBuf[i] != EMPTY_VALUE && KijunBuf[i] != EMPTY_VALUE)
? (TenkanBuf[i] + KijunBuf[i]) / 2.0 : EMPTY_VALUE;
SpanBBuf[i] = (i >= InpSenkou - 1) ? Midpoint(high, low, i, InpSenkou) : EMPTY_VALUE;
bool kumo = (SpanABuf[i] != EMPTY_VALUE && SpanBBuf[i] != EMPTY_VALUE);
KumoABuf[i] = kumo ? SpanABuf[i] : EMPTY_VALUE;
KumoBBuf[i] = kumo ? SpanBBuf[i] : EMPTY_VALUE;
}
return rates_total;
}Reading this indicator from an EA works the same way as reading iIchimoku: buffers 3 and 4 hold the spans at the bar they were computed on, so the cloud at index k is at CopyBuffer index k + InpShift.
Differences you will see on the chart
| TradingView | MetaTrader 5 | |
|---|---|---|
| Displacement | offset= on the plot; built-in uses displacement − 1 | PLOT_SHIFT on the plot; built-in uses the Kijun period |
| First bars | Each line is na until its window is full (8, 25 and 51 bars) | This port writes EMPTY_VALUE there. The terminal's own Ichimoku averages a shortened window on the first bars and shows values where Pine shows nothing |
| Cloud colour | One fill() with a conditional colour | DRAW_FILLING with two colours: the first when buffer A is above B, the second otherwise |
| Right edge | Spans extend 26 bars past the last candle | Same; the terminal reserves space to the right of the last bar for shifted plots |
| Realtime bar | Tenkan, Kijun, the far end of the cloud and the Chikou's newest point move with each tick | Same bars move; only bar rates_total - 1 is recomputed |
| Repainting | None on closed bars | None on closed bars |
After warm-up the values of this port and the Pine version match exactly; they are comparisons and averages with no seeding. If you compare against the terminal's own Ichimoku instead, expect the first 51 bars of history to differ and everything after to agree.
Using it in MetaTrader 5
Save the buffer port as IchimokuPort.mq5 under MQL5\Indicators and the reader as IchimokuReader.mq5 under MQL5\Experts, compile each with F7. The indicator takes four inputs; leave Displacement at 26 for the classic drawing. For alerts on a cloud breakout, test close[i] > MathMax(SpanABuf[i - InpShift], SpanBBuf[i - InpShift]) for i = rates_total - 2 (the last closed bar) when a new bar opens; using the forming bar fires on every tick.
What the Stratlate converter emits
The Pine version converts CLEAN. ta.highest and ta.lowest become the per-bar window helpers pine_highest and pine_lowest, which return na until the window is full. Because the midpoint helper holds ta.* calls and is called from three places, the engine notes it (PM223) and emits specialised copies of the function so each call site keeps its own history, matching Pine's rule that every call has its own state. Each plot() with offset= gets a PlotIndexSetInteger(plot, PLOT_SHIFT, ...) line in OnInit, positive for the spans and negative for the Chikou, and fill() with the conditional colour becomes a DRAW_FILLING plot whose two colours are the two branches of the ternary. The remaining notes are informational: minval on the inputs is dropped. Compile the output and compare it with the TradingView chart before relying on it.
Pitfalls
- Reading
SENKOUSPANA_LINEat index 0 or 1 and calling it "the cloud at the current bar". That is the value computed on the current bar, drawn 26 bars ahead. The cloud around the current bar is at indexkijunorkijun + 1. - Shifting the data instead of the plot. Writing
SpanABuf[i + 26]inOnCalculateoverruns the array on the newest bars and breaks the incremental recalculation. Store ati, shift withPLOT_SHIFT. - Mixing a 25-bar and a 26-bar displacement. The cloud looks one bar off between the platforms and every "price crossed the cloud" signal lands on a neighbouring bar. Decide which convention you want and set both sides to it.
- Comparing early bars with the terminal's Ichimoku. Its shortened windows produce values on the first bars where Pine has
na; this is not a bug in either, just a convention. - Setting the fill buffers to 0 instead of
EMPTY_VALUEduring warm-up.DRAW_FILLINGthen paints a wedge down to zero at the left edge of the chart.
FAQ
Does iIchimoku support a displacement other than the Kijun period?
No. The native indicator shifts the spans forward and the Chikou back by exactly the Kijun-sen length. If you need a different shift, use the buffer port above, which has a separate InpShift input.
Why is the cloud one bar off between TradingView and MetaTrader?
TradingView's built-in draws the spans with an offset of displacement − 1 (25 bars by default), MetaTrader with the full Kijun period (26). The values are identical; only the drawing position differs. Set the shift input in the Pine version to 25 or 26 to match whichever platform you are comparing against.
How do I get the cloud value under a given bar from an EA?
Call CopyBuffer(handle, SENKOUSPANA_LINE, k + kijun, 1, a) and the same for Span B, where k is the bar's newest-first index. The cloud top is the maximum of the two, the bottom the minimum. This holds for both iIchimoku and the port on this page.
Convert your own version
Have a variant of this indicator, or any other TradingView script? Paste it and get MQL5 that is checked against the original on the same price history, with every construct that could not be carried over reported.
Open the converter