//+------------------------------------------------------------------+
//|                                                        IDWMA.mq4 |
//|                               Copyright © 2014, Gehtsoft USA LLC |
//|                                            http://fxcodebase.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, Gehtsoft USA LLC"
#property link      "http://fxcodebase.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Yellow

extern int Length=14;
extern int Price=0;    // Applied price
                       // 0 - Close
                       // 1 - Open
                       // 2 - High
                       // 3 - Low
                       // 4 - Median
                       // 5 - Typical
                       // 6 - Weighted

double IDWMA[];
double Pr[];

int init()
{
 IndicatorShortName("IDWMA");
 IndicatorDigits(Digits);
 SetIndexStyle(0,DRAW_LINE);
 SetIndexBuffer(0,IDWMA);
 SetIndexStyle(1,DRAW_NONE);
 SetIndexBuffer(1,Pr);

 return(0);
}

int deinit()
{

 return(0);
}

int start()
{
 if(Bars<=3) return(0);
 int ExtCountedBars=IndicatorCounted();
 if (ExtCountedBars<0) return(-1);
 int limit=Bars-2;
 if(ExtCountedBars>2) limit=Bars-ExtCountedBars-1;
 int pos;
 int iW, iD;
 double Ew, Ewx, Ed, w;
 pos=limit;
 while(pos>=0)
 {
  Pr[pos]=iMA(NULL, 0, 1, 0, MODE_SMA, Price, pos);
 
  pos--;
 }
  
 pos=limit;
 while(pos>=0)
 {
  Ew=0.;
  Ewx=0.;
  for (iW=pos;iW<=pos+Length;iW++)
  {
   Ed=0.;
   for (iD=pos;iD<=pos+Length;iD++)
   {
    Ed=Ed+MathAbs(Pr[iW]-Pr[iD]);
   }
   w=(Length-1)/MathMax(Ed, Point);
   Ew=Ew+w;
   Ewx=Ewx+w*Pr[iW];
  }
  if (Ew!=0.)
  {
   IDWMA[pos]=Ewx/Ew;
  }
  else
  {
   IDWMA[pos]=EMPTY_VALUE;
  } 

  pos--;
 } 
 return(0);
}

