input.source in MQL5: ENUM_APPLIED_PRICE and Apply to
Short answer
Use an input ENUM_APPLIED_PRICE for a drop-down of standard prices, passed to a built-in like iMA or computed per bar. For Pine's ability to take another indicator as the source, use the short form of OnCalculate with a single price[] array, which adds MetaTrader's Apply to tab.
input.source() lets the user choose which series an indicator runs on: close, open, hl2, hlc3, ohlc4, or the output of another indicator on the chart. MQL5 offers two equivalents. An input ENUM_APPLIED_PRICE gives a drop-down of the standard prices, which you pass to a built-in such as iMA(..., InpSource) or compute yourself per bar. And the short form of OnCalculate, the one that receives a single price[] array, makes MetaTrader add an "Apply to" tab where the user can pick a price or another indicator's data, which is the closest match to Pine's ability to feed one indicator into another.
What input.source() computes
input.source(defval, title, tooltip, inline, group, display, confirm, active) returns a series float. defval is one of the built-in price series (open, high, low, close, hl2, hlc3, ohlc4, hlcc4); in the dialog the user can choose any of those or any plot of another indicator on the same chart, which makes the result a computed series. Pine's compound prices are hl2 = (high + low) / 2, hlc3 = (high + low + close) / 3, ohlc4 = (open + high + low + close) / 4 and hlcc4 = (high + low + 2 * close) / 4. Because the result is a series, it can be indexed with src[1] and passed to any ta.* function.
The MQL5 equivalent
The Pine version takes an EMA of a user-chosen source, hlc3 by default.
//@version=6
indicator("Source demo", overlay=true)
src = input.source(hlc3, "Source")
len = input.int(20, "Length")
plot(ta.ema(src, len), "EMA")The first MQL5 version declares an ENUM_APPLIED_PRICE input and computes the selected price per bar into a calculation buffer, so the result can be indexed like Pine's src. Where the consumer is a built-in indicator, you can skip the helper: iMA(_Symbol, _Period, InpLength, 0, MODE_EMA, InpSource) takes the same enum directly.
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_label1 "EMA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
input ENUM_APPLIED_PRICE InpSource = PRICE_TYPICAL; // Source (hlc3)
input int InpLength = 20; // Length
double BufEma[], BufSrc[];
// The selected price for bar i, with Pine's definitions of the compound prices.
// Note: MetaTrader's own PRICE_WEIGHTED is (H + L + 2C) / 4, Pine's hlcc4.
double Price(const ENUM_APPLIED_PRICE ap, const double &o[], const double &h[],
const double &l[], const double &c[], const int i)
{
switch(ap)
{
case PRICE_OPEN: return o[i];
case PRICE_HIGH: return h[i];
case PRICE_LOW: return l[i];
case PRICE_MEDIAN: return (h[i] + l[i]) / 2.0; // hl2
case PRICE_TYPICAL: return (h[i] + l[i] + c[i]) / 3.0; // hlc3
case PRICE_WEIGHTED: return (h[i] + l[i] + 2.0 * c[i]) / 4.0; // hlcc4
default: return c[i];
}
}
int OnInit()
{
SetIndexBuffer(0, BufEma, INDICATOR_DATA);
SetIndexBuffer(1, BufSrc, INDICATOR_CALCULATIONS); // the source series, not drawn
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[])
{
double alpha = 2.0 / (InpLength + 1);
int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
BufSrc[i] = Price(InpSource, open, high, low, close, i);
// ta.ema seeds with the first value, then smooths
BufEma[i] = i == 0 ? BufSrc[i] : alpha * BufSrc[i] + (1.0 - alpha) * BufEma[i - 1];
}
return rates_total;
}The second version uses the short form of OnCalculate. There is no price input in the code at all: MetaTrader adds an "Apply to" tab with Close, Open, High, Low, Median, Typical, Weighted, "Previous Indicator's Data" and "First Indicator's Data". The begin parameter tells you how many leading bars of the source are empty, which matters when the source is another indicator with its own warm-up.
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "EMA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrOrange
input int InpLength = 20; // Length
double BufEma[];
int OnInit()
{
SetIndexBuffer(0, BufEma, INDICATOR_DATA);
return INIT_SUCCEEDED;
}
// The short form: the terminal passes the series chosen on the "Apply to" tab --
// a price, or "Previous Indicator's Data" / "First Indicator's Data".
int OnCalculate(const int rates_total, const int prev_calculated,
const int begin, const double &price[])
{
double alpha = 2.0 / (InpLength + 1);
int first = prev_calculated > 0 ? prev_calculated - 1 : begin;
if(prev_calculated == 0)
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, begin); // the source's own warm-up
for(int i = first; i < rates_total; i++)
BufEma[i] = i == begin ? price[i] : alpha * price[i] + (1.0 - alpha) * BufEma[i - 1];
return rates_total;
}| Pine source | ENUM_APPLIED_PRICE | Same formula? |
|---|---|---|
close, open, high, low | PRICE_CLOSE, PRICE_OPEN, PRICE_HIGH, PRICE_LOW | Yes |
hl2 | PRICE_MEDIAN | Yes |
hlc3 | PRICE_TYPICAL | Yes |
hlcc4 | PRICE_WEIGHTED | Yes, (H + L + 2C) / 4 |
ohlc4 | none | Compute it yourself |
| Another indicator's plot | none | "Apply to" with the short-form OnCalculate, or iCustom + CopyBuffer |
Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Value | A series you index and pass around | An enum; you turn it into prices yourself, or hand it to a built-in |
| ohlc4 | Built in | No enum member; PRICE_WEIGHTED is hlcc4 |
| Other indicators as source | Any plot on the chart, picked in the dialog | Short-form OnCalculate "Apply to", limited to the previous or first indicator in the window |
| Mixing a source with other OHLC data | Always possible | The short form receives only price[]; use the full form and an enum if you also need high and low |
| Warm-up of a computed source | Leading na values | begin parameter of the short form |
What the Stratlate converter emits
An input.source whose default is close, open, high, low, hl2, hlc3 or ohlc4 becomes a real input ENUM_APPLIED_PRICE, and every read of the variable goes through the runtime helper pine_source(src, open, high, low, close, i) into a per-bar series. The engine maps ohlc4 to PRICE_WEIGHTED and its helper computes PRICE_WEIGHTED as (O + H + L + C) / 4, so in converted indicators "Weighted" in the drop-down means Pine's ohlc4, not MetaTrader's native hlcc4; that is consistent inside the converted file, but differs from what the same choice gives a built-in like iMA. A default of hlcc4 or any other expression cannot be an enum input: it is emitted as a global recomputed each bar with the same value and reported as an approximation (PM220, PM222) because the user can no longer change it in the Inputs tab. Choosing another indicator's plot as the source has no converted equivalent. Compile the result and check the source drop-down before comparing values with TradingView.
Pitfalls
- Mapping
ohlc4toPRICE_WEIGHTEDin a hand-writteniMAcall. MetaTrader computes (H + L + 2C) / 4, so the line is close to TradingView but never equal. - Using the short-form
OnCalculateand then needinghighandlow. The short form does not receive them; switch to the full form with an enum input. - Ignoring
beginwhen applying to another indicator. The first values are empty, the EMA seeds fromEMPTY_VALUE, and the whole line is pinned at a huge number. - Applying a converted indicator to "Previous Indicator's Data" when the previous indicator sits in another window. Apply to only sees indicators in the same window, dropped onto it.
- Forgetting that the enum is not a price. Comparing
InpSourcewith a number or passing it where adoubleis expected compiles (an enum converts to int) and silently uses a small integer (1 to 7) as the price.
FAQ
How do I feed one converted indicator into another?
Either write the consumer with the short-form OnCalculate and drop it onto the first indicator's window with "Previous Indicator's Data", or read the first indicator inside the second with iCustom and CopyBuffer. The second way is explicit and also works in an EA.
Is PRICE_TYPICAL the same as hlc3?
Yes, (high + low + close) / 3 on both platforms, and PRICE_MEDIAN is hl2. The only mismatch among the compound prices is ohlc4, which MetaTrader does not offer.
Can the Strategy Tester optimise the source?
Yes. An ENUM_APPLIED_PRICE input is an enum, so the tester can step through its members like any other enum input. The Apply to choice of a short-form indicator is not an optimisable input.
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