//+------------------------------------------------------------------+
//|                                          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

#define _O                             0
#define _H                             1
#define _L                             2
#define _C                             3

#define ERR_HISTORY_WILL_UPDATED       4066
//+------------------------------------------------------------------+

extern int        RightSideLabelsPos   = 15;
extern int        BrokerGMTOffset      = 0;

extern bool       ShowDailyOpenLabels  = true;
extern bool       ShowPivot            = true;
extern bool       ShowPivotLabels      = true;
extern bool       ShowYDayHiLoLines    = true;
extern bool       ShowYDayHiLoLabels   = true;

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            = C'015,015,025'; //Gainsboro;
extern color      ClrDayEven           = Black;          //White;
//+------------------------------------------------------------------+


int    giaTF[3]      = {PERIOD_D1, PERIOD_W1, PERIOD_MN1};
int    giCurrDOW     = -1;                                              //current day of week   
int    giShift[3]    = {1, 1, 1};
int    giTimeAdjust;

double gdaPivot[3];                                                     //hold pivot price for d1, w1, mn1
double gdaD1Open[6][2];
double gdaPrevDayOHLC[4];                                               //hold previous day hilo

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;
   
   giTimeAdjust = BrokerGMTOffset*PERIOD_H1*60;
   
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   delObjs();
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int      currdow, itf, iday, idow, sb;       
   datetime dtstart, dtend;
   double   aOHLC[4];
   
   currdow = TimeDayOfWeek(iTime(NULL, giaTF[ID_D1], 0) );//+ giTimeAdjust);

   //call display only if day of week not changed
   if (currdow == giCurrDOW)
   {
      display();
      return(0);
   }
   
   //----   
   giCurrDOW      = currdow;   
   giShift[ID_D1] = 1; 
   //----
   
   //--- sunday bar >> adjust giShift[ID_D1]
   if ( TimeDayOfWeek(iTime(NULL, giaTF[ID_D1], giShift[ID_D1]) + giTimeAdjust) == 0 )  
      giShift[ID_D1] += 1;
   
   //--- saturday bar >> adjust giShift[ID_D1]
   if ( TimeDayOfWeek(iTime(NULL, giaTF[ID_D1], giShift[ID_D1]) + giTimeAdjust) == 6 )  
      giShift[ID_D1] += 1;             
   
      
   //--->> pivot(D1, W1, MN1), Prev Day HiLo
   ArrayInitialize(gdaPivot, 0);
   ArrayInitialize(gdaPrevDayOHLC, 0);
      
   for (itf=ID_D1; itf<=ID_MN1; itf++)
   {
      switch (itf)
      {
         //---daily
         case ID_D1:
            dtstart = iTime(NULL, giaTF[itf], giShift[itf]) + giTimeAdjust;
            dtend   = dtstart + (PERIOD_D1*60) - 1;
            
            getCustOHLC(Symbol(), PERIOD_H1, dtstart, dtend, aOHLC);
                        
            gdaPivot[itf] = ( aOHLC[_H] + aOHLC[_L] + aOHLC[_C]) / 3;
            
            //--->> prev day hilo
            ArrayCopy(gdaPrevDayOHLC, aOHLC);
            //---<< prev day hilo
            
            break;
            
         //--- weekly, monthly
         default:
            gdaPivot[itf] = ( iHigh (NULL, giaTF[itf], giShift[itf])
                            + iLow  (NULL, giaTF[itf], giShift[itf])
                            + iClose(NULL, giaTF[itf], giShift[itf])
                            ) / 3;   
            break;     
      }
   }
   //---<< pivot(D1, W1, MN1), Prev Day HiLo     
   
   //--->> days open price and time
   ArrayInitialize(gdaD1Open, 0);
   
   for (idow=giCurrDOW, iday=0; idow>0; idow--, iday++)
   {
      dtstart = iTime(NULL, PERIOD_D1, iday) + giTimeAdjust;      
      sb      = getBarByTime(Symbol(), PERIOD_H1, dtstart, true);
            
      gdaD1Open[idow][ID_OPEN] = iOpen(NULL, PERIOD_H1, sb);
      gdaD1Open[idow][ID_TIME] = iTime(NULL, PERIOD_H1, sb);                         
   }
   //---<< days open price and time
         
   
   //--- show what has to be shown
   display();
   
   //----
   return(0);
}
//+------------------------------------------------------------------+
void display()
//+------------------------------------------------------------------+
{
   int itf, idow;
   
   //--->> pivot d1, w1, mn1  
   if ( ShowPivot )
   {
      for (itf=ID_D1; itf<=ID_MN1; itf++)
      {
         createHl  ("pvt."+itf, gdaPivot[itf], gcaPvt[itf], STYLE_DASH, 1, false);
         
         if ( !ShowPivotLabels ) continue;
         
         createText("pvt."+itf+".name"
            , iTime (NULL, giaTF[ID_D1], giShift[ID_D1]+itf) + giTimeAdjust
            , gdaPivot[itf]
            , gsaTF[itf] + " Pivot"
            , 8, "Tahoma Bold"
            , gcaPvt[itf])
            ;            
      }      
   }
   //---<< pivot d1, w1, mn1  
   
   //--->> days open price & label
   for (idow=giCurrDOW; idow>0; idow--)
   {
      //day open lines
      createTl("open."+idow
         , gdaD1Open[idow][ID_TIME]                   , gdaD1Open[idow][ID_OPEN]
         , Time[0] + (RightSideLabelsPos*Period()*60) , gdaD1Open[idow][ID_OPEN]
         , gcaDOW[idow]
         , STYLE_SOLID, 1, false, false);
      
      //day open labels
      if (ShowDailyOpenLabels)
      {
         createText("open."+idow+".price.text"
            , Time[0] + (RightSideLabelsPos*Period()*60)
            , gdaD1Open[idow][ID_OPEN], gsaDOWL[idow] + "-" + DoubleToStr(gdaD1Open[idow][ID_OPEN], Digits)
            , 8, "Tahoma Bold", ClrDayEven )
            ;     
            
         createText("open."+idow+".price.box"
            , Time[0] + (RightSideLabelsPos*Period()*60)
            , gdaD1Open[idow][ID_OPEN], "ллллллл"
            , 13, "Terminal", gcaDOW[idow])
            ;  
      } 
      
      //days name & background         
      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 )
          ;  
      
   }   
   //---<< days open price & label
   
   //--->> prev day hilo   
   if ( !ShowYDayHiLoLines ) return;
   
   double  hi = gdaPrevDayOHLC[_H]     //iHigh (NULL, giaTF[ID_D1], giShift[ID_D1])
         , lo = gdaPrevDayOHLC[_L]     //iLow  (NULL, giaTF[ID_D1], giShift[ID_D1])
         ;
         
   datetime t1 = iTime (NULL, giaTF[ID_D1], giShift[ID_D1]) + giTimeAdjust
          , t2 = Time[0] + (RightSideLabelsPos*Period()*60) 
          ;
          
   createTl("prevhi", t1, hi, t2, hi, ClrPrevHi, STYLE_SOLID, 3, false, false);              
   createTl("prevlo", t1, lo, t2, lo, ClrPrevLo, STYLE_SOLID, 3, false, false);    
   
   
   if ( !ShowYDayHiLoLabels ) return;
   
   createText("prevhi.price"
      , t1+(0.5*PERIOD_D1*60)
      , hi, "yday hi " + DoubleToStr(hi, Digits)
      , 8, "Tahoma Bold", ClrPrevHi )
      ;  
   
   createText("prevlo.price"
      , t1+(0.5*PERIOD_D1*60)//iTime (NULL, giaTF[ID_D1], giShift[ID_D1]) //t2 + 3*(Period()*60)
      , lo, "yday lo " + DoubleToStr(lo, Digits)
      , 8, "Tahoma Bold", ClrPrevLo )
      ;     
      
   //---<< prev day hilo   

}

//+------------------------------------------------------------------+


/*
- 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.

show daily open lines....
show daily open line labels...
------------Pivots
show pivots
show pivot labels
------------Yesterday's High - Low
show ydays hi-lo lines
show ydays hi-lo line labels

------------
BrokerGMTOffSet
*/

//+------------------------------------------------------------------+
// #include <ay-CommonFn\Object.mqh>                                 |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
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);
}  

//+------------------------------------------------------------------+
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);

}

//+------------------------------------------------------------------+
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);         

}
//+------------------------------------------------------------------+
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); 
   }   
}

//+------------------------------------------------------------------+
//#include <ay-CommonFn\GetBarByTime.mqh>
//#include <ay-CommonFn\GetCustOHLC.mqh>
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
void getCustOHLC(
     string   sym         , int tf
   , datetime dtstart     , datetime dtend
   , double&  aOHLC[4]
   )
//+------------------------------------------------------------------+
{
   int sb, eb;
   
   sb = getBarByTime(sym, tf, dtstart, true);
   eb = getBarByTime(sym, tf, dtend);
   
   aOHLC[_O] = iOpen (sym, tf, sb);
   aOHLC[_H] = iHigh (sym, tf, iHighest(sym, tf, MODE_HIGH, (sb-eb)+1, eb) );
   aOHLC[_L] = iLow  (sym, tf, iLowest (sym, tf, MODE_LOW,  (sb-eb)+1, eb) );
   aOHLC[_C] = iClose(sym, tf, eb);
}
//+------------------------------------------------------------------+
int getBarByTime(string sym, int tf, datetime t, bool fixbar=false)
//+------------------------------------------------------------------+
{
   int bar = iBarShift(sym, tf, t);
   if ( fixbar && iTime(sym, tf, bar) < t ) bar--;
   
   return (bar);

}