VWAP in MetaTrader 5 — MQL5 Source Code

A session VWAP that resets each day for MT5

VWAP in MetaTrader 5
VWAP in MetaTrader 5

MetaTrader doesn’t ship a session VWAP by default, so this is a genuinely useful one to build. The key mechanic is resetting the accumulation at each new trading day. See the explainer for the concept.

What you’ll need

  • MetaTrader 5 with MetaEditor
  • Save as AlgoGen.VWAP.mq5 under MQL5/Indicators/

The source

//+------------------------------------------------------------------+
//|                                                 AlgoGen.VWAP.mq5 |
//|                                          Copyright 2026, AlgoGen |
//|                                           https://www.algogen.io |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, AlgoGen"
#property link      "https://www.algogen.io"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

#property indicator_label1  "VWAP"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrOrangeRed
#property indicator_width1  2

input bool InpUseRealVolume = true; // Real volume (else tick volume)

double VWAPBuffer[];

int OnInit()
  {
   SetIndexBuffer(0, VWAPBuffer, INDICATOR_DATA);
   IndicatorSetString(INDICATOR_SHORTNAME, "AlgoGen VWAP (session)");
   return(INIT_SUCCEEDED);
  }

// Return the calendar day (midnight) for a bar time.
datetime DayOf(datetime t)
  {
   return(t - (t % 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[])
  {
   int start = (prev_calculated > 0) ? prev_calculated - 1 : 0;

   double cumPV = 0.0, cumV = 0.0;
   datetime curDay = 0;

   // Rebuild the running sums from the start of the current bar's day.
   for(int i = start; i < rates_total; i++)
     {
      datetime d = DayOf(time[i]);
      if(d != curDay)                       // new session -> reset
        {
         curDay = d; cumPV = 0.0; cumV = 0.0;
        }
      double typical = (high[i] + low[i] + close[i]) / 3.0;
      double vol = InpUseRealVolume ? (double)volume[i] : (double)tick_volume[i];
      cumPV += typical * vol;
      cumV  += vol;
      VWAPBuffer[i] = (cumV > 0.0) ? cumPV / cumV : typical;
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

Install & compile

  1. Drop it in MQL5/Indicators/, press F7.
  2. Apply to an intraday chart — the VWAP overlays price and resets each day (you’ll see the characteristic new line each session).

Gotchas

  • The daily reset. DayOf strips the time-of-day so a change of calendar day triggers a reset of the cumulative sums. Without a reset, you’d get an anchored-from-forever line, not a session VWAP.
  • Incremental recompute caveat. Because this recomputes cumulative sums, the loop must restart from the beginning of the current session; for production, cache per-session sums rather than the simplified full-rebuild shown here.
  • Volume source. Real volume is ideal; on many FX feeds you only have tick volume, which makes VWAP a rough proxy. Set InpUseRealVolume accordingly.

Same indicator elsewhere: Python, Pine Script, EasyLanguage, NinjaScript. Then test a VWAP rule in AlgoGen.


This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.

Sources and further reading

  1. The Total Cost of Transactions on the NYSE (The Journal of Finance)
  2. Technical indicator functions (MetaQuotes)

Historical research from the Algogen archive. Not investment advice.

Blog

Have a strategy idea?

Describe it in plain English, preview where your rules fire, then decide whether the evidence is worth a backtest.

Keep me postedHow it works