//+------------------------------------------------------------------+
//|                                                     Averages.mq4 |
//|                                       Copyright © 2009, Obaidah. |
//|                                               clefts@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Obaidah."
#property link      "clefts@hotmail.com"
#define IndicatorName "Spread Volume Averages"

#property  indicator_separate_window

extern string aa="Enter the deviation percentage";
extern double dev=0.5;
extern string bb="Enter the period to look back for";
extern int    Lb=20;
extern bool   displayAlert = true;
extern string cc="Enter the periods to calculate the average for";
extern int    x1=10;
extern int    x2=20;
extern int    x3=30;
extern int    x4=40;
extern int    x5=50;
extern int    x6=60;

int array1[6];
int max=-1;
int index1;
datetime flag=0;
int Multi=1;
int init()
{
   IndicatorShortName(IndicatorName);
   for(int w=0;w<Digits;w++) // to avoid screwing up the money management equation such a variable needed
      Multi*=10;
   array1[0]=x1;
   array1[1]=x2;
   array1[2]=x3;
   array1[3]=x4;
   array1[4]=x5;
   array1[5]=x6;
   for(int i=0;i<6;i++) { // Get me the longest period and the index of the look back period
      if(max<array1[i])
         max=array1[i];
      if(Lb==array1[i])
         index1=i;
   }
   return(0);
}

int deinit()
{
   return(0);
}

int start()
{
   if(flag != Time[0]) {
      flag=Time[0];
      double spd=0;
      double vol=0;
      double spdaverage[6];
      double volaverage[6];
      for(int i=1;i<=max;i++) {
         spd+=High[i]-Low[i];
         vol+=Volume[i];
         for(int j=0;j<6;j++) {
            if(i==array1[j]) {
               spdaverage[j]=(spd/i)*Multi;
               volaverage[j]=vol/i;
            }
         }
	   }
	   if(displayAlert) {
         if(High[1]-Low[1] > (1+dev)*spdaverage[index1])
	        Alert("HIGH SPREAD BAR on ",Symbol(), "\nSpread " + DoubleToStr((((High[1]-Low[1])/spdaverage[index1])*100)-100,0) + "% higher than the last " + Lb + " bars avg");
	      else
	        if(High[1]-Low[1] < dev*spdaverage[index1])
	           Alert("LOW SPREAD BAR on ",Symbol(), "\nSpread ", DoubleToStr(100-((High[1]-Low[1])/spdaverage[index1])*100,0), "% lower than the last " + Lb + " bars avg");
	        
	      if(Volume[1] > (1+dev)*volaverage[index1])
	        Alert("HIGH VOLUME BAR on ", Symbol(), "\nVolume " + DoubleToStr(((Volume[1]/volaverage[index1])*100)-100,0) + "% higher than the last " + Lb + " bars avg");
	      else
	        if(Volume[1] < dev*volaverage[index1])
	           Alert("LOW VOLUME BAR on ",Symbol(), "\nVolume ", DoubleToStr(100-(Volume[1]/volaverage[index1])*100,0), "% lower than the last " + Lb + " bars avg");
	   }
	   // Here is where we draw objects.
	   Comment("The average spread for the last " + x1 + "bars is: " + DoubleToStr(spdaverage[0],0)
	   +"\nThe average spread for the last " + x2 + "bars is: " + DoubleToStr(spdaverage[1],0)
	   +"\nThe average spread for the last " + x3 + "bars is: " + DoubleToStr(spdaverage[2],0)
	   +"\nThe average spread for the last " + x4 + "bars is: " + DoubleToStr(spdaverage[3],0)
	   +"\nThe average spread for the last " + x5 + "bars is: " + DoubleToStr(spdaverage[4],0)
	   +"\nThe average spread for the last " + x6 + "bars is: " + DoubleToStr(spdaverage[5],0)
	   +"\nThe average volume for the last " + x1 + "bars is: " + DoubleToStr(volaverage[0],0)
	   +"\nThe average volume for the last " + x2 + "bars is: " + DoubleToStr(volaverage[1],0)
	   +"\nThe average volume for the last " + x3 + "bars is: " + DoubleToStr(volaverage[2],0)
	   +"\nThe average volume for the last " + x4 + "bars is: " + DoubleToStr(volaverage[3],0)
	   +"\nThe average volume for the last " + x5 + "bars is: " + DoubleToStr(volaverage[4],0)
	   +"\nThe average volume for the last " + x6 + "bars is: " + DoubleToStr(volaverage[5],0));
	}
   return(0);
}