//+------------------------------------------------------------------+
//|                                                         xADR.mq4 |
//|                                                        by xecret |
//|                                                                  |
//|  Updated on 07/June/2015 by JMMAC. Added the option to refresh   |
//|  every X minuts and not every tick -> speeds things up.          |
//|                                                                  |
//|  Credits for this indicator to the original creator.              |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "xecret"
#property indicator_chart_window

extern bool		ExcludeSundayData	= true;
extern double	RoomLimitRatio		= 0.2;
extern color	TradeableColor		= Lime;
extern color	UntradeableColor	= Red;
extern color	OtherColor			= Yellow;
extern int		DisplayPosition		= 2;
extern string	SpeedWarning		= "Use refresh interval >0 min to prevent slow speed";
extern int		RefreshEveryXMins	= 1;

int            ATR1_Prd             = 5;
int            ATR2_Prd             = 25;

double         point;
int            LastBars0 = 0;
int            DisplayCorner;
int            x,y;
datetime       prev_time;
string         ccy;



/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
int init(){

  if (RefreshEveryXMins > 240)                             RefreshEveryXMins = 240;
  if (RefreshEveryXMins > 60 && RefreshEveryXMins < 240)   RefreshEveryXMins = 60;
  if (RefreshEveryXMins > 30 && RefreshEveryXMins < 60)    RefreshEveryXMins = 30;
  if (RefreshEveryXMins > 15 && RefreshEveryXMins < 30)    RefreshEveryXMins = 15;
  if (RefreshEveryXMins > 5  && RefreshEveryXMins < 15)    RefreshEveryXMins = 5;
  if (RefreshEveryXMins > 1  && RefreshEveryXMins < 5)     RefreshEveryXMins = 1;

  point=Point;
  if( Digits==3 || Digits==5 ) point=point*10;
  
  if (DisplayPosition==0) {DisplayCorner=0;x=0;y=0;} else {DisplayCorner=2;x=-215;y=1;}
    
  ObjectCreate("xADR2", OBJ_LABEL, 0, 0, 0);
  ObjectSet("xADR2", OBJPROP_CORNER, DisplayCorner);
  ObjectSet("xADR2", OBJPROP_XDISTANCE, 215+x);
  ObjectSet("xADR2", OBJPROP_YDISTANCE, y);
  
  ObjectCreate("xADR3", OBJ_LABEL, 0, 0, 0);
  ObjectSet("xADR3", OBJPROP_CORNER, DisplayCorner);
  ObjectSet("xADR3", OBJPROP_XDISTANCE, 215+x);
  ObjectSet("xADR3", OBJPROP_YDISTANCE, 25+y);
  
  ObjectCreate("xADR4", OBJ_LABEL, 0, 0, 0);
  ObjectSet("xADR4", OBJPROP_CORNER, DisplayCorner);
  ObjectSet("xADR4", OBJPROP_XDISTANCE, 310+x);
  ObjectSet("xADR4", OBJPROP_YDISTANCE, 25+y);
  
  ObjectCreate("xADR5", OBJ_LABEL, 0, 0, 0);
  ObjectSet("xADR5", OBJPROP_CORNER, DisplayCorner);
  ObjectSet("xADR5", OBJPROP_XDISTANCE, 410+x);
  ObjectSet("xADR5", OBJPROP_YDISTANCE, 25+y);
  return(0);
}



/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
int deinit(){
	del_obj();
	return(0);
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
int start(){
	if (RefreshEveryXMins == 0) {
		//del_obj();
		plot_obj();    
	}
	else {
		if(prev_time != iTime(ccy,RefreshEveryXMins,0))  {
			//del_obj();
			plot_obj();
			prev_time = iTime(ccy,RefreshEveryXMins,0);
		}     
	}
	return(0);
}
//----------------------------------------------------------------------------



/*----------------------------------------------------------------------------
 Deletes objects
----------------------------------------------------------------------------*/
void del_obj()   {
	ObjectDelete("xADR1");
	ObjectDelete("xADR2");
	ObjectDelete("xADR3");
	ObjectDelete("xADR4");
	ObjectDelete("xADR5");
}
//----------------------------------------------------------------------------



/*----------------------------------------------------------------------------
 Draw objects
----------------------------------------------------------------------------*/
void plot_obj()   {
  string text; 
  color Color;
  int Bars0=Bars;
  
  if(Bars0>LastBars0){
  
    //ATR Part ==========================================================
    int atr1,atr2;
    atr1=MathRound(iATR(NULL,0,ATR1_Prd,1)/point);
    atr2=MathRound(iATR(NULL,0,ATR2_Prd,1)/point);
    text="ATR" + ATR1_Prd + "=" + atr1 + "  ATR" + ATR2_Prd + "=" + atr2;
    if (atr1<atr2) Color=UntradeableColor; else Color=TradeableColor; 
    ObjectSetText("xADR1",text,10, "Arial", Color);
  
    //ADR Part ==========================================================
    int n=1;  
    static int adr1,adr5,adr10,adr20,adr;
    double s=0.0;    
    for(int i=1;i<=20;i++){
      while(ExcludeSundayData && TimeDayOfWeek(iTime(NULL,PERIOD_D1,n))==0) n++;
      s=s + ( iHigh(NULL,PERIOD_D1,n) - iLow(NULL,PERIOD_D1,n) )/point;
      if(i==1) adr1=MathRound(s);
      if(i==5) adr5=MathRound(s/5);
      if(i==10) adr10=MathRound(s/10);
      if(i==20) adr20=MathRound(s/20);
      n++;  
    }  
    adr  = MathRound((adr1+adr5+adr10+adr20)/4.0);
    text="Y="+adr1+"  ADR="+adr+"  5d="+adr5+"  10d="+adr10+"  20d="+adr20;
    ObjectSetText("xADR2",text,8, "Arial Black", OtherColor);
    LastBars0=Bars0;
  }
  
  //Today's Range and Room ============================================  
  int t,RmUp,RmDn,RmLmt;
  double low0  =  iLow(NULL,PERIOD_D1,0);
  double high0 =  iHigh(NULL,PERIOD_D1,0);
  t = MathRound((high0 - low0)/point);  
  RmUp =  MathRound(adr - (Bid - low0)/point);
  RmDn =  MathRound(adr - (high0 - Bid)/point);
  RmLmt =  MathRound(adr*RoomLimitRatio);
  
  text="RmUp:"+RmUp;
  if (RmUp<RmLmt) Color=UntradeableColor; else Color=TradeableColor;
  ObjectSetText("xADR3",text,10, "Arial Black", Color);
  
  text="RmDn:"+RmDn;
  if (RmDn<RmLmt) Color=UntradeableColor; else Color=TradeableColor;
  ObjectSetText("xADR4",text,10, "Arial Black", Color);
  
  text="T="+t;
  ObjectSetText("xADR5",text,10, "Arial Black", OtherColor);  
  
  //return(0);
}
//----------------------------------------------------------------------------
