
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Crimson
#property indicator_color2 DarkOrange
#property indicator_color3 Teal
#property indicator_color4 Sienna
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 1

extern string  Note = " MumberOfBars = 0  Displays all Bars";
extern int     NumberOfBars = 1000;
extern int     MAPeriod = 10;

double up[],dn[],flat[],v4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
      SetIndexBuffer(0,up);
      SetIndexStyle(0,DRAW_HISTOGRAM,0);
      SetIndexLabel(0,"Expanding Volume");
      
      SetIndexBuffer(1,dn);
      SetIndexStyle(1,DRAW_HISTOGRAM,0);
      SetIndexLabel(1,"Contracting Volume");
      
      SetIndexBuffer(2,flat);
      SetIndexStyle(2,DRAW_HISTOGRAM,0);
      SetIndexLabel(2,"Neutral Volume");

      SetIndexBuffer(3,v4);
      SetIndexStyle(3,DRAW_LINE,2);
      SetIndexLabel(3,"MovingAverage("+MAPeriod+")");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   double v0,v1,v2, tempv;
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   if ( NumberOfBars == 0 ) 
      NumberOfBars = Bars-counted_bars;
   limit=NumberOfBars; //Bars-counted_bars;

   for(int i=0; i<limit; i++)   
      {
         up[i] = 0; dn[i] = 0; v4[i]=0;tempv=0;
       
         v0 = iVolume(NULL,0,i);
         v1 = iVolume(NULL,0,i+1);
         v2 = iVolume(NULL,0,i+2);
         
         flat[i] = v0;
         
         if ( v0<v1 && v0<v2 )
            {
               dn[i] = v0;
               flat[i] = 0;
            }   
         if ( v0>v1 && v0>v2 )
            {
               up[i] = v0;
               flat[i] = 0;
            }   
         for ( int n=i;n<i+MAPeriod;n++ )
            {
               tempv= Volume[n] + tempv; 
            } 
          v4[i] = tempv/MAPeriod;          
       
      
      }
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+