//+------------------------------------------------------------------+
//|                                                Symbol_Period.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window

extern bool Show_Symbol = false;
extern bool Show_Date_Time_Tf = true;
extern bool Show_Spread = true;
extern bool Show_MaxLot = true;
extern bool Show_ADR = true;
extern color Color=Snow;
extern int FontSize=9;
extern int XDISTANCE =1881;
extern int YDISTANCE =9;
extern int Window = 0;
string Tf ;
int X,Y,Anchor;
ENUM_BASE_CORNER Corner =CORNER_LEFT_UPPER;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
 X = XDISTANCE;
 Y = YDISTANCE;
if(Corner==0) Anchor=ANCHOR_LEFT;
if(Corner==1) Anchor=ANCHOR_RIGHT;
if(Corner==2) Anchor=ANCHOR_LEFT;
if(Corner==3) Anchor=ANCHOR_RIGHT;  
 
if(ObjectFind(0,"INSTR")==0)
 {
 ObjectDelete(0,"INSTR");
 } 
 
if(ObjectFind(0,"INSTR1")==0)
 {
 ObjectDelete(0,"INSTR1");
 } 
 
if(ObjectFind(0,"INSTR2")==0)
 {
 ObjectDelete(0,"INSTR2");
 } 
 
 ObjectCreate(0,"INSTR",OBJ_LABEL,Window,0,0); 
 ObjectCreate(0,"INSTR1",OBJ_LABEL,Window,0,0); 
 ObjectCreate(0,"INSTR2",OBJ_LABEL,Window,0,0); 
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//----
 ObjectDelete("INSTR"); 
 ObjectDelete("INSTR1");
 ObjectDelete("INSTR2");
//----
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
      string AcctCurrency = AccountCurrency();
       
      if (AcctCurrency == "")
          return(0);  
  
 X = XDISTANCE;
 Y = YDISTANCE;  
if(Corner==0) Anchor=ANCHOR_LEFT;
if(Corner==1) Anchor=ANCHOR_RIGHT;
if(Corner==2) Anchor=ANCHOR_LEFT;
if(Corner==3) Anchor=ANCHOR_RIGHT;    
//----
    Tf = StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()),7,3);
 
 int Mode = MarketInfo(NULL,MODE_MARGINCALCMODE);
 string CalcMode ="";
 if(Mode==0) CalcMode = "Forex";
 if(Mode==1) CalcMode = "CFD";
 if(Mode==2) CalcMode = "Futures";
 if(Mode==3) CalcMode = "CFD Indices";
 
 

 string Symbol_Period="";
 if(Show_Symbol) Symbol_Period=CalcMode+" "+Symbol()+" ";
 string Date_Time="";
 if(Show_Date_Time_Tf) Date_Time=Tf+" | "+TimeToStr(TimeCurrent(),TIME_DATE)+" | "+TimeToStr(TimeCurrent(),TIME_SECONDS)+" / "+TimeToStr(TimeLocal(),TIME_SECONDS);
 string Spread="";
 string ADRText="";            
 string strmaxLot="";
 
              string symbol=_Symbol;
              int    ADRper=10;
              double SymPt=MarketInfo(symbol,MODE_POINT);
              double DRToday=(iHigh(symbol,PERIOD_D1,0)-iLow(symbol,PERIOD_D1,0))/SymPt;
              double ADR=dblADR(symbol,ADRper,SymPt);
              double Sp=MarketInfo(symbol,MODE_SPREAD);
              if(Sp==0) Sp=1;
              double ADRbySPREAD=(ADR/Sp)/50;
              if(Show_Spread)
               {
               Spread="Spread: "+IntegerToString(MarketInfo(Symbol(),MODE_SPREAD),0);   
               }
              if(Show_ADR) 
               ADRText="ADR("+IntegerToString(ADRper)+"): "+DoubleToString(ADR,0)+" | DR: "+DoubleToString(DRToday,0)+" | ADR/SP: "+DoubleToString(ADRbySPREAD,1);

              double step     = MarketInfo(Symbol(),MODE_LOTSTEP);
              int    norm     = 0;
                    if (step==1)    norm = 0;
                    if (step==0.1)  norm = 1;
                    if (step==0.01) norm = 2;
              if(Show_MaxLot)
               {
               double maxLot=MaxAvailableLot();
               strmaxLot="MaxAvLot: "+DoubleToString(maxLot,norm)+" | ";
               }
   if(ObjectFind(0,"INSTR")<0)
    {
   ObjectCreate(0,"INSTR",OBJ_LABEL,Window,0,0); 
    }
	ObjectSetInteger(0,"INSTR",OBJPROP_ANCHOR,Anchor);
	ObjectSetInteger(0,"INSTR",OBJPROP_CORNER,Corner);
	ObjectSetText("INSTR",Symbol_Period+Date_Time+" | "+Spread,FontSize, "Berlin Sans FB", Color);
	ObjectSetInteger(0,"INSTR", OBJPROP_XDISTANCE, X);
   ObjectSetInteger(0,"INSTR", OBJPROP_YDISTANCE, Y); 
   ObjectSetInteger(0,"INSTR", OBJPROP_SELECTABLE, false);   
   if(ObjectFind(0,"INSTR1")<0)
    {
   ObjectCreate(0,"INSTR1",OBJ_LABEL,Window,0,0); 
    }
	ObjectSetInteger(0,"INSTR1",OBJPROP_ANCHOR,Anchor);
	ObjectSetInteger(0,"INSTR1",OBJPROP_CORNER,Corner);
	ObjectSetText("INSTR1",ADRText,FontSize, "Berlin Sans FB", Color);
	if(Corner==0||Corner==1)
	 {
	ObjectSetInteger(0,"INSTR1", OBJPROP_XDISTANCE, X+99);
   ObjectSetInteger(0,"INSTR1", OBJPROP_YDISTANCE, Y+FontSize+3);
	 } 
   ObjectSetInteger(0,"INSTR1", OBJPROP_SELECTABLE, false); 
   if(ObjectFind(0,"INSTR2")<0)
    {
   ObjectCreate(0,"INSTR2",OBJ_LABEL,Window,0,0);
    }
	ObjectSetInteger(0,"INSTR2",OBJPROP_ANCHOR,Anchor);
	ObjectSetInteger(0,"INSTR2",OBJPROP_CORNER,Corner);
	ObjectSetText("INSTR2",strmaxLot,FontSize, "Berlin Sans FB", Color);
	ObjectSetInteger(0,"INSTR2", OBJPROP_XDISTANCE, X-18);
   ObjectSetInteger(0,"INSTR2", OBJPROP_YDISTANCE, Y+FontSize+3);
   ObjectSetInteger(0,"INSTR2", OBJPROP_SELECTABLE, false);  
//----
   return(rates_total);
  }
//+------------------------------------------------------------------+
double dblADR(string sym,int per,double pt)
 {
 double result=0;
 int i=0;
 
       for(i=0; i<=per; i++)
        {
        result+=((iHigh(sym,PERIOD_D1,i)-iLow(sym,PERIOD_D1,i))/per)/pt;
        }
 return(result);
 }
//+------------------------------------------------------------------+
double MaxAvailableLot()
 {
  double result=0;
                double step     = MarketInfo(Symbol(),MODE_LOTSTEP);
                int    norm     = 0;
                    if (step==1)    norm = 0;
                    if (step==0.1)  norm = 1;
                    if (step==0.01) norm = 2;
                double Max_available_lot=NormalizeDouble(AccountFreeMargin()/MarketInfo(Symbol(),MODE_MARGINREQUIRED)*step,norm);
                double maxLot   = MarketInfo(Symbol(),MODE_MAXLOT);
                double minLot   = MarketInfo(Symbol(),MODE_MINLOT); 
                if(Max_available_lot>maxLot)
                Max_available_lot=maxLot;
                if(Max_available_lot<minLot)
                Max_available_lot=0;
                result=Max_available_lot;
  return(result);
 }
//+------------------------------------------------------------------+
