ta.lowestbars in MQL5: offset of the window low, bars since

Short answer

Find the index of the window minimum and subtract the current index on oldest-first arrays; the result is Pine's non-positive offset, 0 down to -(length - 1). It is na until the window is full.

ta.lowestbars(source, length) returns the position of the lowest value in the last length bars as a non-positive offset: 0 if the current bar is the window low, -3 if the low was three bars ago. ta.lowestbars(length) reads low. The MQL5 equivalent is the index of the window minimum minus the current index on oldest-first arrays, which is negative by construction.

What ta.lowestbars computes

Take the window of length values ending on the current bar, find the minimum, and return its distance back as a negative number. The window includes the current bar, so a new low gives 0. Before length bars exist the result is na. The most common use is -ta.lowestbars(low, len) as "bars since the window low", for example in the Aroon Down line, 100 * (len - bars_since_low) / len, and in scripts that fetch the close at the low bar with close[-ta.lowestbars(low, len)].

Exact ties inside the window are decided by the implementation. Prices seldom tie; discretised sources do. Keep that in mind when comparing output on a flat stretch.

The MQL5 equivalent

Pine: bars since the 20-bar low, drawn as a histogram.

//@version=6
indicator("Bars since low")
len = input.int(20, "Length")
age = -ta.lowestbars(low, len)
plot(age, "Bars since low", style=plot.style_histogram)

MQL5. LowestBarsAt returns the Pine offset; the indicator negates it for the plot, as the Pine script does.

//+------------------------------------------------------------------+
//| Bars since the window low: Pine's ta.lowestbars(low, len) in MQL5 |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "bars since low"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrOrangeRed
#property indicator_minimum 0

input int Length = 20;

double Age[];

// ta.lowestbars(src, len): the offset (0 or negative) of the lowest value
// in the window that ends at bar i. Strict '<': an exact tie keeps the
// older bar.
int LowestBarsAt(const double &src[], const int i, const int len)
{
   int best = i - len + 1;
   for(int k = best + 1; k <= i; k++)
      if(src[k] < src[best]) best = k;
   return best - i;
}

int OnInit()
{
   SetIndexBuffer(0, Age, 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++)
   {
      if(i < Length - 1) { Age[i] = EMPTY_VALUE; continue; }
      // Pine: -ta.lowestbars(low, len) is "bars since the window low".
      Age[i] = -LowestBarsAt(low, i, Length);
   }
   return rates_total;
}

The compact alternative is ArrayMinimum(low, i - Length + 1, Length) - i. If your arrays are in series order (index 0 is the newest bar), the window is ArrayMinimum(low, i, Length) and the Pine offset is i - idx.

Where the two differ

TopicPine ScriptMQL5
ReturnOffset, 0 or negativeCompute idx - i (oldest-first) or i - idx (series)
Early barsnaGuard i < len - 1 yourself; the Stratlate runtime answers 0 there
TiesImplementation-defined< keeps the older bar, <= the newer; the Stratlate runtime uses <
Reading at that barclose[-lb]close[i + lb] oldest-first, since lb is negative
Separate window scaleAutoscaledSet indicator_minimum 0 or let the histogram autoscale; a negative offset plotted directly needs indicator_maximum 0

What the Stratlate converter emits

ta.lowestbars is a windowed builtin in the emitter and becomes pine_lowestbars(src, bar, len); the one-argument form is rewritten to the low array (ta.lowestbars(len) converted to pine_lowestbars(low, __pineBar, (int)(len)) in testing), and a long source such as volume resolves to a matching overload. The runtime scans oldest to newest with a strict <, reports the older bar on a tie, and returns (best - i) as a double. As with pine_highestbars, the helper returns 0 instead of na on the first len - 1 bars; the mapping is otherwise exact and is not flagged in the report.

Pitfalls

  • Forgetting the negation. Symptom: an Aroon Down that reads 100 at every new low instead of 0, or a channel age that is negative.
  • Mixing the sign conventions of oldest-first and series arrays in one program. Symptom: values that look right on the newest bar and wrong everywhere else.
  • Using the age as an unbounded "bars since". Symptom: the number stops at len - 1; ta.barssince is the unbounded function.
  • Comparing output on a flat source without allowing for ties. Symptom: the offset differs from TradingView by a few bars while ta.lowest agrees exactly.
  • Plotting a non-positive value with indicator_minimum 0. Symptom: an empty subwindow; the plot is below the visible range.

FAQ

What is the difference between ta.lowest and ta.lowestbars?

ta.lowest returns the lowest value; ta.lowestbars returns how many bars back that value sits, as 0 or a negative offset. Both use a window of length bars that includes the current bar.

How do I read the close at the window low in MQL5?

Compute the offset lb as idx - i on oldest-first arrays and read close[i + lb]; lb is negative or zero so the index stays at or before i.

Why is the converted value 0 on the first bars where TradingView shows nothing?

The runtime helper pine_lowestbars returns 0 while the window is not full, where Pine returns na. It is a known early-bar difference; later bars match.

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