ta.pivotlow in MQL5: the delayed swing low and its offset
Short answer
Loop over left + right + 1 cells: the pivot is src[i-right] when no left bar is lower and no right bar is as low, returned right bars after the pivot. Store it on the pivot bar the way iFractals does, and keep a second buffer with Pine's delayed value.
ta.pivotlow(source, leftbars, rightbars) finds swing lows: a bar whose source is below the leftbars bars before it and the rightbars bars after it; the two-argument form reads low. The value is returned on the bar that confirms the pivot, rightbars bars after it, and is na on every other bar. This page shows the MQL5 version that stores the low on the pivot bar itself, the way MetaTrader's iFractals does, while keeping a second buffer identical to what Pine returns.
What ta.pivotlow computes
On bar n, take the candidate source[rightbars]. It is a pivot low when no bar among the leftbars before it is lower, and no bar among the rightbars after it, up to the current bar, is as low or lower. The function then returns the candidate's value on bar n; otherwise na. The pivot bar itself gets nothing: by the time the pivot is known, that bar is rightbars bars in the past.
The consequence for anyone comparing charts: a Pine plot of ta.pivotlow without an offset puts the marker rightbars bars to the right of the actual low; with offset=-rightbars it sits on the low, but still appears only when the confirming bar prints. On a live chart the newest rightbars bars never carry a confirmed pivot, and a candidate that looked good can be withdrawn if a newer bar prints a lower low before the window closes. That is not repainting in the sense of rewriting history; it is the definition of a swing low.
Ties are handled asymmetrically in the Stratlate runtime and in iFractals: an equal low on the left does not disqualify the candidate, an equal low on the right does. A flat bottom therefore pivots at its last bar.
The MQL5 equivalent
Pine: 5/5 swing lows, drawn on the pivot bar.
//@version=6
indicator("Pivot lows", overlay=true)
left = input.int(5, "Left")
right = input.int(5, "Right")
pl = ta.pivotlow(left, right)
plot(pl, "Pivot low", style=plot.style_circles, offset=-right)MQL5. Confirmed[i] is exactly ta.pivotlow on bar i; AtPivotBar[i - Right] receives the same value so the arrow draws on the low without a plot shift. Writing EMPTY_VALUE too, every time, erases a candidate that a newer bar has invalidated.
//+------------------------------------------------------------------+
//| Swing lows: Pine's ta.pivotlow(left, right) in MQL5 |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_label1 "pivot low"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
input int Left = 5;
input int Right = 5;
double AtPivotBar[]; // plotted: the low, stored on the pivot bar itself
double Confirmed[]; // what ta.pivotlow returns on each bar (stored on the confirmation bar)
double PivotLowAt(const double &src[], const int i, const int left, const int right)
{
if(i < left + right) return EMPTY_VALUE;
int p = i - right;
double cand = src[p];
for(int k = p - left; k < p; k++) if(src[k] < cand) return EMPTY_VALUE; // left: ties allowed
for(int k = p + 1; k <= i; k++) if(src[k] <= cand) return EMPTY_VALUE; // right: ties disqualify
return cand;
}
int OnInit()
{
SetIndexBuffer(0, AtPivotBar, INDICATOR_DATA);
SetIndexBuffer(1, Confirmed, INDICATOR_CALCULATIONS);
PlotIndexSetInteger(0, PLOT_ARROW, 233);
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++)
{
Confirmed[i] = PivotLowAt(low, i, Left, Right);
// Move the value back to the bar it describes, the way iFractals
// stores a fractal on its own bar. Writing EMPTY_VALUE too keeps
// the slot correct if the forming bar later breaks the low.
if(i >= Right) AtPivotBar[i - Right] = Confirmed[i];
if(i > rates_total - 1 - Right) AtPivotBar[i] = EMPTY_VALUE; // not yet confirmable
}
return rates_total;
}MetaTrader's Fractals example indicator is this program with Left = Right = 2 and low as the source: it tests low[i] < low[i+1] && low[i] < low[i+2] && low[i] <= low[i-1] && low[i] <= low[i-2] on oldest-first arrays, stores the value at i, and stops its loop three bars before the newest bar, so the forming bar never takes part. The iFractals handle exposes that buffer (lower fractals are buffer 1); anything other than 2/2 needs a custom loop.
Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Bar that holds the value | Confirmation bar | Either; Confirmed above matches Pine, AtPivotBar matches iFractals |
| Early bars | na while n < left + right | Guard the same bound; the first left bars can never be pivots |
Newest right bars | No confirmed pivot possible | Write EMPTY_VALUE there on every pass |
| Parameters | Any | iFractals: 2/2 only; custom loop: any |
| Using the value | ta.valuewhen(not na(pl), pl, 0) for the last swing low | Scan Confirmed[] backwards for the last non-empty cell, or keep a running "last pivot" buffer |
What the Stratlate converter emits
The emitter's pivot handler accepts both arities; ta.pivotlow(left, right) converts to pine_pivotlow(low, __pineBar, (int)(left), (int)(right)), and a three-argument call passes the named source array. The runtime mirrors pine_pivothigh: na while i < left + right, candidate src[i - right], na if any value in the window is na, disqualified by a strictly lower left bar or an equal-or-lower right bar, otherwise returned on the confirmation bar. offset=-right on the plot becomes a PLOT_SHIFT of -right. The example converts CLEAN; the mapping is exact.
Pitfalls
- Reading the low from the pivot bar's cell in an EA as soon as it appears, then entering "at the low". Symptom: the Strategy Tester shows entries at exact bottoms that live trading never gets.
- Never writing
EMPTY_VALUEinto the pivot-bar cell. Symptom: a lower low on the forming bar leaves the earlier, now invalid, marker in place. - Using
iFractalsfor a 3/3 or 5/5 script. Symptom: many more pivots than TradingView shows, each confirmed sooner. - Letting the loop write
AtPivotBar[i - Right]fori < Right. Symptom: array out of range on the first bars after attach. - Comparing the pivot-bar buffer against the Pine plot without the offset. Symptom: every marker appears
rightbars apart between the platforms although the values are identical.
FAQ
Why is ta.pivotlow na on the bar where the low actually is?
Because on that bar the right-hand bars have not printed yet. The pivot is confirmed rightbars bars later and that is the bar on which the value is returned; plotting with a negative offset only moves the drawing.
Can I get ta.pivotlow(5, 5) from iFractals?
No. iFractals is fixed at two bars on each side. For other widths write the loop shown above; for 2/2 the iFractals lower buffer gives the same values on the pivot bar.
How do I get the most recent swing low value in MQL5?
Scan the Confirmed buffer backwards from the current bar for the first non-EMPTY_VALUE cell, which is the MQL5 form of ta.valuewhen(not na(pl), pl, 0), or maintain a buffer that carries the last confirmed low forward bar by bar.
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