//+------------------------------------------------------------------+
//|                                                     2DFiboHL.mq4 |
//|                                    Copyright 2020, by. ganztrade |
//|code provided by. BlueRain              http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, by. ganztrade"
#property link      "http://www.metaquotes.net"

#property version   "1.00"
#property indicator_chart_window
//--- input parameters

extern bool   ShowLastDayFibo = true;
extern bool   ShowCurrentDayFibo = true;
extern color  LastDayFiboColorHigh = clrGreen;
extern color  LastDayFiboColorLow = clrCrimson;
extern color  LastDayFiboColor = clrRoyalBlue;
input ENUM_LINE_STYLE    LastDayFiboLineStyle = STYLE_SOLID;
input string  CommentLastDay0 = " ";
input string  CommentLastDay236 = " ";
input string  CommentLastDay382 = " ";
input string  CommentLastDay500 = " ";
input string  CommentLastDay618 = " ";
input string  CommentLastDay764 = " ";
input string  CommentLastDay1000 = " ";
extern color  CurrentDayFiboColorHigh = clrLime;
extern color  CurrentDayFiboColorLow = clrOrangeRed;
extern color  CurrentDayFiboColor = clrDodgerBlue;
input ENUM_LINE_STYLE    CurrentFiboLineStyle = STYLE_SOLID;
input string  CommentCurDay0 = " ";
input string  CommentCurDay236 = " ";
input string  CommentCurDay382 = " ";
input string  CommentCurDay500 = " ";
input string  CommentCurDay618 = " ";
input string  CommentCurDay764 = " ";
input string  CommentCurDay1000 = " ";

string LabelID = "myFiboHL";
double timeX = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping  
  if(Period()==1   )timeX=60;if(Period()==5    )timeX=300;if(Period()==15   )timeX=900;
  if(Period()==30  )timeX=1800;if(Period()==60   )timeX=3600;if(Period()==240  )timeX=14400;
  if(Period()==1440)timeX=86400;if(Period()==10080)timeX=604800;if(Period()==43200)timeX=2592000;
//---
   return(INIT_SUCCEEDED);
  }
  
  
void OnDeinit(const int reason)
  {
//---  
    ObjectDelete(LabelID + "HighCurrent");
    ObjectDelete(LabelID + "LowCurrent");    
    ObjectDelete(LabelID + "236");
    ObjectDelete(LabelID + "382"); 
    ObjectDelete(LabelID + "500"); 
    ObjectDelete(LabelID + "618");
    ObjectDelete(LabelID + "764"); 
    ObjectDelete(LabelID + "HighLast");
    ObjectDelete(LabelID + "LowLast");    
    ObjectDelete(LabelID + "236x");
    ObjectDelete(LabelID + "382x"); 
    ObjectDelete(LabelID + "500x"); 
    ObjectDelete(LabelID + "618x");
    ObjectDelete(LabelID + "764x");
    ObjectDelete(LabelID + "LastDay"); 
    ObjectDelete(LabelID + "CurrentDay");
    ObjectDelete(LabelID + "Label_1"); 
    ObjectDelete(LabelID + "Label_2");
    ObjectDelete(LabelID + "Label_3");    
    ObjectDelete(LabelID + "Label_4");
    ObjectDelete(LabelID + "Label_5"); 
    ObjectDelete(LabelID + "Label_6");
    ObjectDelete(LabelID + "Label_7");
    ObjectDelete(LabelID + "Labels_1"); 
    ObjectDelete(LabelID + "Labels_2");
    ObjectDelete(LabelID + "Labels_3");    
    ObjectDelete(LabelID + "Labels_4");
    ObjectDelete(LabelID + "Labels_5"); 
    ObjectDelete(LabelID + "Labels_6");
    ObjectDelete(LabelID + "Labels_7");    
//---
  } 
//+------------------------------------------------------------------+
void CreateObjects(string no, color cl) {
    ObjectCreate(no,OBJ_RECTANGLE,0,0,0,0,0);
    ObjectSet(no,OBJPROP_COLOR,cl);   
    ObjectSet(no,OBJPROP_RAY,false);  
    ObjectSet(no,OBJPROP_WIDTH,1);
    ObjectSet(no, OBJPROP_BACK, True);
}

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id==CHARTEVENT_CHART_CHANGE)
    ScreenDrawing();
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
  ScreenDrawing(); 
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
datetime decDateTradeDay (datetime dthz) {
  int ty=TimeYear(dthz);
  int tm=TimeMonth(dthz);
  int td=TimeDay(dthz);
  int th=TimeHour(dthz);
  int ti=TimeMinute(dthz);

  td--;
  if (td==0) {
    tm--;
    if (tm==0) {
      ty--;
      tm=12;
    }
    if (tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm==10 || tm==12) td=31;
    if (tm==2) if (MathMod(ty, 4)==0) td=29; else td=28;
    if (tm==4 || tm==6 || tm==9 || tm==11) td=30;
  }
  return(StrToTime(ty+"."+tm+"."+td+" "+th+":"+ti));
}
//+------------------------------------------------------------------+

void ScreenDrawing()
{
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied = CopyRates(Symbol(),PERIOD_D1,0,2,rates);

   if ( copied > 0)
   {
     DrawVLine(LabelID + "LastDay", rates[1].time,clrChocolate,1,STYLE_DOT);
     if(ShowLastDayFibo){
     DrawLine(LabelID + "HighLast", rates[1].time, rates[0].time,rates[1].high,rates[1].high,LastDayFiboColorHigh,2,LastDayFiboLineStyle);
     DrawLine(LabelID + "LowLast", rates[1].time, rates[0].time,rates[1].low,rates[1].low,LastDayFiboColorLow,2,LastDayFiboLineStyle);
     double fib236 = rates[1].low + ((rates[1].high - rates[1].low)*0.236);
     double fib382 = rates[1].low + ((rates[1].high - rates[1].low)*0.382);
     double fib500 = rates[1].low + ((rates[1].high - rates[1].low)*0.500);
     double fib618 = rates[1].low + ((rates[1].high - rates[1].low)*0.618);
     double fib764 = rates[1].low + ((rates[1].high - rates[1].low)*0.764);
     
     DrawLine(LabelID + "236", rates[1].time, rates[0].time,fib236,fib236,LastDayFiboColor,0,LastDayFiboLineStyle);
     DrawLine(LabelID + "382", rates[1].time, rates[0].time,fib382,fib382,LastDayFiboColor,0,LastDayFiboLineStyle);
     DrawLine(LabelID + "500", rates[1].time, rates[0].time,fib500,fib500,LastDayFiboColor,0,LastDayFiboLineStyle);
     DrawLine(LabelID + "618", rates[1].time, rates[0].time,fib618,fib618,LastDayFiboColor,0,LastDayFiboLineStyle);
     DrawLine(LabelID + "764", rates[1].time, rates[0].time,fib764,fib764,LastDayFiboColor,0,LastDayFiboLineStyle);
     
     int x,y;    
     //get X,Y coordinate
     ChartTimePriceToXY(0, 0,rates[1].time, rates[1].high, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Label_1",x,y,"100.0 - "+ CommentLastDay1000,8,"Arial",LastDayFiboColorHigh,DoubleToStr(rates[1].high,Digits));
   
     ChartTimePriceToXY(0, 0,rates[1].time, rates[1].low, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Label_2",x,y,"0.0 - "+ CommentLastDay1000,8,"Arial",LastDayFiboColorLow,DoubleToStr(rates[1].low,Digits));
     
     ChartTimePriceToXY(0, 0,rates[1].time, fib236, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Label_3",x,y,"23.6 - "+ CommentLastDay236,8,"Arial",LastDayFiboColor,DoubleToStr(fib236,Digits));
   
     ChartTimePriceToXY(0, 0,rates[1].time, fib382, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Label_4",x,y,"38.2 - "+ CommentLastDay382,8,"Arial",LastDayFiboColor,DoubleToStr(fib382,Digits));
     
     ChartTimePriceToXY(0, 0,rates[1].time, fib500, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Label_5",x,y,"50.0 - "+ CommentLastDay500,8,"Arial",LastDayFiboColor,DoubleToStr(fib500,Digits));
     
      ChartTimePriceToXY(0, 0,rates[1].time, fib618, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Label_6",x,y,"61.8 - "+ CommentLastDay618,8,"Arial",LastDayFiboColor,DoubleToStr(fib618,Digits));
     
     ChartTimePriceToXY(0, 0,rates[1].time, fib764, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Label_7",x,y,"76.4 - "+ CommentLastDay764,8,"Arial",LastDayFiboColor,DoubleToStr(fib764,Digits));
    }


     
    }
    
    if(ShowCurrentDayFibo){
    
     DrawVLine(LabelID + "CurrentDay", rates[0].time,clrTeal,1,STYLE_DOT);
     DrawLine(LabelID + "HighCurrent", rates[0].time, Time[0],rates[0].high,rates[0].high,CurrentDayFiboColorHigh,2,CurrentFiboLineStyle);
     DrawLine(LabelID + "LowCurrent", rates[0].time, Time[0],rates[0].low,rates[0].low,CurrentDayFiboColorLow,2,CurrentFiboLineStyle);
     double fib236x = rates[0].low + ((rates[0].high - rates[0].low)*0.236);
     double fib382x = rates[0].low + ((rates[0].high - rates[0].low)*0.382);
     double fib500x = rates[0].low + ((rates[0].high - rates[0].low)*0.500);
     double fib618x = rates[0].low + ((rates[0].high - rates[0].low)*0.618);
     double fib764x = rates[0].low + ((rates[0].high - rates[0].low)*0.764);
     
     DrawLine(LabelID + "236x", rates[0].time, Time[0],fib236x,fib236x,CurrentDayFiboColor,0,CurrentFiboLineStyle);
     DrawLine(LabelID + "382x", rates[0].time, Time[0],fib382x,fib382x,CurrentDayFiboColor,0,CurrentFiboLineStyle);
     DrawLine(LabelID + "500x", rates[0].time, Time[0],fib500x,fib500x,CurrentDayFiboColor,0,CurrentFiboLineStyle);
     DrawLine(LabelID + "618x", rates[0].time, Time[0],fib618x,fib618x,CurrentDayFiboColor,0,CurrentFiboLineStyle);
     DrawLine(LabelID + "764x", rates[0].time, Time[0],fib764x,fib764x,CurrentDayFiboColor,0,CurrentFiboLineStyle);
       
     //get X,Y coordinate
     ChartTimePriceToXY(0, 0,rates[0].time, rates[0].high, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Labels_1",x,y,"100.0 - " + CommentCurDay1000,8,"Arial",CurrentDayFiboColorHigh,DoubleToStr(rates[0].high,Digits));
   
     ChartTimePriceToXY(0, 0,rates[0].time, rates[0].low, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Labels_2",x,y,"0.0 - "+ CommentCurDay0,8,"Arial",CurrentDayFiboColorLow,DoubleToStr(rates[0].low,Digits));
     
     ChartTimePriceToXY(0, 0,rates[0].time, fib236x, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Labels_3",x,y,"23.6 - "+ CommentCurDay236,8,"Arial",CurrentDayFiboColor,DoubleToStr(fib236x,Digits));
   
     ChartTimePriceToXY(0, 0,rates[0].time, fib382x, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Labels_4",x,y,"38.2 - "+ CommentCurDay382,8,"Arial",CurrentDayFiboColor,DoubleToStr(fib382x,Digits));
     
     ChartTimePriceToXY(0, 0,rates[0].time, fib500x, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Labels_5",x,y,"50.0 - "+ CommentCurDay500,8,"Arial",CurrentDayFiboColor,DoubleToStr(fib500x,Digits));
     
      ChartTimePriceToXY(0, 0,rates[0].time, fib618x, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Labels_6",x,y,"61.8 - "+ CommentCurDay618,8,"Arial",CurrentDayFiboColor,DoubleToStr(fib618x,Digits));
     
     ChartTimePriceToXY(0, 0,rates[0].time, fib764x, x, y);
     x = x -50;
     y = y - 18 ;
     DrawLabel(LabelID + "Labels_7",x,y,"76.4 - "+ CommentCurDay764,8,"Arial",CurrentDayFiboColor,DoubleToStr(fib764x,Digits));
    }
    
    
   WindowRedraw();
}

void DrawLabel(string name,int x,int y,string label,int size=9,string font="Arial",color clr=DimGray,string tooltip="")
  { 
   if (ObjectFind(name)) ObjectDelete(name);
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,label,size,font,clr);
   ObjectSet(name,OBJPROP_CORNER,0);
   ObjectSet(name,OBJPROP_XDISTANCE,x);
   ObjectSet(name,OBJPROP_YDISTANCE,y);
   ObjectSetString(0,name,OBJPROP_TOOLTIP,tooltip);
  }
  
//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
void DrawVLine(string name, datetime Time1, color lcolor, int lwidth,ENUM_LINE_STYLE linestyle)
{  
   ObjectCreate   (name,            OBJ_VLINE, 0, Time1,0);//, dValue);
   ObjectSet      (name,            OBJPROP_STYLE, linestyle);
   ObjectSet      (name,            OBJPROP_COLOR, lcolor); 
}

void DrawLine(string name, datetime Time1, datetime Time2, double Price1, double Price2,color lcolor, int lwidth,ENUM_LINE_STYLE linestyle)
{  
          ObjectDelete(name);            
          ObjectCreate(name,OBJ_TREND,0,Time1,0,Time2,0);                      
          ObjectSet(name,OBJPROP_COLOR,lcolor);          
          if (linestyle == STYLE_SOLID)
           ObjectSet(name,OBJPROP_WIDTH,lwidth);          
          ObjectSet(name,OBJPROP_STYLE,linestyle);         
          ObjectSet(name,OBJPROP_RAY,false);              
          ObjectSet(name,OBJPROP_PRICE1,Price1);
          ObjectSet(name,OBJPROP_PRICE2,Price2);   
}  

