//+------------------------------------------------------------------+
//|                                  Detrended Price Oscillator.mq4  |
//|                                       Ramdass - Conversion only  |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
#property indicator_level1 0
#property indicator_levelcolor Gray
#property indicator_levelstyle 2

#property strict
//----
input int x_prd = 14; // DPO Period
input int MAP = 20; // MA Period
input int MAS = 0; // MA Shift
input ENUM_MA_METHOD MAM = MODE_LWMA; // MA Method
input ENUM_APPLIED_PRICE MAPr = PRICE_CLOSE; // MA Price 
input int CountBarsx = 300; // Bars Lookback

//---- buffers
double dpo[];
double mab[];

int CountBars = CountBarsx;
string mat;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- indicator line
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, dpo);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, mab);
   
   switch(MAM)
   {
     case MODE_SMA: mat = "SMA"; break;
     case MODE_EMA: mat = "EMA"; break;
     case MODE_SMMA: mat = "SMMA"; break;
     case MODE_LWMA: mat = "LWMA";
   }  
   
//---- name for DataWindow and indicator subwindow label
   short_name = "DPO(" + IntegerToString(x_prd) + ")" + "+" + mat+"("+IntegerToString(MAP)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0, short_name);
//----
   if(CountBars >= Bars) 
   CountBars = Bars;
   SetIndexDrawBegin(0, Bars - CountBars + x_prd + 1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| DPO                                                              |
//+------------------------------------------------------------------+
//int start()
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, counted_bars=IndicatorCounted();
   double t_prd;
//----
   if(Bars <= x_prd)  return(0);
//---- initial zero
   if(counted_bars < x_prd)
   {
     for(i = 1; i <= x_prd; i++) dpo[CountBars-i] = 0.0;
   }
//----
   i = CountBars - x_prd - 1;
   t_prd =  x_prd / 2 + 1;
   
//----
   while(i >= 0)
   {
       dpo[i] = Close[i] - iMA(NULL, 0, x_prd, (int) t_prd, MODE_SMA, PRICE_CLOSE, i);
       mab[i]=iMAOnArray(dpo,0,MAP,0,MAM,i);
   
       i--;
   }
  
  /*if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
   int limit=MathMin(rates_total-counted_bars,rates_total-1); 
   for(i =limit; i>=0; i--)
   {
   mab[i]=iMAOnArray(dpo,0,MAP,0,MAM,i);
    
   }*/
 
   return(rates_total);

  // return(0);
  }
//+------------------------------------------------------------------+-------------+