//+------------------------------------------------------------------
//|
//+------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"

#property indicator_separate_window
#property indicator_buffers    3
#property indicator_color1     clrLimeGreen
#property indicator_color2     clrOrange
#property indicator_color3     clrOrange
#property indicator_width1     2
#property indicator_width2     2
#property indicator_width3     2
#property indicator_level1     0
#property indicator_levelstyle STYLE_DOT
#property strict

//
//
//

input int                DpoPeriod           = 14;          // Dpo period
input ENUM_APPLIED_PRICE DpoPrice            = PRICE_CLOSE; // Dpo price
enum enColorOn
{
   cc_zero,                                                 // Color on Dpo cross zero
   cc_slop,                                                 // Color on Dpo slope
};
input enColorOn          ColorOn             = cc_slop;     // Dpo color

double dpo[],dpoa[],dpob[],dpoc[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnInit()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,dpo ,INDICATOR_DATA); SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(1,dpoa,INDICATOR_DATA); SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(2,dpob,INDICATOR_DATA); SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(3,dpoc,INDICATOR_CALCULATIONS); 

   IndicatorSetString(INDICATOR_SHORTNAME,"DPO ("+(string)DpoPeriod+")");
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| DPO                                                              |
//+------------------------------------------------------------------+

int OnCalculate (const int       rates_total,
                 const int       prev_calculated,
                 const datetime& time[],
                 const double&   open[],
                 const double&   high[],
                 const double&   low[],
                 const double&   close[],
                 const long&     tick_volume[],
                 const long&     volume[],
                 const int&      spread[])
{
   int i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1; 
   
   //
   //
   //
   
   if (dpoc[i]==-1) iCleanPoint(i,rates_total,dpoa,dpob);
   int shift = DpoPeriod/2+1;
   for (; i>=0 && !_StopFlag; i--)
   {
      dpo[i]  = iMA(NULL,0,1,0,MODE_SMA,DpoPrice,i)-iMA(NULL,0,DpoPeriod,0,MODE_SMA,DpoPrice,i+shift);
      switch(ColorOn)
      {
         case cc_zero: dpoc[i] = (i<rates_total-1) ? (dpo[i]>0)        ? 1 : (dpo[i]<0)  ? -1 : dpoc[i+1] : 0;  break;
         default     : dpoc[i] = (i<rates_total-1) ? (dpo[i]>dpo[i+1]) ? 1 : (dpo[i]<dpo[i+1]) ? -1 : dpoc[i+1] : 0; 
      }  
      dpoa[i] = dpob[i] = EMPTY_VALUE; 
      if (dpoc[i] == -1) iPlotPoint(i,rates_total,dpoa,dpob,dpo);    
   }
return(rates_total);
}

//
//
//

void iCleanPoint(int i,int bars,double& first[],double& second[])
{
   if (i>=bars-3) return;
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}
void iPlotPoint(int i,int bars,double& first[],double& second[],double& from[])
{
   if (i>=bars-2) return;
   if (first[i+1] == EMPTY_VALUE)
      if (first[i+2] == EMPTY_VALUE) 
            { first[i]  = from[i]; first[i+1]  = from[i+1]; second[i] = EMPTY_VALUE; }
      else  { second[i] = from[i]; second[i+1] = from[i+1]; first[i]  = EMPTY_VALUE; }
   else     { first[i]  = from[i];                          second[i] = EMPTY_VALUE; }
}