ta.percentrank in MQL5: share of previous values at or below
Short answer
MQL5 has no counterpart. Count the previous length values that are less than or equal to the current one: 100 * count / len, with an empty value until the window is full.
ta.percentrank(source, length) tells you where the current value sits relative to its own recent history: the percentage of the previous length values that are less than or equal to the current value. 100 means the current value is at or above everything in the window, 0 means it is below everything. MQL5 has no counterpart; the equivalent is a counting loop over the length cells before the current one.
What ta.percentrank computes
Count how many of source[1] through source[length] are <= source[0], divide by length, multiply by 100. Two details are easy to get wrong. The sample is the previous length values; the current bar is the reference and is not counted, unlike ta.highest, whose window includes it. And the comparison is less than or equal, so a value that exactly repeats a recent value ranks above it. Consequently the result needs length earlier bars and is na on the first length bars of the chart, one more than a windowed average of the same length. The possible outputs are the multiples of 100 / length; with length = 20 that is steps of 5.
The function is a quick normaliser: ta.percentrank(volume, 50) flags unusual volume regardless of symbol, and ta.percentrank(ta.atr(14), 100) is a common volatility-regime filter.
The MQL5 equivalent
Pine: percent rank of the close over 20 bars.
//@version=6
indicator("Percent rank")
len = input.int(20, "Length")
pr = ta.percentrank(close, len)
plot(pr, "Percent rank")
hline(50)MQL5. PercentRankAt is the whole function; note the guard i < len rather than i < len - 1, and the loop starting at k = 1.
//+------------------------------------------------------------------+
//| Percent rank of close: Pine's ta.percentrank(close, len) in MQL5 |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "percentrank"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 50
input int Length = 20;
double Rank[];
// ta.percentrank(src, len): the share (0..100) of the len PREVIOUS values
// that are less than or equal to the current one. The current bar is the
// reference, not part of the sample, so len prior bars are needed.
double PercentRankAt(const double &src[], const int i, const int len)
{
if(len <= 0 || i < len) return EMPTY_VALUE;
int count = 0;
for(int k = 1; k <= len; k++)
if(src[i - k] <= src[i]) count++;
return 100.0 * count / len;
}
int OnInit()
{
SetIndexBuffer(0, Rank, 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++)
Rank[i] = PercentRankAt(close, i, Length);
return rates_total;
}Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Sample | Previous length values, current excluded | Loop k = 1..len over src[i - k] on oldest-first arrays |
| Comparison | <= | Keep <=; < changes the value on every repeated price |
| First defined bar | Bar length (0-based) | Guard i < len, write EMPTY_VALUE |
| na in the window | Result na | An EMPTY_VALUE cell is a huge number and simply fails <=, lowering the rank silently; test for it if the source can be empty |
| Output | 0 to 100 in steps of 100/length | Same; use indicator_minimum 0 and indicator_maximum 100 for a fixed scale |
What the Stratlate converter emits
The emitter treats ta.percentrank as a windowed builtin and emits pine_percentrank(src, bar, len); the example converted to pine_percentrank(close, __pineBar, (int)(len)). The runtime returns na while i < len or the current value is na, then counts src[i - k] <= cur for k = 1..len and returns 100.0 * below / len, so the exclusion of the current bar and the less-or-equal comparison are both preserved. A computed source is written into a per-script array each bar before the call: ta.percentrank(close - open, 20) converted to __ts0[__pineBar] = (close[__pineBar] - open[__pineBar]); p1 = pine_percentrank(__ts0, __pineBar, (int)(20));. The mapping is exact.
Pitfalls
- Including the current bar in the sample. Symptom: the rank never reaches 0, since the current value always counts as at or below itself, and every value is biased upward by
100/(len+1)-ish. - Guarding with
i < len - 1. Symptom: an array out of range read on the first evaluated bar, or one spurious value at the start. - Using
<instead of<=. Symptom: lower ranks than TradingView wherever the price repeats a recent level, which on instruments with few decimals is frequent. - Dividing integers. Symptom:
100 * count / lenwith anintcount andintlength is fine, butcount / len * 100truncates to 0 or 100. - Feeding an unseeded upstream buffer. Symptom: a rank that is too low by exactly one step per empty cell during warm-up.
FAQ
Does ta.percentrank include the current bar in the window?
No. It compares the current value with the previous length values, so the current bar is the reference, not part of the sample, and the result needs length earlier bars.
Why are the values in steps of 5 with length 20?
Because the result is a count out of length, scaled to 100. Twenty values give 21 possible ranks: 0, 5, 10, up to 100.
What does the converter do with ta.percentrank on an expression?
It writes the expression's value into an array on every bar and runs the same pine_percentrank helper over that array, so the window holds exactly the values Pine would see.
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