//+------------------------------------------------------------------+
//|                               Forex Generator version 4.4        |
//|                                 Copyright (C) 2011, Etasoft Inc. |
//|                                    http://www.forexgenerator.com |
//+------------------------------------------------------------------+
// Keywords: indicator developer, Forex indicator builder, create forex indicator, MT4


#property copyright "Your copyright"
#property link "Link to your website"
#property indicator_separate_window
#property indicator_buffers 5

#property indicator_color1 Red
#property indicator_width1 2
#property indicator_color2 Blue
#property indicator_width2 2
#property indicator_color3 Green
#property indicator_width3 2
#property indicator_color4 Gold
#property indicator_width4 1
#property indicator_color5 Aqua
#property indicator_width5 1

extern int  avgPeriod = 200;
extern int  MAPeriod1 = 10;
extern int  MAPeriod2 = 40;
extern bool showTotal = false;
extern bool showSmall = false;
extern bool showLarge = true;
extern bool showRatio = false;

double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double Buffer5[];
double Buffer6[];
double Buffer7[];
double Buffer8[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
int init()
{
   IndicatorBuffers(8);
    SetIndexBuffer(0, Buffer1);
    SetIndexBuffer(1, Buffer2);
    SetIndexBuffer(2, Buffer3);
    SetIndexBuffer(3, Buffer4);
    SetIndexBuffer(4, Buffer5);
    SetIndexBuffer(5, Buffer6);
    SetIndexBuffer(6, Buffer7);
    SetIndexBuffer(7, Buffer8);
       IndicatorShortName("EVIndicator2011v8Noam");
    return(0);
}
int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//


int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars > 0) counted_bars--;
         int limit = MathMin(Bars -counted_bars,Bars-1);
 
   //
   //
   //
   //
   //

      double alpha1 = 2.0 / (1.0+MAPeriod1);
      double alpha2 = 2.0 / (1.0+MAPeriod2);
      for(int i = limit; i >= 0; i--)
      {
         double totalVol = 0;
         double totalVoa = 0;
         for (int j = 0; j < avgPeriod; j++)
         {
            double tmp  = 0;
            double tmp2 = ((MathMax(High[i+j], Close[i+j+1]))-(MathMin(Low[i+j], Close[i+j+1])))+Point;
         	  if ( tmp2 > 0.00001 || tmp2 < -0.00001)  // filter out extremes
                   tmp = (((Close[i+j]-Close[i+j+1])+Point)/tmp2)*Volume[i+j];
         	totalVol += MathAbs(tmp);
         	totalVoa += MathAbs(Volume[i+j]);
         }
         double avgVol = totalVol/avgPeriod; ChartLine3(i,avgVol);
      
      //
      //
      //
      //
      //

      if (showRatio)
      {
         double EV  = Buffer6[i]; double EVPast  = Buffer6[i+avgPeriod-1];
         double EVS = Buffer7[i]; double EVSPast = Buffer7[i+avgPeriod-1];
         double EVL = Buffer8[i]; double EVLPast = Buffer8[i+avgPeriod-1];
      }

      //
      //
      //
      //
      //
            
      if (showTotal) 
      {
         if (showRatio)
               Buffer1[i] = (EV-EVPast)/totalVoa;
         else  Buffer1[i] = Buffer6[i];
      	      Buffer4[i] = Buffer4[i+1]+alpha1*(Buffer1[i]-Buffer4[i+1]);
      	      Buffer5[i] = Buffer5[i+1]+alpha2*(Buffer1[i]-Buffer5[i+1]);
      	continue;
      }
      if (showSmall) 
      {
         if (showRatio)
               Buffer2[i] = (EVS-EVSPast)/totalVoa;
         else  Buffer2[i] = Buffer7[i];
      	      Buffer4[i] = Buffer4[i+1]+alpha1*(Buffer2[i]-Buffer4[i+1]);
      	      Buffer5[i] = Buffer5[i+1]+alpha2*(Buffer2[i]-Buffer5[i+1]);
      	continue;
      }
      if (showLarge) 
      {
         if (showRatio)
               Buffer3[i] = (EVL-EVLPast)/totalVoa;
         else  Buffer3[i] = Buffer8[i];
    	         Buffer4[i] = Buffer4[i+1]+alpha1*(Buffer3[i]-Buffer4[i+1]);
     	         Buffer5[i] = Buffer5[i+1]+alpha2*(Buffer3[i]-Buffer5[i+1]);
      }
    }
    return(0);
}

//
//
//
//
//

void ChartLine3(int i, double avgVol)
{
   double tmp4 = 0;
   double tmp3 = ((MathMax(High[i], Close[i+1]))-(MathMin(Low[i], Close[i+1])))+Point;
      if (tmp3 > 0.00001 || tmp3 < -0.00001)
           tmp4 = (((Close[i]-Close[i+1])+Point)/tmp3)*Volume[i];

      //
      //
      //
      //
      //
      
      if (i>=Bars-avgPeriod)
      {
         if (showTotal) Buffer6[i] = 0;
         if (showSmall) Buffer7[i] = 0;
         if (showLarge) Buffer8[i] = 0;
         return;
      }
      else
      {
         if (showTotal) Buffer6[i] = Buffer6[i+1];
         if (showSmall) Buffer7[i] = Buffer7[i+1];
         if (showLarge) Buffer8[i] = Buffer8[i+1];
      }         
      if (showTotal) Buffer6[i] += tmp4;
      if (showSmall && MathAbs(tmp4) <  avgVol) Buffer7[i] += tmp4;
      if (showLarge && MathAbs(tmp4) >= avgVol) Buffer8[i] += tmp4;
      return;
}