//+------------------------------------------------------------------+
//|                                                Efficiency_v1.mq4 |
//|                                Copyright © 2006, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"


#property indicator_separate_window
#property indicator_buffers 2

#property indicator_color1 SkyBlue
#property indicator_width1 2
#property indicator_color2 Tomato
#property indicator_width2 0
#property indicator_style2 2
#property indicator_maximum  100 
#property indicator_minimum -100


//---- input parameters
extern int     Price          = 0;   // Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close) 
extern int     Length         = 10;  // Period of NonLagMA
extern int     Smooth         = 5;   // Period of Smoothing MA  
extern int     MASmooth       = 0;   // Mode of Smoothing MA 
extern int     NoiseMode      = 0;   // Mode of Calculation of Noise
//---- indicator buffers
double ER[];
double SmoothBuffer[];
double Del[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   IndicatorBuffers(3);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ER);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,SmoothBuffer);
   SetIndexBuffer(2,Del);
    
   string short_name;
//---- indicator line
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="Efficiency("+Length+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"ER");
   SetIndexLabel(1,"Smooth");
//----
    
   SetIndexDrawBegin(0,Length+Smooth);
   SetIndexDrawBegin(1,Length+Smooth);
   SetIndexDrawBegin(2,Length+Smooth);
//----
  
   return(0);
  }

//+------------------------------------------------------------------+
//| Efficiency_v1                                                      |
//+------------------------------------------------------------------+
int start()
{
   int    i,shift, counted_bars=IndicatorCounted(),limit;
   double price;      
   if ( counted_bars > 0 )  limit=Bars-counted_bars;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-1; 
   if ( counted_bars < 1 ) 
   
   for(i=1;i<Length+Smooth;i++) 
   {
   ER[Bars-i]=0;    
   SmoothBuffer[Bars-i]=0;  
   }
   
   for(shift=limit;shift>=0;shift--) 
   {	
   Del[shift] = iMA(NULL,0,1,0,0,Price,shift) - iMA(NULL,0,1,0,0,Price,shift+1);
   
   if ( NoiseMode == 0 )
   { 
   double Noise=0;
   for (i=0;i<=Length-1;i++) Noise += MathAbs(Del[shift+i]);
   }
   else
   if ( NoiseMode == 1 )
   Noise = High[Highest(NULL,0,MODE_HIGH,Length,shift)] - Low[Lowest(NULL,0,MODE_LOW,Length,shift)];
   else
   if ( NoiseMode == 2 )
   { 
   Noise=0;
   for (i=0;i<=Length-1;i++) Noise += (High[shift+i] - Low[shift+i]);
   }
               
   if (Noise > 0) ER[shift] = 100*(iMA(NULL,0,1,0,0,Price,shift) - iMA(NULL,0,1,0,0,Price,shift+Length))/
   Noise;
   }
   
   for(shift=limit;shift>=0;shift--) 
   SmoothBuffer[shift] = iMAOnArray(ER,0,Smooth,0,MASmooth,shift);  
//----------   

//----
	return(0);	
}

