#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  LimeGreen
#property indicator_color2  Red
#property indicator_width1  2
#property indicator_width2  2
#property indicator_minimum 0
#property indicator_maximum 1

//Input parameters
extern int Periods = 20;

//Indicator buffers
double CMF[];
double UpH[];
double DnH[];
double trend[];

int init()
{
IndicatorBuffers(4);
SetIndexBuffer(0,UpH);    SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(1,DnH);    SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexBuffer(2,CMF);
SetIndexBuffer(3,trend);
IndicatorShortName( "CMF(" + Periods + ")" );

SetIndexDrawBegin(0,Periods);

return(0);
}

int deinit()
{
return(0);
}

int start()
{
int shift,limit,counted_bars=IndicatorCounted();
   
   if(Bars<=Periods) return(0);
//---- initial zero
   if(counted_bars<1)
      for(int i=1;i<=Periods;i++) CMF[Bars-i]=0.0;
//----
   if ( counted_bars > 0 )  limit=Bars-counted_bars;
   if ( counted_bars ==0 )  limit=Bars-Periods-1; 
   
   for(shift=limit;shift>=0;shift--)
   {	   
   double dN_Sum=0.0;
   double Volume_Sum=0.0;
      for(i=0;i<Periods-1;i++) 
      {	
      Volume_Sum+=Volume[shift+i];
      if((High[shift+i]-Low[shift+i])>0)
      dN_Sum += Volume[shift+i]*(Close[shift+i]-Open[shift+i])/(High[shift+i]-Low[shift+i]);
      }
   CMF[shift]=dN_Sum/Volume_Sum;
   UpH[shift]   = EMPTY_VALUE;
   DnH[shift]   = EMPTY_VALUE;
   trend[shift] = trend[shift+1];
   if (CMF[shift]>0)      trend[shift] =  1;
   if (CMF[shift]<0)      trend[shift] =-1;
   if (trend[shift] == 1) UpH[shift] = 1;
   if (trend[shift] ==-1) DnH[shift] = 1;
   }
return(0);
}