Custom MT4 indicator

Build an MQL4 VWAP indicator with explicit volume assumptions

MQL4 has no universal one-call VWAP function. A custom indicator can accumulate typical price times volume and divide by cumulative volume, but MT4’s standard Volume series contains tick volume. Label the result as tick-volume-weighted unless the broker supplies another verified volume source.

Open the Coding Agent, then choose MQL4 from the language menu.

DailyTickVWAP.mq4.mq4
#property strict
#property indicator_chart_window
#property indicator_buffers 1

double VwapBuffer[];

int OnInit()
  {
   SetIndexBuffer(0, VwapBuffer);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2, clrBlue);
   SetIndexLabel(0, "Daily Tick VWAP");
   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 cumulative_pv = 0.0;
   double cumulative_volume = 0.0;

   for(int i = rates_total - 1; i >= 0; i--)
     {
      bool new_day = (i == rates_total - 1);
      if(!new_day)
         new_day = (TimeDay(time[i]) != TimeDay(time[i + 1]) ||
                    TimeMonth(time[i]) != TimeMonth(time[i + 1]) ||
                    TimeYear(time[i]) != TimeYear(time[i + 1]));

      if(new_day)
        {
         cumulative_pv = 0.0;
         cumulative_volume = 0.0;
        }

      double bar_volume = (double)tick_volume[i];
      double typical_price = (high[i] + low[i] + close[i]) / 3.0;
      cumulative_pv += typical_price * bar_volume;
      cumulative_volume += bar_volume;

      VwapBuffer[i] = (cumulative_volume > 0.0)
                      ? cumulative_pv / cumulative_volume
                      : EMPTY_VALUE;
     }

   return(rates_total);
  }
  • Typical price is an explicit choice
  • MT4 Volume is tick volume
  • Session reset defines the anchor
  • Zero-volume bars need a guard

VWAP is a formula plus an anchor

The numerator accumulates price multiplied by volume. The denominator accumulates volume. The anchor decides when both totals reset. A daily indicator, a broker session, a week, and an event-anchored calculation can produce different lines from the same bars.

Typical price using high, low, and close is one common source, but close-only or another defined source is also possible. The page or indicator name should state that choice.

DecisionExample in this codeAlternative
Price source(high + low + close) / 3Close or another defined price
Weighttick_volumeVerified broker-provided real volume
AnchorCalendar day in server timestampsSession, week, or event
Zero denominatorEMPTY_VALUECarry-forward only if explicitly intended

Tick volume is not exchange trade volume

The official MQL4 predefined Volume array contains tick volumes for bars on the current chart. That is a count of price updates, not a guaranteed count of contracts or shares traded on a centralized venue.

Broker feeds and server sessions can differ. A VWAP-style line from one MT4 feed should not be presented as identical to exchange VWAP without evidence about the source.

  • Name the indicator Daily Tick VWAP or disclose the weight source
  • Use server timestamps when defining daily boundaries
  • Test Sunday candles and broker session gaps
  • Recalculate after deeper history loads or chart changes

Verify the plotted line before using it as a signal

Compare several bars with a hand calculation and check where the daily reset occurs. Reload the chart and change timeframe to confirm the line rebuilds consistently from available data.

A price crossing this line is not automatically a profitable rule. Define confirmation, bar timing, costs, stops, and invalidation before testing any strategy interpretation.

Review the complete example

The indicator walks from the oldest loaded bar toward the current bar, resets cumulative values at each calendar-day boundary, uses typical price, weights by tick_volume, and avoids division by zero.

This is a tick-volume-weighted reference indicator, not an exchange-volume claim or a trading recommendation. Verify feed, session, and history behavior in the target MT4 installation.

DailyTickVWAP.mq4.mq4
#property strict
#property indicator_chart_window
#property indicator_buffers 1

double VwapBuffer[];

int OnInit()
  {
   SetIndexBuffer(0, VwapBuffer);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2, clrBlue);
   SetIndexLabel(0, "Daily Tick VWAP");
   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 cumulative_pv = 0.0;
   double cumulative_volume = 0.0;

   for(int i = rates_total - 1; i >= 0; i--)
     {
      bool new_day = (i == rates_total - 1);
      if(!new_day)
         new_day = (TimeDay(time[i]) != TimeDay(time[i + 1]) ||
                    TimeMonth(time[i]) != TimeMonth(time[i + 1]) ||
                    TimeYear(time[i]) != TimeYear(time[i + 1]));

      if(new_day)
        {
         cumulative_pv = 0.0;
         cumulative_volume = 0.0;
        }

      double bar_volume = (double)tick_volume[i];
      double typical_price = (high[i] + low[i] + close[i]) / 3.0;
      cumulative_pv += typical_price * bar_volume;
      cumulative_volume += bar_volume;

      VwapBuffer[i] = (cumulative_volume > 0.0)
                      ? cumulative_pv / cumulative_volume
                      : EMPTY_VALUE;
     }

   return(rates_total);
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Choose price and volume sources

    Document the price formula and whether the weight is tick or verified real volume.

  2. 2

    Define the anchor

    Select calendar day, broker session, week, or another reset event.

  3. 3

    Accumulate with guards

    Reset both totals together and avoid division when cumulative volume is zero.

  4. 4

    Compare plotted values

    Check hand-calculated bars, session boundaries, reloads, and timeframe changes.

Frequently asked questions

Does MQL4 have a built-in VWAP function?

MQL4 does not provide a universal one-call VWAP function in its standard technical indicator list. A custom calculation must define price, volume, and anchor.

What volume does MT4 use for VWAP?

The standard MQL4 Volume series contains tick volume. Do not label it exchange trade volume without another verified source.

When should an MQL4 VWAP reset?

The reset depends on the intended anchor. This example resets by calendar day using server-time bar timestamps.

Why does VWAP differ between MT4 brokers?

Tick feeds, server sessions, history, symbols, and available volume sources can differ between brokers.

Can a VWAP crossover guarantee a trade outcome?

No. The line is a calculation. A trading rule still needs defined timing, execution assumptions, risk controls, and validation.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent