ta.highest in MQL5: ArrayMaximum, iHighest and the window edge

Short answer

Take high[ArrayMaximum(high, i-len+1, len)] on oldest-first arrays, or iHighest on the chart's price series, with an empty value for the first len - 1 bars. The window includes the current bar.

ta.highest(source, length) returns the largest of the last length values of source, the current bar included. The one-argument form ta.highest(length) uses high. In MQL5 the same number comes from ArrayMaximum over the window that ends at the current index, or from iHighest when you work with the chart's price series directly.

What ta.highest computes

On bar n the result is max(source[0], source[1], ..., source[length - 1]), where source[0] is the current bar. The window therefore spans length bars and ends on the bar being evaluated; it does not start one bar back. While fewer than length bars exist, or while any value inside the window is na, the result is na. For a 20-bar window the first defined value is on the 20th bar of the chart (bar index 19).

The value changes on the realtime bar as its high extends, so a breakout test such as close > ta.highest(high, 20)[1] is usually written against the previous bar's value; otherwise the current high is part of the window and the current close can never exceed it. That [1] is a separate history read, not part of the function.

The MQL5 equivalent

Pine: the upper line of a 20-bar channel.

//@version=6
indicator("Highest high", overlay=true)
len = input.int(20, "Length")
hh = ta.highest(len)
plot(hh, "Highest")

MQL5. The high[] array passed into OnCalculate is oldest-first, so the window ending at bar i is the Length cells starting at i - Length + 1, and ArrayMaximum(array, start, count) returns the index of the largest one.

//+------------------------------------------------------------------+
//| Highest high channel line: Pine's ta.highest(high, len) in MQL5  |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "Highest"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue

input int Length = 20;

double Highest[];

int OnInit()
{
   SetIndexBuffer(0, Highest, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   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[])
{
   int start = (prev_calculated > 1) ? prev_calculated - 1 : 0;
   for(int i = start; i < rates_total; i++)
   {
      // Pine returns na until `Length` bars exist, the current bar included.
      if(i < Length - 1) { Highest[i] = EMPTY_VALUE; continue; }
      // Arrays are oldest-first: the window is the Length cells ending at i.
      int idx = ArrayMaximum(high, i - Length + 1, Length);
      Highest[i] = high[idx];
   }
   return rates_total;
}

If you prefer the chart-series functions, iHighest(_Symbol, _Period, MODE_HIGH, Length, shift) returns the series index (0 = newest bar) of the highest high among Length bars starting at shift bars back; convert an oldest-first i to a shift with rates_total - 1 - i, then read iHigh(_Symbol, _Period, idx). It answers the same question but costs a function call per bar and only works on the chart's own OHLC, not on a buffer you computed.

Where the two differ

TopicPine ScriptMQL5
WindowCurrent bar and the length - 1 before itArrayMaximum(src, i - len + 1, len) oldest-first; ArrayMaximum(src, i, len) after ArraySetAsSeries(true)
Early barsna until length bars existNothing stops you reading src[-1]; guard with i < len - 1 and write EMPTY_VALUE
na inside the windowResult is naArrayMaximum happily returns EMPTY_VALUE (a huge number) as the maximum if a cell holds it; skip or guard such cells
Default sourceta.highest(len) means highName the array explicitly
Typefloat, or int for an int sourcedouble; volume and tick_volume are long arrays and need their own loop or a cast

What the Stratlate converter emits

The engine treats ta.highest as a windowed builtin and emits pine_highest(src, bar, len), whose runtime body returns na while i < len - 1 or any value in the window is na and otherwise scans the len cells ending at the current bar. The one-argument form is recognised and rewritten to the high array (ta.highest(len) became pine_highest(high, __pineBar, (int)(len)) in the test above); a computed source such as ta.highest(close - open, 10) is first written into a per-script array each bar and the same helper reads it (pine_highest(__ts0, __pineBar, (int)(10)) in testing); where no array can be built, pine_highest_step stores the value into a call-site window array and runs the same scan. The mapping is exact.

Pitfalls

  • Starting the window one bar back (i - len to i - 1). Symptom: the line lags TradingView by one bar and breakouts register a bar late.
  • Passing ArrayMaximum(high, i, Length) on an oldest-first array. Symptom: the function looks forward into bars that do not exist yet and returns garbage or the last bar's high for every bar.
  • No guard for the first Length - 1 bars. Symptom: an array out of range error on attach, or a line that starts at a wrong level.
  • Calling ta.highest on a series that contains EMPTY_VALUE before warm-up. Symptom: the channel shoots to 1.797e308 on early bars.
  • Reading iHighest with an oldest-first index as the shift. Symptom: the highest of the wrong end of the chart.

FAQ

Does ta.highest include the current bar?

Yes. The window is the current bar plus the previous length - 1 bars, which is why a breakout test normally compares the close with ta.highest(high, len)[1].

Is iHighest the same as ta.highest?

It finds the same bar for the chart's own OHLC series when count = length and the shift points at the current bar. It returns an index, not a value, and cannot run on a computed buffer; ArrayMaximum can.

Why is the converted line na for the first 19 bars with length 20?

That is TradingView's behaviour too: the window needs 20 values, so the first defined result is on bar 20. The runtime helper reproduces it with EMPTY_VALUE in the plot buffer.

Convert the whole script

Paste a Pine Script indicator or strategy and get MQL5 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