//+------------------------------------------------------------------+
//|                                                   ADX Filter.mq4 |
//|                                                              cja |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property  indicator_buffers 3
#property indicator_color2 Green
#property indicator_width2 3
#property indicator_color3 Red
#property indicator_width3 3
#property indicator_maximum 0.01
#property indicator_minimum 0

extern int TimeFrame  = 0;
extern int DMI_Period = 13;
extern int DMI_Price  = 0;
extern int DMI_Level  = 20;  

string TF;
double DMI[];
double DMI_UP[];
double DMI_DN[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
 IndicatorDigits(1);
    
   SetIndexDrawBegin(0,DMI_Period);
   SetIndexDrawBegin(1,DMI_Period);
   SetIndexDrawBegin(2,DMI_Period);
   
   SetIndexBuffer(0,DMI);
   SetIndexBuffer(1,DMI_UP);
   SetIndexBuffer(2,DMI_DN);
   
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   
   SetIndexLabel(0,"DMI");
   SetIndexLabel(1,"");
   SetIndexLabel(2,"");
    if (TimeFrame<Period()) TimeFrame=Period();
   	   	switch(TimeFrame)
	{
		case 1:		TF="M1";  break;
		case 5:		TF="M5";  break;
		case 15:		TF="M15"; break;
		case 30:		TF="M30"; break;
		case 60:		TF="H1";  break;
		case 240:	TF="H4";  break;
		case 1440:	TF="D1";  break;
		case 10080:	TF="W1";  break;
		case 43200:	TF="MN1"; break;
		default:	  {TimeFrame = Period(); init(); return(0);}
	}
	 
	 IndicatorShortName("MTF DMI [ "+TF+" ][ "+DMI_Period+" ]  " );
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
    int limit;
   
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- 
    if (TimeFrame<Period()) TimeFrame=Period();
   for(int i=0; i<limit; i++)
    DMI[i]=iADX(NULL,TimeFrame,DMI_Period,DMI_Price,MODE_PLUSDI,iBarShift(NULL,TimeFrame,Time[i]));
   
   for( i=0; i<limit; i++)
   if(DMI[i]>=DMI_Level){DMI_UP[i]=DMI[i];DMI_DN[i]=EMPTY_VALUE;}
   else if(DMI[i]<DMI_Level){DMI_DN[i]=DMI[i];DMI_UP[i]=EMPTY_VALUE;}  
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+