ta.vwap in MQL5: session VWAP without a built-in iVWAP
Short answer
MetaTrader 5 has no VWAP. Keep two running sums, source × volume and volume, and reset them at each session start. The reset is the hard part: TradingView resets at the symbol's session, while MetaTrader only knows the broker's server time.
ta.vwap(source) is the session volume-weighted average price: the sum of source * volume since the session began, divided by the sum of volume over the same bars. MetaTrader 5 has no iVWAP function and no VWAP among its standard indicators, so the MQL5 equivalent is two running sums and a reset rule. The arithmetic is trivial; the reset is not, because TradingView resets at the symbol's session start and MetaTrader only knows the broker's server time.
What ta.vwap computes
On the first bar of each session both sums restart; on every other bar they add source * volume and volume. The VWAP is their ratio, so it is defined from the first bar of the session, where it equals that bar's source. The default source in TradingView's VWAP indicator is hlc3. TradingView decides "first bar of the session" from the symbol's own session calendar: for a US stock that is the 09:30 New York open, for many futures the evening open, for crypto 00:00 UTC. Pine v5 and v6 also offer ta.vwap(source, anchor) with an explicit anchor condition and a form with a standard-deviation multiplier that returns the VWAP together with bands. On the realtime bar both sums include the forming bar's latest price and volume, so the last value moves each tick.
The MQL5 equivalent
//@version=6
indicator("VWAP ref", overlay=true)
plot(ta.vwap(hlc3), "VWAP")The MQL5 indicator below makes the session boundary an input in server time. SessionId shifts each bar's timestamp back by the reset time and divides by one day, so all bars of one trading day share an id and the sums restart when it changes. Running sums live in calculation buffers so the forming bar recomputes from the previous bar's totals.
//+------------------------------------------------------------------+
//| Session VWAP like ta.vwap(hlc3), reset at a chosen server time |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 1
#property indicator_label1 "VWAP"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
input int InpResetHour = 0; // Session start, server time: hour
input int InpResetMinute = 0; // Session start, server time: minute
double VwapBuf[], PvBuf[], VBuf[];
int OnInit()
{
SetIndexBuffer(0, VwapBuf, INDICATOR_DATA);
SetIndexBuffer(1, PvBuf, INDICATOR_CALCULATIONS);
SetIndexBuffer(2, VBuf, INDICATOR_CALCULATIONS);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
return INIT_SUCCEEDED;
}
// The trading day a bar belongs to, with the day starting at the reset time.
long SessionId(const datetime t)
{
return ((long)t - (InpResetHour * 3600 + InpResetMinute * 60)) / 86400;
}
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[])
{
const bool realVol = rates_total > 0 && volume[rates_total - 1] > 0;
int start = prev_calculated > 1 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
const double src = (high[i] + low[i] + close[i]) / 3.0; // hlc3
const double v = (double)(realVol ? volume[i] : tick_volume[i]);
const bool newSession = i == 0 || SessionId(time[i]) != SessionId(time[i - 1]);
PvBuf[i] = (newSession ? 0.0 : PvBuf[i - 1]) + src * v;
VBuf[i] = (newSession ? 0.0 : VBuf[i - 1]) + v;
VwapBuf[i] = VBuf[i] == 0.0 ? EMPTY_VALUE : PvBuf[i] / VBuf[i];
}
return rates_total;
}For a forex or crypto symbol whose broker day rolls at the same moment as TradingView's, leave the reset at 00:00. For an exchange-traded symbol, set the reset to the exchange open expressed in your broker's server time; that offset changes when either side switches daylight saving time, so check it twice a year.
Where the two differ
| Aspect | Pine ta.vwap | MQL5 |
|---|---|---|
| Built-in | Yes | No iVWAP; write it or use a custom indicator |
| Session start | Symbol's session calendar | Whatever your reset rule says; usually server midnight |
| Volume | Feed volume | Real volume if the broker supplies it, otherwise tick volume |
| Anchors | Session by default; anchor argument for week, month, earnings, etc. | Any rule you code against the bar time |
| Bands | Built-in stdev form returns upper and lower bands | Accumulate sum(src² * volume) as well and compute them yourself |
| Daily and higher charts | Each bar is its own session | Same with a daily reset; each bar's VWAP equals its source |
With the right reset and the same volume, the two agree from the first bar of each session, because there is no smoothing or seed. A VWAP that is right in shape but resets an hour early or late is almost always the session boundary; a VWAP that sits slightly off all day is usually the volume series.
What the Stratlate converter emits
The converter supports the single-argument call, ta.vwap(source); write ta.vwap(hlc3) rather than the bare ta.vwap variable, which is currently reported as unsupported. It emits its runtime helper pine_vwap(src, volume, time, pv, vv, bar), which keeps the two running sums in per-call-site arrays and restarts them when the bar's server date changes, that is at the broker's server midnight. Because that is not TradingView's session calendar, the report adds an approximation note (PM290) with a suggested session-anchored reset, and the test script above is graded APPROXIMATED for exactly that reason. Real volume is used when the broker supplies it, tick volume otherwise. The anchored form ta.vwap(source, anchor), the form with a standard-deviation multiplier and the bare variable are reported as unsupported, and the file is graded PARTIAL. Compile the output, check the reset time against TradingView on your symbol, and backtest it.
Pitfalls
- Resetting on
time[i] / 86400for a stock or index CFD. The VWAP restarts at server midnight, hours before the exchange opens, and the overnight bars pull it away from TradingView's. - Assuming a fixed hour offset between server time and the exchange. Broker servers and exchanges switch daylight saving time on different dates; for a few weeks a year the reset is off by an hour.
- Using a
staticsum and adding the forming bar on every tick. The sums count the same bar many times and the VWAP drifts toward the current price. - Using real volume on a forex symbol where
volume[]is all zeros. The denominator is 0 and nothing is drawn. - Searching for
iVWAPor a VWAP handle. There is none in MQL5; code that calls it does not compile.
FAQ
Does MetaTrader 5 have a built-in VWAP?
No. There is no iVWAP function and no VWAP among the standard indicators. Compute it with two running sums as on this page, or install a custom VWAP indicator and read it with iCustom.
Why does my MT5 VWAP reset at a different time than TradingView's?
TradingView resets at the symbol's session start in the exchange's time zone, while an MQL5 reset is written against the broker's server time. Set the reset to the session open converted to server time, and recheck it when daylight saving time changes on either side.
Can the converter translate ta.vwap with an anchor or with bands?
Not currently. The single-argument call ta.vwap(source) is converted with a server-midnight reset and reported as an approximation; the anchor argument, the standard-deviation form and the bare ta.vwap variable are reported as unsupported, and you write those parts by hand from the code on this page.
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