request.security in MQL5: iBarShift, iClose and lookahead
Short answer
There is no single MQL5 equivalent. For each chart bar, find the other timeframe's bar with iBarShift(symbol, period, time[i]), then read a price with iClose or iHigh, or an indicator value with CopyBuffer from a handle created in OnInit.
request.security evaluates an expression on another timeframe or symbol and returns its value aligned to the chart's bars. MQL5 has no single equivalent; the translation is two steps per chart bar: find the bar of the other timeframe that the chart bar belongs to with iBarShift(symbol, period, time[i]), then read a price with iClose/iHigh/… or an indicator value with CopyBuffer from a handle created in OnInit. This page is the short version; the full treatment, including repainting and session alignment, is in request.security and multi-timeframe logic in MQL5.
What request.security computes
Pine runs the expression on the requested timeframe's own bars, producing a series there, and then maps each chart bar to one value of that series. Which value depends on lookahead:
barmerge.lookahead_off(the default): a chart bar sees the higher-timeframe value only once that bar has closed. On a live chart the realtime value updates tick by tick on the forming higher-timeframe bar.barmerge.lookahead_on: on history every chart bar sees the value of the higher-timeframe bar that contains it, including its final close, which is future information. Combined with[1](request.security(sym, "D", high[1], lookahead=barmerge.lookahead_on)) it is the standard non-repainting way to read the previous day.
The gaps argument decides whether the value is repeated on every chart bar (barmerge.gaps_off, default) or appears only on the bar where it changes. The expression can be anything, including user functions, which run in the context of the other timeframe.
The MQL5 equivalent
The example reads the hourly close with lookahead off and the previous day's high with the lookahead/[1] idiom on any lower chart timeframe. Both reduce to iBarShift(...) + 1, the last completed bar of the other timeframe.
//@version=6
indicator("security demo", overlay=true)
float h1Close = request.security(syminfo.tickerid, "60", close)
float prevDayHigh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
plot(h1Close, "H1 close")
plot(prevDayHigh, "previous day high", color=color.orange)#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_label1 "H1 close"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrOrangeRed
#property indicator_label2 "previous day high"
double H1CloseBuf[];
double PrevDayHighBuf[];
int OnInit()
{
SetIndexBuffer(0, H1CloseBuf, INDICATOR_DATA);
SetIndexBuffer(1, PrevDayHighBuf, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(1, 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[])
{
// The higher timeframes must be loaded before they can be read.
if(Bars(_Symbol, PERIOD_H1) <= 0 || Bars(_Symbol, PERIOD_D1) <= 0) return 0;
int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
// Pine: request.security(syminfo.tickerid, "60", close)
// lookahead off: the last COMPLETED H1 bar, i.e. the bar before the
// one containing this chart bar. Using the containing bar (no + 1)
// shows an H1 close before that hour has ended on history.
int h1 = iBarShift(_Symbol, PERIOD_H1, time[i]);
H1CloseBuf[i] = h1 < 0 ? EMPTY_VALUE : iClose(_Symbol, PERIOD_H1, h1 + 1);
// Pine: request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
// The previous completed daily bar: containing day + 1. Known at the
// open of the day, so it does not repaint.
int d = iBarShift(_Symbol, PERIOD_D1, time[i]) + 1;
PrevDayHighBuf[i] = d < 1 ? EMPTY_VALUE : iHigh(_Symbol, PERIOD_D1, d);
}
return rates_total;
}For an indicator on the other timeframe, create the handle once in OnInit (iMA(_Symbol, PERIOD_H4, 20, 0, MODE_EMA, PRICE_CLOSE)) and read one value per chart bar with CopyBuffer(handle, 0, shift, 1, tmp), where shift is the aligned index. A user-defined expression has no native handle; compute it on CopyRates data of the other timeframe, or run the indicator itself on that timeframe with iCustom.
Where the two differ
| Aspect | Pine | MQL5 |
|---|---|---|
| Alignment | automatic | iBarShift(sym, tf, time[i]) per chart bar |
| lookahead_off | last closed higher-timeframe bar | aligned shift + 1 |
| lookahead_on | containing bar (future leak unless combined with [1]) | aligned shift as is |
| Day boundary | exchange session | broker server day; daily bars can start at a different hour |
| Data availability | loaded before the script runs | may not be loaded on the first call; return 0 and wait |
| Arbitrary expression | any Pine code | native handle, hand computation on CopyRates, or iCustom |
| Timeframes | any, e.g. "45", "3D" | only ENUM_TIMEFRAMES values |
What the Stratlate converter emits
For a bare price the engine emits pine_htf_price(sym, period, kind, iBarShift(sym, period, time[i]) + shift + 1) with lookahead off and without the + 1 with lookahead on; ta.sma/ta.wma of a price are rebuilt inline from the other timeframe's bars, a single native indicator such as ta.ema or ta.rsi goes through a handle created in OnInit and read with CopyBuffer, and a script's own value on another timeframe runs the converted indicator there through iCustom. Every higher-timeframe read is reported as an approximation (PM292) because TradingView aligns to the exchange session and MetaTrader to the broker's server day; lookahead_on adds an informational note (PM291). Expressions outside these shapes are reported as unconverted.
Pitfalls
- Reading
iClose(sym, PERIOD_D1, 1)withoutiBarShift: that is yesterday relative to now, the same number on every bar. Symptom: a flat line across the whole history. - Forgetting the
+ 1for lookahead off: the history shows each hour's close from its first minute. Symptom: a backtest that looks far better than live trading. - Not waiting for data: on the first call after attaching, the other timeframe may have no bars and
iBarShiftreturns -1. Return 0 fromOnCalculateso the terminal calls again. - Daily bars that start at a different time: a broker on GMT+2 or GMT+3 server time builds days that differ from TradingView's. Symptom: "previous day high" levels that disagree by a few points on some days.
- Requesting a timeframe MT5 does not have: "45" or "3D" have no
ENUM_TIMEFRAMES; build them from M15 or D1 bars by hand.
FAQ
What is the MQL5 equivalent of request.security?
There is no single function. For prices, iBarShift plus iClose/iHigh/iLow/iOpen on the other period; for indicators, a handle created with iMA, iRSI and the like on that period and read with CopyBuffer at the aligned shift; for anything else, CopyRates and your own calculation, or iCustom.
How do I avoid repainting when converting request.security?
Read the last completed bar of the other timeframe: aligned shift + 1. That matches Pine's default lookahead_off in realtime and never shows a value before the higher-timeframe bar has closed.
Why do my daily levels differ from TradingView's?
Daily bars in MetaTrader follow the broker's server day, while TradingView follows the exchange session. Highs and lows of bars that straddle the different start times differ; check against the broker's own D1 chart rather than TradingView's.
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