
#property copyright ""
#property link      ""

#property indicator_separate_window

#property indicator_buffers 5
#property indicator_color1 Red
#property indicator_color2 YellowGreen
#property indicator_color3 Tomato
#property indicator_color4 Yellow
#property indicator_color5 White

#property indicator_width1 5
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 1
#property indicator_width5 2

extern bool    ShowAverageVolume = false;
extern bool    ShowMA = true;

extern int     NumberOfDays = 50;
extern string  Note = "0 for all the days in history";

extern int     MAPeriod = 4;
extern int     MAShift = 2;

double rvGreen[], rvRed[], aVolume[],activityVolume[], sma[], averageVolume[], maxVolume[], maximum[], nbBars[];
double rangeTick[];

int barPeriod, nbBar ;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
          
      SetIndexBuffer(0,maximum);
      SetIndexStyle(0,DRAW_LINE,0,5);
      SetIndexLabel(0,"High Activity ");
      
      SetIndexBuffer(1,rvGreen);
      SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexLabel(1,"Volume Activity ");
      
      SetIndexBuffer(2,rvRed);
      SetIndexStyle(2,DRAW_HISTOGRAM);
      SetIndexLabel(2,"Volume Activity ");
      
      SetIndexBuffer(3,sma);
      SetIndexStyle(3,DRAW_LINE,0,2);
      SetIndexLabel(3,"MA " + MAPeriod);
      
      SetIndexBuffer(4,aVolume);
      SetIndexStyle(4,DRAW_HISTOGRAM);
      SetIndexLabel(4,"average Volume ");
         
      IndicatorShortName("Volume Activity Indicator" );
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  int subWindowIndex = WindowFind("Volume Activity Indicator");

   barPeriod = PERIOD_D1/Period();
   nbBar = 0;
   if((NumberOfDays!=0)&&(NumberOfDays*barPeriod < Bars)) {
      nbBar = NumberOfDays*barPeriod;
   } else {
      nbBar = Bars;
   }

   calculateAverage();
   
   ArrayResize(activityVolume,nbBar);
   double max = 0, mean = 0;
   
   for(int i=0;i<barPeriod;i++) {
      if(averageVolume[i] != 0) {
         max += (maxVolume[i]/averageVolume[i])/barPeriod;
      }
      else Print("!! average volume is null! index=" + i);
      mean += averageVolume[i]/barPeriod;
   }
   
   int index = 0;
   double ma = 0;
   for(i = 0; i<nbBar; i++) {
      index = (TimeMinute(Time[i]) + TimeHour(Time[i])*60)/Period();  
      if(averageVolume[index] != 0) {
         activityVolume[i]= Volume[i]/averageVolume[index];
      } 
      ma += activityVolume[i]; 
      if(Close[i]>=Open[i]) {
         rvGreen[i] = activityVolume[i];
         rvRed[i] = 0;
      } else {
         rvRed[i] = activityVolume[i];
         rvGreen[i] = 0;
      }     
      if(i>=(MAPeriod-1)) {
         sma[i-MAPeriod + MAShift] = ma/MAPeriod;
         ma -= activityVolume[i-MAPeriod+1];
      }
      if(ShowAverageVolume) {
         aVolume[i] = -averageVolume[index]/mean;
      } else {
         aVolume[i] = 0;
      }
      
      maximum[i] = max;
   }
   
   if( ObjectFind("1 Line") == -1) {
      if(!ObjectCreate("1 Line",OBJ_HLINE, subWindowIndex,Time[0], 1)) {
         Print("error: can't create label_object! code #",GetLastError());
      }
   } else {
      ObjectSet("1 Line", 1, 1 );
   }
   ObjectSet("1 Line",OBJPROP_COLOR, RoyalBlue );
   ObjectSet("1 Line",OBJPROP_STYLE, STYLE_DOT );  
   
   if(!ShowMA) {
      SetIndexStyle(3,DRAW_NONE,0,1);
   } else {
      SetIndexStyle(3,DRAW_LINE,0,1);
   }
      
   return(0);
  }
  
  void calculateAverage() {
      double totVolume[];
      ArrayResize( averageVolume,barPeriod);
      ArrayResize( maxVolume,barPeriod);    
      ArrayResize(totVolume, barPeriod);
      ArrayResize(nbBars, barPeriod);
      
      for(int i=0; i<nbBar;i++) {
         int index = (TimeMinute(Time[i]) + TimeHour(Time[i])*60)/Period();
         totVolume[index] += Volume[i];
         nbBars[index]++;
         if(Volume[i]>maxVolume[index]) {
            maxVolume[index]=Volume[i];    
         }       
      }
      for(i=0; i<barPeriod;i++){
         if(nbBars[i]!= 0) {
            averageVolume[i]= totVolume[i]/nbBars[i];
         } else {
            Print("!! nbBars[]=0");
            averageVolume[i] = 1;
         }
      }
  }
  
//+------------------------------------------------------------------+
         