hl2, hlc3, ohlc4 in MQL5: applied prices and the ohlc4 gap
Short answer
hl2 is PRICE_MEDIAN, hlc3 is PRICE_TYPICAL and hlcc4 is PRICE_WEIGHTED in MQL5's ENUM_APPLIED_PRICE. ohlc4 has no applied-price constant, so compute (open + high + low + close) / 4 from the four arrays.
Pine's hl2, hlc3, ohlc4 and hlcc4 are built-in price series: the median, typical, average and weighted-close prices of each bar. MQL5 offers the same formulas as ENUM_APPLIED_PRICE constants for its native indicators: hl2 is PRICE_MEDIAN, hlc3 is PRICE_TYPICAL, hlcc4 is PRICE_WEIGHTED. ohlc4 has no applied-price constant and must be computed from the four arrays.
What the price sources compute
hl2 = (high + low) / 2hlc3 = (high + low + close) / 3ohlc4 = (open + high + low + close) / 4hlcc4 = (high + low + close + close) / 4
They are plain series, so they take the history operator (hlc3[1]), can be passed to any ta.* function and are the usual choices for input.source. None of them is na on any bar. A common naming trap: MetaTrader calls hlcc4 the "weighted close", while Pine users sometimes assume the weighted price is ohlc4. It is not; the two differ whenever open and close differ.
The MQL5 equivalent
There are two routes. Passing the applied price to a native indicator handle (iMA(_Symbol, _Period, 20, 0, MODE_SMA, PRICE_MEDIAN)) is the shortest translation of ta.sma(hl2, 20). Computing the source per bar from the OnCalculate arrays works for any formula, including ohlc4, and for any calculation that is not a native indicator. The example shows both on one chart; after the warm-up the hand-made sma(hl2) and the PRICE_MEDIAN handle coincide to the last digit.
//@version=6
indicator("price sources demo", overlay=true)
int length = input.int(20, "Length")
plot(ohlc4, "ohlc4")
plot(ta.sma(hl2, length), "sma(hl2)")#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_label1 "ohlc4"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrOrangeRed
#property indicator_label2 "sma(hl2) by hand"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrGray
#property indicator_label3 "iMA PRICE_MEDIAN"
input int Length = 20;
double Ohlc4Buf[];
double SmaHandBuf[];
double SmaNativeBuf[];
int maHandle = INVALID_HANDLE;
// Pine's price sources, one bar at a time
double Hl2(const double &h[], const double &l[], const int i) { return (h[i] + l[i]) / 2.0; }
double Hlc3(const double &h[], const double &l[], const double &c[], const int i) { return (h[i] + l[i] + c[i]) / 3.0; }
double Ohlc4(const double &o[], const double &h[], const double &l[], const double &c[], const int i)
{ return (o[i] + h[i] + l[i] + c[i]) / 4.0; }
double Hlcc4(const double &h[], const double &l[], const double &c[], const int i) { return (h[i] + l[i] + 2.0 * c[i]) / 4.0; }
int OnInit()
{
SetIndexBuffer(0, Ohlc4Buf, INDICATOR_DATA);
SetIndexBuffer(1, SmaHandBuf, INDICATOR_DATA);
SetIndexBuffer(2, SmaNativeBuf, INDICATOR_DATA);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
// ta.sma(hl2, Length) through the native MA: hl2 is PRICE_MEDIAN
maHandle = iMA(_Symbol, _Period, Length, 0, MODE_SMA, PRICE_MEDIAN);
return maHandle == INVALID_HANDLE ? INIT_FAILED : 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[])
{
if(BarsCalculated(maHandle) < rates_total) return 0;
int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
Ohlc4Buf[i] = Ohlc4(open, high, low, close, i); // no applied price for this one
if(i < Length - 1) { SmaHandBuf[i] = EMPTY_VALUE; continue; }
double sum = 0.0;
for(int k = i - Length + 1; k <= i; k++) sum += Hl2(high, low, k);
SmaHandBuf[i] = sum / Length;
}
// The native MA over the same bars; both lines coincide after warm-up
int toCopy = prev_calculated > 0 ? rates_total - prev_calculated + 1 : rates_total;
if(CopyBuffer(maHandle, 0, 0, toCopy, SmaNativeBuf) < 0) return 0;
return rates_total;
}The Hlc3 and Hlcc4 helpers are included for completeness; swap them into the sum to translate ta.sma(hlc3, n) or ta.sma(hlcc4, n). For ohlc4 the hand route is the only one.
Where the two differ
| Pine | MQL5 applied price | Formula | Note |
|---|---|---|---|
open, high, low, close | PRICE_OPEN, PRICE_HIGH, PRICE_LOW, PRICE_CLOSE | as named | exact |
hl2 | PRICE_MEDIAN | (h + l) / 2 | exact |
hlc3 | PRICE_TYPICAL | (h + l + c) / 3 | exact |
hlcc4 | PRICE_WEIGHTED | (h + l + 2c) / 4 | exact; MetaTrader calls it "weighted close" |
ohlc4 | none | (o + h + l + c) / 4 | compute per bar; no native handle can take it |
input.source with a user series | none | any | MQL5 inputs cannot hold a series; see input.source |
Precision is the one place the routes can disagree: a native handle computes in double on the same bar data, so the differences are at the 1e-12 level and vanish after NormalizeDouble. The tick-volume and real-volume arrays are not price sources and have no applied-price constant either.
What the Stratlate converter emits
A bare hl2, hlc3, ohlc4 or hlcc4 is emitted inline as the arithmetic on the current bar's arrays. When one of them feeds a ta.* function the engine materialises it as a real per-bar series (the example produced double __hl2[], filled each bar and passed to pine_sma) rather than switching to an iMA handle, so ohlc4 needs no special case. The applied-price table above is used by the engine only for request.security sources, where a higher-timeframe read goes through a native handle and ohlc4 falls back to the runtime's own price helper. All four sources are exact and carry no note.
Pitfalls
- Mapping
ohlc4toPRICE_WEIGHTED: the two differ by (c - o) / 4 on every bar. Symptom: the converted moving average sits above or below the original on trending days. - Computing the source on the newest bar only: a per-bar source must be written for every bar from
prev_calculated - 1, or the window function reads stale values on history. - Integer division:
(high[i] + low[i]) / 2is fine for doubles, but a helper that takesintparameters by mistake truncates. Symptom: a staircase plot at whole-point levels. - Using an
iMAhandle before it has data:CopyBufferreturns -1 on the first ticks after attaching. CheckBarsCalculated(handle)and return 0 so the terminal calls again. - Expecting
PRICE_MEDIANto mean the median of several bars: in MetaTrader it is the single-bar (h + l) / 2, exactly Pine'shl2.
FAQ
Which MQL5 applied price is hlc3?
PRICE_TYPICAL, defined as (high + low + close) / 3, the same formula Pine uses for hlc3. PRICE_MEDIAN is hl2 and PRICE_WEIGHTED is hlcc4.
Is there a PRICE_ constant for ohlc4?
No. ENUM_APPLIED_PRICE has no (open + high + low + close) / 4 member, so a native indicator cannot be fed ohlc4 directly. Compute it per bar from the OnCalculate arrays and run the calculation by hand, or feed the computed series to a custom indicator through iCustom.
Does PRICE_WEIGHTED equal Pine's ohlc4?
No. PRICE_WEIGHTED is (high + low + 2 * close) / 4, which is Pine's hlcc4. It equals ohlc4 only on bars where open equals close.
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