//+------------------------------------------------------------------------------------------+
//|                                                                         DailyData.mq4    |
//|                                                                                mladen    |
//|   Version: DailyData_v01.mq4   by okfar (forex-tsd forum nick)      12-Apr-2009          |
//|         - add changable fontsize                                                         |
//|         - add changable distance from top                                                |
//|         - add ADX indicator (timeframe: MN1 - Monthly, W1-Weekly, D1-Daily, H4-4 hour)   | 
//|                                                                                          |
//|   Version: DailyData_v02.mq4   by okfar (forex-tsd forum nick)      14-Apr-2009          |
//|         - redefine distance from top (independant from fontsize)                         |
//|         - simplifying the addition unlimit of the indicators and value of info (numeric) | 
//|                                                                                          |
//|   Version: DailyData_v03.mq4   by okfar (forex-tsd forum nick)  15-Apr-2009              |
//|         - add statistic box                                                              |
//|         - add OpenTime (start time for daily data if typHist=0;)                         |
//|   Version: DailyData_v03.mq4   by okfar (forex-tsd forum nick)  3-Aug-2009               |
//|         - add option for statistic box (pips, %)                                         |                     |
//|                                                                                          |
//+------------------------------------------------------------------------------------------+


#property copyright "mladen"
#property link      "mladenfx@gmail.com"


#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  Green
#property indicator_color2  Maroon
#property indicator_color3  Lime
#property indicator_color4  Red
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  2
#property indicator_width4  2


extern color zoneColor      = C'30,33,36';
extern int   CandleShift    = 8;
extern bool  ShowInfo       = True;
extern bool  ShowSwap       = True;
extern bool  ShowBackground = True;
extern bool  ShowBar        = True;

extern bool    ShowInd      = False;           //Show indicators: ON = True, OFF = False 
extern bool    ShowStatist  = True;           //Show statistic: ON = True, OFF = False
extern bool    StatInPips   = True;           //Show statistic in : pips = True, % = False   
extern int y   = 35;                          //verical distance from top                   
extern int fontsize=  12;                     //Font size
extern int typHist = 1;                       //Typ of daily statistic: 0 - current day from OpenTime  1- last 24 hours (0=current year, 1=period of one year)
extern int OpenTime = 1;                      //Open time (terminal time) = start statistic for typHist=0

//-------------
// Indicators (info) names - number names = number indicators != limit 
// CHANGE THIS!!!      
string indtxt[]={"EMA5-H1:", "EMA21-H1:","EMA50-H1:", "Stoh1-14,3,3-H1:","Stoh1-Main-Signal-H1:", "Stoh2-8,3,3-H1:" };
//--------------


double DayHigh[];
double DayLow[];
double DayOpen[];
double DayClose[];
double prevCurr =-1;
int    DataPeriod;
int    DataBar,
       nDataBar,
       sDataBar;
int    dd, dw, dm; 
int cind;
string dataString;
string indNames = "DailyData";



double dy=0,
       dx=0;
int y2;
int numind;
int boxnum;
int statbox=10;
int dlt;

 
int digit[];

double tmp;
double value[];
string valuetxt[][];


//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//---- indicators

void init()
{
   SetIndexBuffer(0, DayHigh);
   SetIndexBuffer(1, DayLow);
   SetIndexBuffer(2, DayClose);
   SetIndexBuffer(3, DayOpen);
      if (ShowBar)
         for (int i=0;i<4;i++)
            {
               SetIndexStyle(i,DRAW_HISTOGRAM);
               SetIndexShift(i,CandleShift);
               SetIndexLabel(i,"Daily data");
            }            
      else  for (i=0;i<4;i++) SetIndexStyle(i,DRAW_NONE);
            


   switch(Period())
   {
      case PERIOD_MN1:
      case PERIOD_W1: {
         DataPeriod = PERIOD_MN1; 
         //nDataBar = PERIOD_H1;
         nDataBar = PERIOD_D1;
         dm=1;
         dataString = "Monath";
         
      } break;
      case PERIOD_D1: {  
         DataPeriod = PERIOD_W1;
         //nDataBar = PERIOD_H1;
         nDataBar = PERIOD_D1;
         dw=1;  
         dataString = "Week";
         
      }  break;  
      default: {
         DataPeriod = PERIOD_D1;
         nDataBar = PERIOD_H1;
         dd=1;
         dataString = "Day";
         
     } break;
   }
   prevCurr = -1;
   
   dy=(fontsize/10.0);
   dx=(fontsize/15.0);
   
   numind = ArraySize( indtxt); 
   ArrayResize( value, numind); 
   ArrayResize( digit, numind);
   for (i=0; i<numind ;i++) 
      digit[i]=2;
   
 
   return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   for (int counter=-1; counter<(19+2*numind+statbox); counter++) ObjectDelete(indNames+counter);
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+


int start()
{
  
//----
      
double LoDAILY;
double HiDAILY;
//-----------------------------------
// Indicators (info) calculation)
// value[0] -> first name  of indtxt[]
// CHANGE THIS!!!!!
   

   value[0] = iMA(Symbol(), PERIOD_H1, 5,  0, MODE_EMA, PRICE_CLOSE, 0);
   value[1] = iMA(Symbol(), PERIOD_H1, 21,  0, MODE_EMA, PRICE_CLOSE, 0);
   value[2] = iMA(Symbol(), PERIOD_H1, 50,  0, MODE_SMMA, PRICE_CLOSE, 0);
   value[3] = iStochastic(Symbol(), PERIOD_H1, 14, 3, 3, MODE_SMA, 1, MODE_MAIN, 0);
   value[4] = iStochastic(Symbol(), PERIOD_H1, 14, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) - iStochastic(Symbol(), PERIOD_H1, 14, 3, 3, MODE_SMA, 1, MODE_SIGNAL, 0);
   value[5] = iStochastic(Symbol(), PERIOD_H1, 8, 3, 3, MODE_SMA, 1, MODE_MAIN, 0);
   
  
   
 
//Number of dispayed digits for indicators (info)        indtxt[] -> value[0] -> digit[0]  
// default is 2 for all data other data
   
   digit[0]=4;
   digit[1]=4;
   digit[2]=4;
   
   digit[4]=3;
   
   
//-----------------------------------------  
  
  
  
  
  fDailyData_v01();


//----
   return(0);
}
//+------------------------------------------------------------------+



void fDailyData_v01()
{
 
   
   int    digits   = MarketInfo(Symbol(),MODE_DIGITS);
   double modifier = 1;
   
   if (digits==3 || digits==5) modifier = 10.0;
   else if (ObjectFind(indNames+"16"+numind+statbox) != -1)
      ObjectDelete(indNames+"26"+numind+statbox);
      
      //
      //
      //
      //
      //
            
   RefreshRates();
   DataBar = iBarShift(NULL, nDataBar, StartBar(0, dm, dw, dd, typHist, OpenTime), False);
   //DataBar  = iBarShift(NULL,DataPeriod,Time[0]);
   double HiDAILY  = iHigh(NULL, nDataBar, iHighest(NULL,nDataBar,MODE_HIGH, DataBar+1,0));
   double LoDAILY  = iLow(NULL, nDataBar, iLowest(NULL,nDataBar,MODE_LOW, DataBar+1,0)); 
   double current  = iClose(NULL,nDataBar,0);
   double range    = NormalizeDouble((HiDAILY-LoDAILY)/(Point*modifier), 0);
   double change   = (current-iOpen(NULL,nDataBar,DataBar))/(Point*modifier);
   int    limit,i;
      
  
  
      //
      //
      //
      //
      //
//+------
   if (ShowInfo)   {
      if (ObjectFind(indNames+"1") ==-1) {
         objectCreate("1",10*dx,10*dy+y,"-------------------------------------",fontsize,NULL);  
         objectCreate("3",60*dx,32*dy+y,"distance from high : ",fontsize);
         objectCreate("4",60*dx,44*dy+y,"distance from low : " ,fontsize);
         objectCreate("5",10*dx,56*dy+y,"-------------------------------------",fontsize,NULL);
         if (ShowSwap) {
            objectCreate("2",60*dx,20*dy+y,dataString+" change/range : ",fontsize);
            objectCreate("6",60*dx,64*dy+y,"swap long : "         ,fontsize);
            objectCreate("7",60*dx,76*dy+y,"swap short : "        ,fontsize);
            objectCreate("8",10*dx,84*dy+y,"--------------------------------------",fontsize,NULL);  
         }
         else  {
            objectCreate("2",60*dx,20*dy+y,dataString+" range : "       ,fontsize);
            objectCreate("6",60*dx,64*dy+y,"distance from open : ",fontsize);
            objectCreate("8",10*dx,76*dy+y,"---------------------------------------",fontsize,NULL);  
         }                     
      }

         //
         //
         //
         //
         //
//+------
      if (prevCurr != current) {
         prevCurr = current;
         double currFromHi = (HiDAILY-current)/(Point*modifier);
         double currFromLo = (current-LoDAILY)/(Point*modifier);

                  //
                  //
                  //
                  //
                  //
//+------                  
         objectCreate("0",128*dx,0*dy+y,Symbol(),fontsize,"Arial bold",Snow);  
         if (ShowSwap)
            objectCreate("10",10*dx,20*dy+y,DoubleToStr(change,0)+"/"+DoubleToStr(range,0)    ,fontsize,"Arial bold",Gold);
         else  
            objectCreate("10",10*dx,20*dy+y,                          DoubleToStr(range,0)    ,fontsize,"Arial bold",Gold);
         objectCreate("11",10*dx,32*dy+y,DoubleToStr(currFromHi,0)                         ,fontsize,"Arial bold",Snow);
         objectCreate("12",10*dx,44*dy+y,DoubleToStr(currFromLo,0)                         ,fontsize,"Arial bold",Snow);
         if (ShowSwap)  {
            objectCreate("13",10*dx,64*dy+y,DoubleToStr(MarketInfo(Symbol(),MODE_SWAPLONG),2) ,fontsize,"Arial bold",Snow);
            objectCreate("14",10*dx,76*dy+y,DoubleToStr(MarketInfo(Symbol(),MODE_SWAPSHORT),2),fontsize,"Arial bold",Snow);
         }
         else {
            if (change<0)
               objectCreate("13",10*dx,64*dy+y,DoubleToStr(change,0) ,fontsize,"Arial bold",Red);
            else  objectCreate("13",10*dx,64*dy+y,DoubleToStr(change,0) ,fontsize,"Arial bold",Gold);
         }
         string currentValue = DoubleToStr(current,digits);                  
         objectCreate("15",10*dx, 0*dy+y,currentValue,fontsize+2,"Arial bold",Snow);  
         if (modifier !=1)
            objectCreate("16",10*dx, 0*dy+y,StringSubstr(currentValue,StringLen(currentValue)-1),fontsize+2,"Arial bold",Gold);  

//+------         
         if (ShowSwap)
               y2=84;
            else 
               y2=76;
         boxnum=16;
//+------
         if (ShowInd) {
 //           if (ShowSwap)
 //              y2=84;
  //          else 
  //             y2=76;
                  
            for (i=0; i<numind ;i++) {
               valuetxt[i][0] = DoubleToStr(value[i], digit[i]); 
               if (value[i] < 0)
                  cind= Red;
               else
                  cind = Snow;             
               y2 += 12;
               boxnum +=2;              
               objectCreate( boxnum-1,70*dx,y2*dy+y,indtxt[i],fontsize);
               objectCreate( boxnum,10*dx,y2*dy+y, valuetxt[i][0], fontsize,  "Arial bold",cind);  
            }
            
         }

//+------
         if (ShowStatist) {
           
            y2 += 12;
            objectCreate( boxnum+1,10*dx,y2*dy+y,"=======================" ,fontsize);
            y2 += 12;
            if (StatInPips == True)
               objectCreate( boxnum+2,20*dx,y2*dy+y,"Change from:            (pips)" ,fontsize);
            else
               objectCreate( boxnum+2,20*dx,y2*dy+y,"Change from:            (%)" ,fontsize);
            
            y2 += 12;
            sDataBar = iBarShift(NULL, nDataBar, StartBar(0, 0, 0, 1, typHist, OpenTime), False);             
            
            
            
            if (100*(current/iOpen(Symbol(),nDataBar,sDataBar)-1) < 0)
                  cind= Red;
               else
                  cind = Snow; 
            objectCreate( boxnum+3,70*dx,y2*dy+y,TimeToStr(iTime(NULL,nDataBar,sDataBar),TIME_DATE|TIME_MINUTES) ,fontsize);
            if (StatInPips == True)
               objectCreate( boxnum+4,10*dx,y2*dy+y,DoubleToStr((current-iOpen(Symbol(),nDataBar,sDataBar))/(Point*modifier),0),fontsize,"Arial bold",cind); 
            else
               objectCreate( boxnum+4,10*dx,y2*dy+y,DoubleToStr(100*(current/iOpen(Symbol(),nDataBar,sDataBar)-1),3),fontsize,"Arial bold",cind); 

            
            y2 += 12;
            sDataBar = iBarShift(NULL, nDataBar, StartBar(0, 0, 1, 0, typHist, OpenTime), False);             
            if (100*(current/iOpen(Symbol(),nDataBar,sDataBar)-1) < 0)
                  cind= Red;
               else
                  cind = Snow; 
            objectCreate( boxnum+5,70*dx,y2*dy+y,TimeToStr(iTime(NULL,nDataBar,sDataBar),TIME_DATE|TIME_MINUTES) ,fontsize);
            if (StatInPips == True)
               objectCreate( boxnum+6,10*dx,y2*dy+y,DoubleToStr((current-iOpen(Symbol(),nDataBar,sDataBar))/(Point*modifier),0),fontsize,"Arial bold",cind); 
            else 
               objectCreate( boxnum+6,10*dx,y2*dy+y,DoubleToStr(100*(current/iOpen(Symbol(),nDataBar,sDataBar)-1),3),fontsize,"Arial bold",cind); 
            
            y2 += 12;
            sDataBar = iBarShift(NULL, PERIOD_D1, StartBar(0, 1, 0, 0, typHist, OpenTime), False);             
            if (100*(current/iOpen(Symbol(),PERIOD_D1,sDataBar)-1) < 0)
                  cind= Red;
               else
                  cind = Snow; 
            objectCreate( boxnum+7,70*dx,y2*dy+y,TimeToStr(iTime(NULL,PERIOD_D1,sDataBar),TIME_DATE|TIME_MINUTES) ,fontsize);
            if (StatInPips == True)
               objectCreate( boxnum+8,10*dx,y2*dy+y,DoubleToStr((current-iOpen(Symbol(),PERIOD_D1,sDataBar))/(Point*modifier),0),fontsize,"Arial bold",cind); 
            else 
               objectCreate( boxnum+8,10*dx,y2*dy+y,DoubleToStr(100*(current/iOpen(Symbol(),PERIOD_D1,sDataBar)-1),3),fontsize,"Arial bold",cind);

            
            y2 += 12;
            sDataBar = iBarShift(NULL, PERIOD_D1, StartBar(0, 3, 0, 0, typHist, OpenTime), False);             
            if (100*(current/iOpen(Symbol(),PERIOD_D1,sDataBar)-1) < 0)
                  cind= Red;
               else
                  cind = Snow; 
            objectCreate( boxnum+9,70*dx,y2*dy+y,TimeToStr(iTime(NULL,PERIOD_D1,sDataBar),TIME_DATE|TIME_MINUTES) ,fontsize);
            if (StatInPips == True)
               objectCreate( boxnum+10,10*dx,y2*dy+y,DoubleToStr((current-iOpen(Symbol(),PERIOD_D1,sDataBar))/(Point*modifier),0),fontsize,"Arial bold",cind); 
            else 
               objectCreate( boxnum+10,10*dx,y2*dy+y,DoubleToStr(100*(current/iOpen(Symbol(),PERIOD_D1,sDataBar)-1),3),fontsize,"Arial bold",cind);
            
            y2 += 12;
            sDataBar = iBarShift(NULL, PERIOD_D1, StartBar(1, 0, 0, 0, typHist, OpenTime), False);             
            if (100*(current/iOpen(Symbol(),PERIOD_D1,sDataBar)-1) < 0)
                  cind= Red;
               else
                  cind = Snow; 
            objectCreate( boxnum+11,70*dx,y2*dy+y,TimeToStr(iTime(NULL,PERIOD_D1,sDataBar),TIME_DATE|TIME_MINUTES) ,fontsize);
            if (StatInPips == True)
               objectCreate( boxnum+12,10*dx,y2*dy+y,DoubleToStr((current-iOpen(Symbol(),PERIOD_D1,sDataBar))/(Point*modifier),0),fontsize,"Arial bold",cind); 
            else 
               objectCreate( boxnum+12,10*dx,y2*dy+y,DoubleToStr(100*(current/iOpen(Symbol(),PERIOD_D1,sDataBar)-1),3),fontsize,"Arial bold",cind);
            
         }   
      }   
   }           
   //setBarIndicator(change,HiDAILY,LoDAILY,iOpen(NULL,DataPeriod,DataBar),current);
   setBarIndicator(change,HiDAILY,LoDAILY,iOpen(NULL,nDataBar,DataBar),current);
   for(i=0,limit=Bars-1;i<4;i++) SetIndexDrawBegin(i,limit); 
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+  
//
//
//
//
//

void setBarIndicator(double change, double hi,double low, double open, double close)
{
   string name  = indNames+"-1";
   int    shift = 0;
   
   if (change>0) { DayHigh[shift]  = hi;  DayLow[shift] = low; }            
   else          { DayHigh[shift]  = low; DayLow[shift] = hi;  }            
                   DayOpen[shift]  = open;
                   DayClose[shift] = close;

   if (ShowBackground)
      {                         
         if (ObjectFind(name) == -1)
             ObjectCreate(name,OBJ_RECTANGLE,0,0,0);
             //ObjectSet(name,OBJPROP_TIME1,iTime(NULL,DataPeriod,DataBar));
             ObjectSet(name,OBJPROP_TIME1,iTime(NULL,nDataBar,DataBar));
             ObjectSet(name,OBJPROP_TIME2,iTime(NULL,         0,0));
             ObjectSet(name,OBJPROP_PRICE1,hi);
             ObjectSet(name,OBJPROP_PRICE2,low);
             ObjectSet(name,OBJPROP_COLOR,zoneColor);
      }             
}

//
//
//
//
//



//
//
//
//
//

void objectCreate(string name,int x,int y,string text="-",int size=10,
                  string font="Arial",color colour=DimGray,int window = 0)
{
   if (ObjectFind(indNames+name) == -1)
   {
      ObjectCreate(indNames+name,OBJ_LABEL,window,0,0);
         ObjectSet(indNames+name,OBJPROP_CORNER,1);
         ObjectSet(indNames+name,OBJPROP_XDISTANCE,x);
         ObjectSet(indNames+name,OBJPROP_YDISTANCE,y);
   }               
   ObjectSetText(indNames+name,text,size,font,colour);
}


datetime StartBar(int dye, int dmo, int dwe, int dda, int typ, int ot)
{
   int ye,mo,da,ho;
   datetime ct;
     
      
   ct=TimeCurrent();
   switch (typ) {
      case 0: {
         ye = TimeYear(ct);
         mo = TimeMonth(ct);
         da = TimeDay(ct);
         ho = TimeHour(ct);
    //     ho -= ot;
    //     if (ho < 0)
   //         ho +=24;
         if (dda > 0)
            ho=0;
         if (dwe > 0) {
            da -= (TimeDayOfWeek(TimeCurrent())-1);
            ho=0;
            //da -= DayOfWeek();      
         }
         if (dmo > 0) {
           da=1;
           ho=0;
         }
         if (dye > 0) {
           da=1;
           mo=1;
           // da  -= DayOfYear()+1 ;
           ho=0;
         
         }
       if (ot != 0) {
           ct = StrToTime(da+"."+mo+"."+ye+" "+ho);
           if (ot > TimeHour(TimeLocal()))
               ct = ct + (ot-24)*3600;     
           else 
             ct += (ot)*3600;    
           //cd -=  (ot)*3600
           ye = TimeYear(ct);
           mo = TimeMonth(ct);
           da = TimeDay(ct);
           ho = TimeHour(ct);
        }
        
      } break;
      
      case 1: {      
         ct = TimeCurrent()+3600;
         if (dda > 0) 
            ct -= 60*60*(24)*dda;
         if (dwe > 0)
            ct -= 60*60*24*(7)*dwe;   
      
         ye = TimeYear(ct);
         mo = TimeMonth(ct);
         da = TimeDay(ct);
         ho = TimeHour(ct);
      
         if (dmo > 0) {
            mo -= dmo;
            while (mo < 0) {
               mo +=12;
               ye -= 1;
            }
         }
         if (dye > 0) {
            ye -= dye;
         }
     } break;
     
     default: {
         ye = Year();
          mo = Month();
          da = Day();
         ho = Hour();   
     } break;
  }   
 // return (StrToTime(StringConcatenate(/*"D'",*/da,".",mo,".",ye," ",ho/*,"'"*/)));
  return (StrToTime(da+"."+mo+"."+ye+" "+ho));   
}  