//+------------------------------------------------------------------+
//|                                                Efficiency_v1.mq4 |
//|                                Copyright © 2007, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, 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
#property indicator_level1 0
#property indicator_level2 -30
#property indicator_level3 30

//---- 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 Efficiency
extern int     Smooth         = 5;   // Period of Smoothing MA  
extern int     Signal         = 5;   // Period of Signal MA  
extern int     MA_Mode        = 0;   // Mode of Smoothing MA 
extern int     NoiseMode      = 0;   // Mode of Calculation of Noise
//---- indicator buffers
double DbSmoothBuffer[];
double SmoothBuffer[];
double SignalBuffer[];
double ER[];
double Del[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   IndicatorBuffers(5);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,DbSmoothBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,SignalBuffer);
   SetIndexBuffer(2,ER);
   SetIndexBuffer(3,Del);
   SetIndexBuffer(4,SmoothBuffer); 
   string short_name;
//---- indicator line
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="Efficiency("+Length+","+Smooth+","+Signal+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Efficiency");
   SetIndexLabel(1,"Signal");
//----
    
   SetIndexDrawBegin(0,Length+2*Smooth+Signal);
   SetIndexDrawBegin(1,Length+2*Smooth+Signal);
//----
  
   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+2*Smooth+Signal;i++) 
   {
   SignalBuffer[Bars-i]=0;    
   DbSmoothBuffer[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[iHighest(NULL,0,MODE_HIGH,Length,shift)] - Low[iLowest(NULL,0,MODE_LOW,Length,shift)];
                  
   if (Noise > 0) ER[shift] = 100*(iMA(NULL,0,1,0,0,Price,shift) - iMA(NULL,0,1,0,0,Price,shift+Length-1))/
   Noise;
   }
   
   for(shift=limit;shift>=0;shift--) 
   SmoothBuffer[shift] = iMAOnArray(ER,0,Smooth,0,MA_Mode,shift);  
   
   for(shift=limit;shift>=0;shift--) 
   DbSmoothBuffer[shift] = iMAOnArray(SmoothBuffer,0,Smooth,0,MA_Mode,shift); 
   
   for(shift=limit;shift>=0;shift--) 
   SignalBuffer[shift] = iMAOnArray(DbSmoothBuffer,0,Signal,0,MA_Mode,shift); 

//----------   

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

