//+------------------------------------------------------------------+
//|                                               MA of Momentum.mq4 |
//|                               Copyright © 2011, PDSoftware Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, PDSoftware Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red

#property indicator_level1 100

//---- input parameters
extern int Mom_Period=10;
extern int MA_Period=5;

//---- buffers
double MomBuffer[];
double MA_Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;

   SetIndexDrawBegin(0,Mom_Period);
   SetIndexDrawBegin(1,MA_Period);

   IndicatorDigits(Digits);

   //SetLevelValue(0,100);

//---- indicator line
   SetIndexBuffer(0,MomBuffer);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexLabel(0,"Momentum");

   SetIndexBuffer(1,MA_Buffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexLabel(1,"SMA");
      
//---- name for DataWindow and indicator subwindow label
   short_name="MA of Momentum("+Mom_Period+","+MA_Period+")";
   IndicatorShortName(short_name);

   
//----


//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Momentum                                                         |
//+------------------------------------------------------------------+
int start()
  {
/*   int i,counted_bars=IndicatorCounted();
//----
   if(Bars<=Mom_Period) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=Mom_Period;i++) MomBuffer[Bars-i]=0.0;
//----
   i=Bars-Mom_Period-1;
   if(counted_bars>=Mom_Period) i=Bars-counted_bars-1;
   while(i>=0)
     {
      MomBuffer[i]=Close[i]*100/Close[i+Mom_Period];
      MA_Buffer[i]=iMAOnArray(MomBuffer,Bars,MA_Period,0,MODE_SMA,i);
      i--;
     }
*/

   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   //limit=Bars-counted_bars;
   limit=Bars-counted_bars-Mom_Period;
   
//---- CCI counted in the 1-st buffer
   for(int i=0; i<limit; i++)
      MomBuffer[i]=Close[i]*100/Close[i+Mom_Period];
      //if (Close[i+Mom_Period]!=0) { MomBuffer[i]=Close[i]*100/Close[i+Mom_Period]; }
      
//---- MA on CCI in the 2-nd buffer
   for(i=0; i<limit; i++)
      MA_Buffer[i]=iMAOnArray(MomBuffer,Bars,MA_Period,0,MODE_SMA,i);
//---- done

   return(0);
  }
//+------------------------------------------------------------------+