Volume_Weighted_MA

Download script
Author: StatBars TO
Volume_Weighted_MA

This tool is designed to provide traders with a specialized moving average that gives extra importance to trading volume. Unlike standard moving averages, which treat every price point equally regardless of how much activity occurred at that time, this script calculates a "Volume-Weighted Moving Average."

How it Works

The logic follows a three-step process to determine the final line shown on your chart:

1. Volume Distribution The script looks back at a specific number of bars (defined by the user, such as 21) to see how much trading volume occurred in each period. It calculates the total volume for that timeframe and then determines the relative weight of each individual bar. This ensures that bars with high trading activity have a larger influence on the final calculation than bars with very little activity.

2. Price Selection The tool allows you to choose which specific price point from each candle should be used for the calculation. You can select various options, such as the opening price, closing price, the highest/lowest points, or a "Median" price (which is the exact middle point between the high and low). By default, it focuses on the median price to provide a balanced view of the market's range.

3. The Weighted Calculation Finally, the script multiplies each selected price by its corresponding volume weight and adds them all together. It then divides this total by the sum of the weights to produce a single average value for that point in time.

The Resulting Benefit

By using this method, the resulting line on your chart reacts more significantly to "heavy" moves where significant money is changing hands. It helps filter out "noisy" price movements that occur on low volume, providing a smoother, more reliable trend indicator that reflects where the actual market weight is moving.

Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
Volume_Weighted_MA
//+------------------------------------------------------------------+
//|                                           Volume_Weighted_MA.mq4 |
//|                                                      StatBars TO |
//|                                      http://ridecrufter.narod.ru |
//+------------------------------------------------------------------+
#property copyright "StatBars TO"
#property link      "http://ridecrufter.narod.ru"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 DeepPink

extern int  Period_MA=21;
extern int  Price_MA=PRICE_MEDIAN;

double MABuffer[];
double Vol[1];

int init()
  {
   IndicatorBuffers(1);
   SetIndexDrawBegin(0,Period_MA);
   SetIndexBuffer(0,MABuffer);
   SetIndexStyle(0,DRAW_LINE);
   IndicatorDigits(Digits+1);
   
   ArrayResize(Vol,Period_MA);
   
   return(0);
  }

int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
   int j,i;
   double sum;
   double Price;
   
   if(counted_bars<0) return(-1);

   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(i=0; i<limit; i++)
   {
      sum=0;
      Price=0;
      for(j=i;j<i+Period_MA;j++){Vol[j-i]=Volume[j];sum+=Vol[j-i];}
      for(j=0;j<Period_MA;j++)Vol[j]/=sum;
      for(j=i;j<i+Period_MA;j++)
      {
         Price+=get_price(j,Price_MA)*Vol[j-i];
      }
      MABuffer[i]=Price;
   }

   return(0);
  }

double get_price(int num, int applied_price)
{
   switch(applied_price)
   {
      case  PRICE_CLOSE    :  return(Close[num]);
      case  PRICE_OPEN     :  return(Open[num]);
      case  PRICE_HIGH     :  return(High[num]);
      case  PRICE_LOW      :  return(Low[num]);
      case  PRICE_MEDIAN   :  return((High[num]+Low[num])/2);
      case  PRICE_TYPICAL  :  return((High[num]+Low[num]+Close[num])/3);
      case  PRICE_WEIGHTED :  return((High[num]+Low[num]+Close[num]+Close[num])/4);
      default              :  return((High[num]+Low[num])/2);
   }
}

Comments