Complete .mq5 source code

VWAP D1 MQL5: Rolling Daily Price–Volume Average

A D1-chart tutorial for a rolling volume-weighted average of daily typical prices. It uses an explicit window of D1 bars and either tick volume or reported real volume. This is a daily-bar VWAP approximation, not a transaction-level session VWAP, volume profile or estimate of fundamental fair value.

How the indicator signal works

Calculation and signal conditions

For each daily bar, take the latest LookbackDays D1 bars, compute each typical price as (high + low + close) / 3, and weight it by the selected volume field. The window removes old bars as it advances. Weighted population deviation gives one upper and lower band. A one-day window collapses to that bar's typical price with zero deviation; daily resetting on D1 cannot reconstruct intraday VWAP.

Interpretation and limitations

The indicator only plots values. It has no reversal-candle detector, entry orders, ATR stops, partial profits, two-bar trail or five-day time exit. Tick counts and reported trade volume are different units; the input selects one source for all bars, with no automatic mixing. Zero-volume bars receive no weight, and an all-zero window stays empty.

MQL5 Custom Indicator Code

#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_label1 "Weighted mean"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrOrangeRed
#property indicator_label2 "Upper weighted deviation"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrOrangeRed
#property indicator_label3 "Lower weighted deviation"
input double BandMultiplier=2;
input bool UseTickVolume=true; // Explicit source; no per-bar fallback or unit mixing.
input int LineWidth=2;
double meanLine[],upper[],lower[];
input int LookbackDays=20; // Rolling D1 bars, not calendar days or intraday executions.
int OnInit()
{
   if(_Period!=PERIOD_D1 || LookbackDays<1 || LookbackDays>500 || BandMultiplier<0 || LineWidth<1 || LineWidth>5)
      return INIT_PARAMETERS_INCORRECT;
   SetIndexBuffer(0,meanLine,INDICATOR_DATA); SetIndexBuffer(1,upper,INDICATOR_DATA); SetIndexBuffer(2,lower,INDICATOR_DATA);
   ArraySetAsSeries(meanLine,false); ArraySetAsSeries(upper,false); ArraySetAsSeries(lower,false);
   for(int p=0;p<3;p++) { PlotIndexSetDouble(p,PLOT_EMPTY_VALUE,EMPTY_VALUE); PlotIndexSetInteger(p,PLOT_DRAW_BEGIN,LookbackDays-1); PlotIndexSetInteger(p,PLOT_LINE_WIDTH,LineWidth); }
   IndicatorSetString(INDICATOR_SHORTNAME,"D1 rolling bar-volume weighted typical price");
   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[])
{
   ArraySetAsSeries(high,false); ArraySetAsSeries(low,false); ArraySetAsSeries(close,false);
   ArraySetAsSeries(tick_volume,false); ArraySetAsSeries(volume,false);
   ArrayInitialize(meanLine,EMPTY_VALUE); ArrayInitialize(upper,EMPTY_VALUE); ArrayInitialize(lower,EMPTY_VALUE);
   for(int i=LookbackDays-1;i<rates_total;i++)
   {
      double weight=0,mean=0,m2=0;
      for(int j=i-LookbackDays+1;j<=i;j++)
      {
         double w=UseTickVolume?(double)tick_volume[j]:(double)volume[j];
         if(w<=0) continue;
         double price=(high[j]+low[j]+close[j])/3.0;
         double total=weight+w,delta=price-mean;
         mean+=w/total*delta; m2+=w*delta*(price-mean); weight=total;
      }
      if(weight<=0) continue;
      double deviation=MathSqrt(MathMax(0,m2/weight));
      meanLine[i]=mean; upper[i]=mean+BandMultiplier*deviation; lower[i]=mean-BandMultiplier*deviation;
   }
   return rates_total;
}

Copy this code into MetaEditor, save it in the MQL5/Indicators folder, and compile with F7.

Change the settings or add your own signal filter

Describe the inputs, buffers, alerts, visual style, or confirmation filter you want. Pineify generates editable MQL5 source code that you can inspect and compile in MetaEditor.

Frequently Asked Questions

Related MQL5 pages

Risk and testing note

These educational examples require independent compilation and testing. A compile pass or an illustrative setting does not establish execution correctness or profitability. Any reported performance must be supported by reproducible data and test settings; past results do not predict future returns. Trading involves significant risk of loss. This content is not financial advice.