//+------------------------------------------------------------------+
//|                                    ClusterDelta_Accumulation.mq4 |
//|                                         Copyright 2013, nicholas |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, nicholas"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 LightSeaGreen

extern int    ClusterDeltaBarsLimit = 100;
extern string Comment_Instrument = "--- ClusterDelta_Volume Parameters ";
extern string Instrument = "AUTO";
extern int Update_in_sec = 14;
extern string MetaTrader_GMT = "AUTO";

//---- buffers
double Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("ClusterDelta A/D");
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Buffer);
   //SetIndexEmptyValue(0,0.0);
   Print("ClusterDelta_Accumulation test from nicholas");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| ClusterDleta Accumulation/Distribution                                        |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();   
   double ClusterDeltaVolume;
//----      
   
   for(int i=Bars-counted_bars - 1; i>=0; i--)
   {
      double high =High[i];
      double low  =Low[i];
      double open =Open[i];
      double close=Close[i];
      Buffer[i]=(close-low)-(high-close);
      if(Buffer[i]!=0)
      {
         double diff=high-low;
         if(0==diff)
            Buffer[i]=0;
         else
         {
            Buffer[i]/=diff;  
            if(!ClusterDeltaBarsLimit || i<=ClusterDeltaBarsLimit)
               ClusterDeltaVolume = iCustom(NULL, 0, "ClusterDelta_Volume", Instrument, Update_in_sec, MetaTrader_GMT, 0, i);
            else
               ClusterDeltaVolume = 0;
                           
            Buffer[i]*=ClusterDeltaVolume;            
         }
      }
      if(i<Bars-1) 
         Buffer[i]+=Buffer[i+1];      
   }
//----
   return(0);
}
//+------------------------------------------------------------------+