//+------------------------------------------------------------------+
//|                                                      PPO_Lnx.mq4 |
//|                                                                  |
//|                                                                  |
//| Made according to rules explained in Stockcharts.com by Linuxser |
//| http://stockcharts.com                                           |
//| basic formula is:                                                |  
//| (10-period EMA minus 30-period EMA) divided by the 30-period EMA |
//|                                                                  |
//| Linuxser 2007                                                    |
//+------------------------------------------------------------------+
#property copyright "Under The GNU General Public License"
#property link      "www.gnu.org"

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 8
#property  indicator_color1  Red
#property  indicator_color2  Green
#property  indicator_color3  Red
#property  indicator_color4  Magenta
#property  indicator_color5  Green
#property  indicator_color6  Blue
#property  indicator_color7  Red
#property  indicator_color8  Yellow
#property  indicator_width1  1
#property  indicator_width2  1
#property  indicator_width3  1
#property  indicator_width4  2
#property  indicator_width5  2
#property  indicator_width6  2
#property  indicator_width7  1
#property  indicator_width8  1
#property  indicator_level1  0
#property  indicator_levelcolor  LimeGreen
#property  indicator_levelstyle 2

//---- indicator parameters
extern int FastMA=10;
extern int SlowMA=30;
extern int SignalEMA=9;
extern int Applied_Price  =  0; // 0: Close // 1: Open // 2: High // 3: Low // 4: Median (H+L/2) // 5: Typical (H+L+C/3) // 6: Weighted (H+L+C+C/4)
extern int MA_Method  =  1; //MODE_SMA :	0 // MODE_EMA:	1 // MODE_SMMA: 2 // MODE_LWMA: 3 //
extern int Style  = 0; 

//---- indicator buffers
double     PPOBuffer[];
double     SignalBuffer[];
double     StyleBuffer[];
double     OsmaBuffer[];
double     ExtBuffer1[];
double     ExtBuffer2[];
double     ExtBuffer3[];
double     ExtBuffer4[];
double     prev,current;
double     prevosma,currentosma;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_NONE);
   SetIndexStyle(2,DRAW_NONE);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_HISTOGRAM);
   SetIndexStyle(6,DRAW_LINE);
   SetIndexStyle(7,DRAW_LINE);
   SetIndexDrawBegin(1,SignalEMA);
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping

   SetIndexBuffer(0,StyleBuffer);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);  
   SetIndexBuffer(3,ExtBuffer3);
   SetIndexBuffer(4,ExtBuffer4);
   SetIndexBuffer(5,OsmaBuffer);
   SetIndexBuffer(6,PPOBuffer);
   SetIndexBuffer(7,SignalBuffer);
   
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("PPO Lnx ("+FastMA+","+SlowMA+","+SignalEMA+") Method ["+MA_Method+"])");
   SetIndexLabel(6,"PPO");
   SetIndexLabel(7,"Signal");
   SetIndexLabel(5,"Histogram");      
   SetIndexLabel(0,NULL);
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,NULL);
   SetIndexLabel(4,NULL);
 
   
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int i;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
//---- Take care about user mistakes and defaulting on errors  
   if(MA_Method>3) MA_Method=0;
   if(Applied_Price>6) Applied_Price=0;
   if(Style>7) Style=0;
//---- PPO counted in the 1-st buffer
   for(i=0; i<limit; i++)
//---- Avoid the zero divide   
      if(iMA(NULL,0,SlowMA,0,MA_Method,Applied_Price,i) != 0)
      
      PPOBuffer[i]=(iMA(NULL,0,FastMA,0,MA_Method,Applied_Price,i)-iMA(NULL,0,SlowMA,0,MA_Method,Applied_Price,i))/iMA(NULL,0,SlowMA,0,MA_Method,Applied_Price,i);
//---- signal line counted in the 2-nd buffer
   for(i=0; i<limit; i++)
      SignalBuffer[i]=iMAOnArray(PPOBuffer,Bars,SignalEMA,0,MA_Method,i);
//---- main loop
   for(i=0; i<limit; i++)
      OsmaBuffer[i]=PPOBuffer[i]-SignalBuffer[i]; 
//---- dispatch values between 2 buffers
   bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current=PPOBuffer[i];
      prev=PPOBuffer[i+1];
      if(current>prev) up=true;
      if(current<prev) up=false;
      if(!up)
        {
         ExtBuffer2[i]=current;
         ExtBuffer1[i]=0.0;
        }
      else
        {
         ExtBuffer1[i]=current;
         ExtBuffer2[i]=0.0;
        }
     }
     
   for(i=limit-1; i>=0; i--)
     {
      currentosma=OsmaBuffer[i];
      prevosma=OsmaBuffer[i+1];
      if(currentosma>prevosma) up=true;
      if(currentosma<prevosma) up=false;
      if(!up)
        {
         ExtBuffer3[i]=currentosma;
         ExtBuffer4[i]=0.0;
        }
      else
        {
         ExtBuffer4[i]=currentosma;
         ExtBuffer3[i]=0.0;
        }
     }        
//---- Different Styles      
   if(Style == 1)
      {
         for(i=0; i<limit; i++)
         {
               StyleBuffer[i]=PPOBuffer[i];
               SetIndexStyle(0,DRAW_HISTOGRAM);             
         } 
         }  
    
    if(Style == 2)
      {
         for(i=0; i<limit; i++)
         {
               StyleBuffer[i]=PPOBuffer[i];
               SetIndexStyle(0,DRAW_LINE);
               SetIndexStyle(1,DRAW_NONE);
               SetIndexStyle(2,DRAW_NONE);
               SetIndexStyle(3,DRAW_NONE);
               SetIndexStyle(4,DRAW_NONE);
               SetIndexStyle(5,DRAW_NONE);
               SetIndexStyle(6,DRAW_HISTOGRAM);
               SetIndexStyle(7,DRAW_NONE);
               SetIndexLabel(2,"PPO");
               SetIndexLabel(0,NULL);
               SetIndexLabel(1,NULL);
               SetIndexLabel(3,NULL);
               SetIndexLabel(4,NULL);
               SetIndexLabel(5,NULL);
               SetIndexLabel(6,NULL);

         }
         }
    if(Style == 3)
      {
         for(i=0; i<limit; i++)
         {
               SetIndexStyle(0,DRAW_NONE);
               SetIndexStyle(1,DRAW_NONE);
               SetIndexStyle(2,DRAW_NONE);
               SetIndexStyle(3,DRAW_NONE);
               SetIndexStyle(4,DRAW_NONE);
               SetIndexStyle(5,DRAW_HISTOGRAM);
               SetIndexStyle(6,DRAW_NONE);
               SetIndexStyle(7,DRAW_NONE);
               SetIndexLabel(3,"PPO");
               SetIndexLabel(0,NULL);
               SetIndexLabel(1,NULL);
               SetIndexLabel(2,NULL);
               SetIndexLabel(3,NULL);
               SetIndexLabel(4,NULL);
               SetIndexLabel(6,NULL);
               SetIndexLabel(7,NULL);
         }
         }         
    if(Style == 4)
      {
         for(i=0; i<limit; i++)
          {               
               SetIndexStyle(0,DRAW_NONE);
               SetIndexStyle(1,DRAW_NONE);
               SetIndexStyle(2,DRAW_NONE);
               SetIndexStyle(3,DRAW_NONE);
               SetIndexStyle(4,DRAW_NONE);
               SetIndexStyle(5,DRAW_NONE);
               SetIndexStyle(6,DRAW_LINE);
               SetIndexStyle(7,DRAW_LINE);
               SetIndexLabel(7,"Signal");
               SetIndexLabel(6,"PPO");
               SetIndexLabel(0,NULL);
               SetIndexLabel(1,NULL);
               SetIndexLabel(2,NULL);
               SetIndexLabel(3,NULL);
               SetIndexLabel(4,NULL);
               SetIndexLabel(5,NULL);               
         }
         }
    if(Style == 5)
      {
         for(i=0; i<limit; i++)
         {
               
               SetIndexStyle(0,DRAW_NONE);
               SetIndexStyle(1,DRAW_HISTOGRAM);
               SetIndexStyle(2,DRAW_HISTOGRAM);
               SetIndexStyle(3,DRAW_NONE);
               SetIndexStyle(4,DRAW_NONE);
               SetIndexStyle(5,DRAW_NONE);
               SetIndexStyle(6,DRAW_NONE);
               SetIndexStyle(7,DRAW_NONE);
               SetIndexDrawBegin(4,34);
               SetIndexDrawBegin(5,34);
               SetIndexLabel(1,"PPO Up");
               SetIndexLabel(2,"PPO Down");
               SetIndexLabel(0,NULL);
               SetIndexLabel(3,NULL);
               SetIndexLabel(4,NULL);
               SetIndexLabel(5,NULL);
               SetIndexLabel(6,NULL);
               SetIndexLabel(7,NULL);
         }
         }
     if(Style == 6)
      {
         for(i=0; i<limit; i++)
         {
               SetIndexStyle(0,DRAW_NONE);
               SetIndexStyle(1,DRAW_HISTOGRAM);
               SetIndexStyle(2,DRAW_HISTOGRAM);
               SetIndexStyle(3,DRAW_NONE);
               SetIndexStyle(4,DRAW_NONE);
               SetIndexStyle(5,DRAW_NONE);
               SetIndexStyle(6,DRAW_NONE);
               SetIndexStyle(7,DRAW_LINE);
               SetIndexDrawBegin(4,34);
               SetIndexDrawBegin(5,34);
               SetIndexLabel(1,"PPO Up");
               SetIndexLabel(2,"PPO Down");
               SetIndexLabel(7,"Signal");
               SetIndexLabel(0,NULL);
               SetIndexLabel(3,NULL);
               SetIndexLabel(4,NULL);
               SetIndexLabel(5,NULL);
               SetIndexLabel(6,NULL);               
 
         }
         }
     
     if(Style == 7)
      {
         for(i=0; i<limit; i++)
         {
               SetIndexStyle(0,DRAW_NONE);
               SetIndexStyle(1,DRAW_NONE);
               SetIndexStyle(2,DRAW_NONE);
               SetIndexStyle(3,DRAW_HISTOGRAM);
               SetIndexStyle(4,DRAW_HISTOGRAM);
               SetIndexStyle(5,DRAW_NONE);
               SetIndexStyle(6,DRAW_LINE);
               SetIndexStyle(7,DRAW_LINE);
               SetIndexDrawBegin(6,34);
               SetIndexDrawBegin(7,34);               
               SetIndexLabel(4,"PPO Up");
               SetIndexLabel(3,"PPO Down"); 
               SetIndexLabel(7,"Signal");
               SetIndexLabel(6,"PPO"); 
               SetIndexLabel(0,NULL);
               SetIndexLabel(1,NULL);
               SetIndexLabel(2,NULL);
               SetIndexLabel(5,NULL);
 
         }
         }                                  
//---- done
   return(0);
  }
//+------------------------------------------------------------------+