//+------------------------------------------------------------------+
//|                                     Moving_Average_Slope_0.7.mq4 |
//|                                              Profit_Warning 2010 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Yellow
#property indicator_color2 Red
#property indicator_color3 Lime
#property indicator_color4 Yellow
#property indicator_color5 Red
#property indicator_color6 Lime

//---- indicator parameters
extern int MA_Period_Fast     = 25;
extern int MA_Period_Medium   = 50;
extern int MA_Period_Slow     = 100;

extern int Smoothing_Period = 5;

//---- indicator buffers

double SlopeBuffer_Fast_Smoothed [];
double SlopeBuffer_Medium_Smoothed [];
double SlopeBuffer_Slow_Smoothed [];
double SlopeBuffer_Fast[];
double SlopeBuffer_Medium[];
double SlopeBuffer_Slow[];

//----
int ExtCountedBars=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int    draw_begin;
   string short_name;
   
   IndicatorBuffers (6);

//---- drawing settings

   SetIndexStyle(0,DRAW_LINE,EMPTY,2);
   SetIndexShift(0,0);
   
   SetIndexStyle(1,DRAW_LINE,EMPTY,2);
   SetIndexShift(1,0);

   SetIndexStyle(2,DRAW_LINE,EMPTY,2);
   SetIndexShift(2,0);
   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexShift(3,0);
   
   SetIndexStyle(4,DRAW_LINE);
   SetIndexShift(4,0);

   SetIndexStyle(5,DRAW_LINE);
   SetIndexShift(5,0);
      
   SetIndexDrawBegin(0,draw_begin);
   
   SetIndexDrawBegin(1,draw_begin); 

   SetIndexDrawBegin(2,draw_begin); 
   SetIndexDrawBegin(3,draw_begin);
   
   SetIndexDrawBegin(4,draw_begin); 

   SetIndexDrawBegin(5,draw_begin); 

    
//---- indicator buffers mapping
   
   SetIndexBuffer(0,SlopeBuffer_Fast_Smoothed);
   SetIndexBuffer(1,SlopeBuffer_Medium_Smoothed);
   SetIndexBuffer(2,SlopeBuffer_Slow_Smoothed); 
   SetIndexBuffer(3,SlopeBuffer_Fast);
   SetIndexBuffer(4,SlopeBuffer_Medium);
   SetIndexBuffer(5,SlopeBuffer_Slow);   
   
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(2,0.0);
   SetIndexEmptyValue(3,0.0);
   SetIndexEmptyValue(4,0.0);
   SetIndexEmptyValue(5,0.0);
   
      IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   
   if(MA_Period_Slow<2) MA_Period_Slow=13;
 //  draw_begin=MA_Period_Slow-1;
  draw_begin=200;  
//---- initialization done

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=MA_Period_Slow) return(0);
   ExtCountedBars=IndicatorCounted();
   
//---- check for possible errors

   if (ExtCountedBars<0) return(-1);
   
//---- last counted bar will be recounted

   if (ExtCountedBars>0) ExtCountedBars--;

   ema();
   
   return(0);
  }

//+------------------------------------------------------------------+
//| Exponential Moving Average                                       |
//+------------------------------------------------------------------+
void ema()
  {
   double pr_slow=2.0/(MA_Period_Slow+1);
   double pr_medium=2.0/(MA_Period_Medium+1);
   double pr_fast=2.0/(MA_Period_Fast+1);
   
   int    pos=Bars-2;
   int    cas=Bars-2;
   int    n;
   double value = 0;
   
   if(ExtCountedBars>2) pos=Bars-ExtCountedBars-1;
   
   if (Bars + 1 < Smoothing_Period+59) return(0);
   
  // if (Bars-1 < 125) return;
   
//---- main calculation loop

   if (Bars < 200) return;
   while(pos>=0)
     {

      SlopeBuffer_Fast [pos] = Close[pos]*pr_fast - iMA(0,0,MA_Period_Fast,0,1,PRICE_CLOSE,pos+1)*pr_fast;
   
      SlopeBuffer_Medium [pos] = Close[pos]*pr_medium - iMA(0,0,MA_Period_Medium,0,1,PRICE_CLOSE,pos+1)*pr_medium;  
        
      SlopeBuffer_Slow [pos] = Close[pos]*pr_slow - iMA(0,0,MA_Period_Slow,0,1,PRICE_CLOSE,pos+1)*pr_slow; 

 	   pos--;
     }
    if(ExtCountedBars>2) pos=Bars-ExtCountedBars-1;
   

   while(cas>=0)
     {


      SlopeBuffer_Fast_Smoothed [cas+1] = iMAOnArray(SlopeBuffer_Fast,0,Smoothing_Period,0,MODE_EMA,cas);     

      SlopeBuffer_Medium_Smoothed [cas+1] = iMAOnArray(SlopeBuffer_Medium,0,Smoothing_Period,0,MODE_EMA,cas);         

      SlopeBuffer_Slow_Smoothed [cas+1] = iMAOnArray(SlopeBuffer_Slow,0,Smoothing_Period,0,MODE_EMA,cas);    
           
 	   cas--;
     }
  }

