//+------------------------------------------------------------------+
//|                                          ay-RollingInTheDeep.mq4 |
//|                         CopyRight © 2012, ahmad.yani@hotmail.com |
//|                                          http://eyesfx.web44.net |
//+------------------------------------------------------------------+
#property copyright "CopyRight © 2012, ahmad.yani@hotmail.com"
#property link      "http://eyesfx.web44.net"

#property indicator_chart_window
//+------------------------------------------------------------------+
#define ID_D1       0
#define ID_W1       1
#define ID_MN1      2

#define ID_OPEN     0
#define ID_TIME     1
//+------------------------------------------------------------------+
extern color      ClrPivotD1        = DarkGreen;
extern color      ClrPivotW1        = RoyalBlue;
extern color      ClrPivotMN1       = Indigo;

extern color      ClrOpenLineMon    = Blue;
extern color      ClrOpenLineTue    = Teal;
extern color      ClrOpenLineWed    = Green;
extern color      ClrOpenLineThu    = Chocolate;
extern color      ClrOpenLineFri    = DeepPink;

extern color      ClrPrevHi         = Teal;
extern color      ClrPrevLo         = Maroon;

extern color      ClrDayOdd         = Gainsboro;
extern color      ClrDayEven        = White;
//+------------------------------------------------------------------+


int    giaTF[3]      = {PERIOD_D1, PERIOD_W1, PERIOD_MN1};
int    giCurrDOW     = -1;                                                       //current day of week   
int    giShift[3]    = {1, 1, 1};
double gdaPivot[3];                                                     //hold pivot price for d1, w1, mn1
double gdaD1Open[6][2];
string gsaDOW[6]     = {"SUN", "MON", "TUE", "WED", "THU", "FRI"};
string gsaDOWL[6]    = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
string gsaTF[3]      = {"Daily", "Weekly", "Monthly"};
string gsPref        = "ritd.";

color  gcaPvt[3];// = {DarkGreen, RoyalBlue, Indigo};
color  gcaDOW[6] = {CLR_NONE};


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   gcaPvt[ID_D1 ]  = ClrPivotD1;
   gcaPvt[ID_W1 ]  = ClrPivotW1;
   gcaPvt[ID_MN1]  = ClrPivotMN1;
   
   gcaDOW[1] = ClrOpenLineMon;
   gcaDOW[2] = ClrOpenLineTue;
   gcaDOW[3] = ClrOpenLineWed;
   gcaDOW[4] = ClrOpenLineThu;
   gcaDOW[5] = ClrOpenLineFri;
   
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   delObjs();
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int currdow, itf, iday, idow;
   
   currdow = TimeDayOfWeek(iTime(NULL, giaTF[ID_D1], 0));
   
   //only call display if day of week not changed
   if (currdow == giCurrDOW)
   {
      display();
      return(0);
   }
   
   giCurrDOW = currdow;
   
   if ( TimeDayOfWeek(iTime(NULL, giaTF[ID_D1], 1)) == 0 ) giShift[ID_D1] = 2;
            
   for (itf=ID_D1; itf<=ID_MN1; itf++)
   {
      gdaPivot[itf] = ( iHigh (NULL, giaTF[itf], giShift[itf])
                      + iLow  (NULL, giaTF[itf], giShift[itf])
                      + iClose(NULL, giaTF[itf], giShift[itf])
                      ) / 3;   
   }
         
   
   for (idow=giCurrDOW, iday=0; idow>0; idow--, iday++)
   {
      gdaD1Open[idow][ID_OPEN] = iOpen(NULL, giaTF[ID_D1], iday);
      gdaD1Open[idow][ID_TIME] = iTime(NULL, giaTF[ID_D1], iday);      
   }
   
   display();
   
   //----
   return(0);
}

void display()
{
   int itf, idow;
     
   for (itf=ID_D1; itf<=ID_MN1; itf++)
   {
      createHl  ("pvt."+itf, gdaPivot[itf], gcaPvt[itf], STYLE_DASH, 1, false);
      createText("pvt."+itf+".name"
         , gdaD1Open[giCurrDOW][ID_TIME] + 0.5*(PERIOD_D1*60)
         , gdaPivot[itf]
         , gsaTF[itf] + " Pivot"
         , 8, "Tahoma Bold"
         , gcaPvt[itf])
         ;      
      
   }      
   
   for (idow=giCurrDOW; idow>0; idow--)
   {
     
      createTl("open."+idow
      , gdaD1Open[idow][ID_TIME]               , gdaD1Open[idow][ID_OPEN]
      , gdaD1Open[idow][ID_TIME]+(PERIOD_D1*60), gdaD1Open[idow][ID_OPEN]
      , gcaDOW[idow]
      , STYLE_SOLID, 1, false, true);
     
      createText("open."+idow+".price"
         , gdaD1Open[giCurrDOW][ID_TIME] + 2*(PERIOD_D1*60)
         , gdaD1Open[idow][ID_OPEN], gsaDOWL[idow] + "-" + DoubleToStr(gdaD1Open[idow][ID_OPEN], Digits)
         , 8, "Tahoma Bold", gcaDOW[idow] )
         ;     
                 
     color clrday = ClrDayOdd;
     if (MathMod(idow, 2) != 0)
     {
         createRect("daybg."+idow
            , High[iHighest(NULL, 0, MODE_HIGH)], gdaD1Open[idow][ID_TIME]
            , 0.0000                            , gdaD1Open[idow][ID_TIME]+(PERIOD_D1*60)
            , ClrDayOdd);   
            
         clrday =   ClrDayEven;
      }
      
      createText("dayname."+idow
         , gdaD1Open[idow][ID_TIME] + 0.5*(PERIOD_D1*60)
         , Low[iLowest(NULL, 0, MODE_LOW, WindowFirstVisibleBar())] + 0.75*iATR(NULL, 0, WindowFirstVisibleBar(),1)
         , gsaDOW[idow] 
         , 24, "Tahoma Bold", clrday )
         ;  
      
   }   
   
   //pref day hilo
   double  hi = iHigh (NULL, giaTF[ID_D1], giShift[ID_D1])
         , lo = iLow  (NULL, giaTF[ID_D1], giShift[ID_D1])
         ;
   datetime t1 = iTime (NULL, giaTF[ID_D1], giShift[ID_D1])
          , t2 = gdaD1Open[giCurrDOW][ID_TIME]+(PERIOD_D1*60)
          ;
   createTl("prevhi", t1, hi, t2, hi, ClrPrevHi, STYLE_SOLID, 3, false, false); 
   createText("prevhi.price"
      , t2 + 3*(Period()*60)
      , hi, "yday hi " + DoubleToStr(hi, Digits)
      , 8, "Tahoma Bold", ClrPrevHi )
      ;      
      
   createTl("prevlo", t1, lo, t2, lo, ClrPrevLo, STYLE_SOLID, 3, false, false);    
   createText("prevlo.price"
      , t2 + 3*(Period()*60)
      , lo, "yday lo " + DoubleToStr(lo, Digits)
      , 8, "Tahoma Bold", ClrPrevLo )
      ;     
   
}

//+------------------------------------------------------------------+


/*
- Display daily opens for 5 trading days [trading week] and open day/price 
  displayed at the right hand-side of the chart until the end of friday trading. 
  [please see image above]
- Display yesterday's High and Low
- Display 3 pivots [daily, weekly and monthly]
- Option to display days with large labels on chart background similar to the image above.
*/

//+------------------------------------------------------------------+
// #include <ay-CommonFn\Object.mqh>                                 |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| createRect                                                       |
//+------------------------------------------------------------------+   
void createRect(string objname, double p1, datetime t1, double p2, 
   datetime t2, color clr, bool back=true,  int winid=0)
{

   objname = gsPref + objname;
   if(ObjectFind(objname) != winid)    
      ObjectCreate(objname, OBJ_RECTANGLE, winid, 0, 0, 0, 0);
   
   ObjectSet(objname, OBJPROP_PRICE1, p1);
   ObjectSet(objname, OBJPROP_TIME1,  t1);
   ObjectSet(objname, OBJPROP_PRICE2, p2);
   ObjectSet(objname, OBJPROP_TIME2,  t2);
   ObjectSet(objname, OBJPROP_COLOR,  clr);
   ObjectSet(objname, OBJPROP_BACK,   back);
}  

//+------------------------------------------------------------------+
//| createText                                                       |
//+------------------------------------------------------------------+
void createText(string name, datetime t, double p, string text
   , int size=8, string font="Arial", color c=White, int winid=0)
{
   name = gsPref + name;
   
   if (ObjectFind(name) != winid)
      ObjectCreate (name,OBJ_TEXT,winid,0,0);
   
   ObjectSet    (name,OBJPROP_TIME1,  t);
   ObjectSet    (name,OBJPROP_PRICE1, p);
   ObjectSetText(name,text,size,font, c);

}

//+------------------------------------------------------------------+
//| createHl                                                         |
//+------------------------------------------------------------------+ 
void createHl (string hlname, double v1, color hlcolor
   , int style= STYLE_SOLID, int width = 1, bool back=true, int winid=0)
{
   hlname = gsPref + hlname;
   
   if(ObjectFind(hlname) != winid)   
        ObjectCreate(hlname, OBJ_HLINE, winid, Time[0], v1); 
   else ObjectSet(hlname, OBJPROP_PRICE1, v1);  
   
   
   ObjectSet(hlname, OBJPROP_COLOR, hlcolor);
   ObjectSet(hlname, OBJPROP_BACK,  back);
   ObjectSet(hlname, OBJPROP_STYLE, style);
   ObjectSet(hlname, OBJPROP_WIDTH, width);         

}
//+------------------------------------------------------------------+
//| createTl                                                         |
//+------------------------------------------------------------------+ 
void createTl(string tlname, datetime t1, double v1, 
     datetime t2, double v2, color tlColor, int style = STYLE_SOLID, 
     int width = 1, bool back=true, bool ray=false, string desc="", int winid=0)
{
   tlname = gsPref + tlname;
   if(ObjectFind(tlname) != winid)
   {
      ObjectCreate(
            tlname
            , OBJ_TREND
            , winid
            , t1
            , v1
            , t2
            , v2
            );      
   }else
   {
      ObjectMove(tlname, 0, t1, v1);   
      ObjectMove(tlname, 1, t2, v2);
   }
   ObjectSet(tlname, OBJPROP_COLOR, tlColor);
   ObjectSet(tlname, OBJPROP_RAY,   ray);
   ObjectSet(tlname, OBJPROP_STYLE, style);
   ObjectSet(tlname, OBJPROP_WIDTH, width);
   ObjectSet(tlname, OBJPROP_BACK,  back);
   ObjectSetText(tlname, desc);
} 

//+------------------------------------------------------------------+
//| delObjs function                                                 |
//+------------------------------------------------------------------+
void delObjs(string s="")
{
   int objs = ObjectsTotal();
   if (StringLen(s) == 0) s = gsPref;
   
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringSubstr(name,0,StringLen(s)) == s)       
         ObjectDelete(name); 
   }   
} 