ta.lowest in MQL5: ArrayMinimum, iLowest and the one-arg form
Short answer
Take src[ArrayMinimum(src, i-len+1, len)] on oldest-first arrays, or iLowest on the chart's price series, with an empty value for the first len - 1 bars. ta.lowest(length) alone reads low.
ta.lowest(source, length) returns the smallest of the last length values of source, counting the current bar; ta.lowest(length) alone reads low. The MQL5 equivalent is ArrayMinimum over the window of length cells that ends at the current index, or iLowest for the chart's price series.
What ta.lowest computes
The result on a bar is min(source[0], ..., source[length - 1]). The window includes the bar being evaluated and extends length - 1 bars into the past. Until length bars exist the result is na, and a single na inside the window also makes the result na. On the realtime bar the value can only fall as new ticks arrive; it is recomputed on each tick like every other Pine expression.
The one-argument form is a frequent source of silent bugs when translating by hand: ta.lowest(20) is ta.lowest(low, 20), so a reader who takes the 20 as the source and assumes a length of 1 ends up with a line equal to the current low. The same shortcut exists for ta.highest, ta.highestbars and ta.lowestbars.
The MQL5 equivalent
Pine: the lower channel line, written with the two-argument form.
//@version=6
indicator("Lowest low", overlay=true)
len = input.int(20, "Length")
ll = ta.lowest(low, len)
plot(ll, "Lowest")MQL5. The helper LowestAt mirrors Pine's signature and na rule, and the calculation loop stays in the default oldest-first order.
//+------------------------------------------------------------------+
//| Lowest low channel line: Pine's ta.lowest(low, len) in MQL5 |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "Lowest"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrOrangeRed
input int Length = 20;
double Lowest[];
// ta.lowest(src, len) over an oldest-first array: na until the window
// is full, then the minimum of the len cells ending at i (i included).
double LowestAt(const double &src[], const int i, const int len)
{
if(len <= 0 || i < len - 1) return EMPTY_VALUE;
int idx = ArrayMinimum(src, i - len + 1, len);
return src[idx];
}
int OnInit()
{
SetIndexBuffer(0, Lowest, 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++)
Lowest[i] = LowestAt(low, i, Length);
return rates_total;
}With the chart-series API the same value is iLow(_Symbol, _Period, iLowest(_Symbol, _Period, MODE_LOW, Length, shift)), where shift is the series index of the bar you are on; in an oldest-first loop that is rates_total - 1 - i. iLowest returns -1 on error, which must be checked before it is passed to iLow.
Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Window | Current bar plus length - 1 earlier bars | ArrayMinimum(src, i - len + 1, len) on oldest-first arrays |
| Early bars | na | Explicit guard, EMPTY_VALUE in the buffer, PlotIndexSetDouble(..., PLOT_EMPTY_VALUE, EMPTY_VALUE) so the line breaks there |
| na in window | na | ArrayMinimum ignores nothing; an EMPTY_VALUE cell is simply a large number and will not be chosen, which hides the problem rather than reporting it |
| Default source | low for the one-argument form | No default; pass low |
| Series API | None needed | iLowest returns an index in series order (0 = newest) |
What the Stratlate converter emits
ta.lowest is emitted as pine_lowest(src, bar, len), the twin of pine_highest: na while i < len - 1, na if any value in the window is na, otherwise the minimum of the len cells ending at the current bar. The emitter's optional-source table maps the one-argument form to the low array, so ta.lowest(len) converts to pine_lowest(low, __pineBar, (int)(len)). A computed source is materialised into an array first, or, where that is not possible, goes through pine_lowest_step with a per-call-site window array. The mapping is exact and the Pine example above converts CLEAN.
Pitfalls
- Reading
ta.lowest(20)as "source 20, length 1". Symptom: the line equals the current low and the channel has no width. - Window off by one bar in either direction. Symptom: a channel that lags or leads TradingView by exactly one bar, visible on every new low.
- Not setting
PLOT_EMPTY_VALUE. Symptom: the first bars draw at zero and the chart autoscale collapses. - Running the oldest-first loop on arrays you switched to series order elsewhere in the program. Symptom: the minimum is taken over future bars and the line looks shifted by the window length.
- Using
iLowestwith the oldest-first loop index asstart. Symptom: values from the wrong end of the chart.
FAQ
What does ta.lowest(20) with one argument do?
It is ta.lowest(low, 20): the lowest low of the current bar and the previous 19. The Stratlate engine rewrites the one-argument form to the low array for the same reason.
Why does ArrayMinimum take a start index and a count?
Because the window is a slice of the array. For oldest-first arrays the slice ending at bar i begins at i - len + 1 and has len cells; the function returns the index of the smallest cell, and you read the value from the array.
Can I use iLowest inside OnCalculate?
Yes, but it works on the symbol's price series in series order, so convert the loop index to a shift with rates_total - 1 - i and check for -1. For a computed buffer use ArrayMinimum instead.
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