//+------------------------------------------------------------------+ 
//|                                                          DPO.mq4 | 
//|                                         Detrend Price Oscillator | 
//|                                       Copyright © 2006, firedave | 
//|                                        http://www.fx-review.com/ | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright © 2006, firedave" 
#property link      "http://www.fx-review.com/" 

#property indicator_separate_window 
#property indicator_buffers 4 
#property indicator_color1 LimeGreen
#property indicator_level1 0
#property indicator_width1 3
#property indicator_color2 DarkGreen
#property indicator_level2 0
#property indicator_width2 2
#property indicator_color3 Red
#property indicator_level3 0
#property indicator_width3 3
#property indicator_color4 Maroon
#property indicator_level4 0
#property indicator_width4 2



extern int 
      FastMAperiod   = 3,
      FastMAtype     = 1,
      FastMAprice    = 0,
      SlowMAperiod   = 9,
      SlowMAtype     = 1,
      SlowMAprice    = 0,
      MA1            = 5,
      //MA1_Shift      =  0,
      //MA1_Method     =  0,     
      MA2            = 7;
      //MA2_Shift      =  0,
      //MA2_Method     =  0, 
      

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double TS[], MA1Buffer[], MA2Buffer[];

//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
  { 
   IndicatorBuffers(7);  

   IndicatorDigits(Digits+1);
   IndicatorShortName("DPO ("+FastMAperiod+","+SlowMAperiod+") with MA1("+MA1+") and MA2("+MA2+")"); 

   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(3,ExtMapBuffer4);

 /*   
   SetIndexBuffer(0,TS);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexLabel(0,"DPO");
   SetIndexBuffer(1,MABuffer1);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexLabel(1,"MA1");
   SetIndexBuffer(2,MABuffer2);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   SetIndexLabel(2,"MA2");
   
SetIndexDrawBegin(0,2);
SetIndexDrawBegin(1,0); 
SetIndexDrawBegin(2,0);
*/   
   return(0); 
  } 

//+------------------------------------------------------------------+ 
//| Custor indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() 
{ 
   return(0); 
} 

//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
int start() 
{ 
   
   int    i, 
          limit,
          counted_bars=IndicatorCounted(); 
         
   if(counted_bars<0) return(-1); 
   if(counted_bars>0) counted_bars--; 
   limit=Bars-counted_bars; 

   for(i=0; i<limit; i++) {
      TS[i] = NormalizeDouble((iMA(NULL,0,FastMAperiod,0,FastMAtype,FastMAprice,i) - iMA(NULL,0,SlowMAperiod,0,SlowMAtype,SlowMAprice,i))/Point,0);
   for(i=0; i<limit; i++){
      MA1Buffer[i]=iMAOnArray(TS,Bars,MA1,0,MODE_EMA,i);
      MA2Buffer[i]=iMAOnArray(TS,Bars,MA2,0,MODE_EMA,i);
      }
      if(TS[i]>0) {
      if  (MA1Buffer[i]>TS[i]) {
        ExtMapBuffer1[i] = TS[i];
        ExtMapBuffer2[i] = 0;
        }
      else {
        ExtMapBuffer2[i] = TS[i];
        ExtMapBuffer1[i] = 0;
         }
        ExtMapBuffer3[i] = 0;
        ExtMapBuffer4[i] = 0;
        }
   else {
     if (MA1Buffer[i]<TS[i]) { 
        ExtMapBuffer3[i] = TS[i];
        ExtMapBuffer4[i] = 0;
        }
     else { 
        ExtMapBuffer4[i] = TS[i];
        ExtMapBuffer3[i] = 0;
        }
        ExtMapBuffer3[i] = 0;
        ExtMapBuffer4[i] = 0;
        }
      }
    
 
   return(0); 
  } 
//+------------------------------------------------------------------+ 


