//*
//* Two Days Open Close Lines
//* BlueRain
//* 10/1/2020

#property copyright "BlueRain"

#property indicator_chart_window

extern color YHL_LineColor = clrBlue;
extern color THL_LineColor = clrRed;
extern int   HL_LineWidth = 2;
extern int YHTH_PipDiff_Yposition = 30;
extern int YLTL_PipDiff_Yposition = -20;


extern int TimeZoneOfData= 0;

string LabelID = "TDOC_";

double Poin;

int YHTH_Yposition = 30;
int YLTL_Yposition = -20;
int THTC_Yposition = 30;


 
double timeX = 0;

   
   


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{

    if (Point==0.00001) Poin=0.0001;
   else {
      if (Point==0.001) Poin=0.01;
      else Poin=Point;
   }
   
   
    
  //Print("I is ", i);
  
  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(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   deleteObjects();
 
	return(0);
}

void deleteObjects()
{
   string lookFor       = LabelID;
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//+------------------------------------------------------------------+
//| 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 start()
{

   ScreenDrawing();
   return (0);
}

void ScreenDrawing()
{


   deleteObjects();
   
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied = CopyRates(Symbol(),PERIOD_D1,0,2,rates);

   if ( copied > 0)
   {
     //lines Open/Close 
    
    
    
     DrawVLine(LabelID + "d2openvline", rates[1].time,clrYellow,1,STYLE_DOT);
     DrawVLine(LabelID + "d1openvline", rates[0].time,clrYellow,1,STYLE_DOT);
   
   
   
    //yesterday
    //DrawLine(string name, datetime Time1, datetime Time2, double Price1, double Price2,color lcolor, int lwidth,ENUM_LINE_STYLE linestyle)
    
     DrawLine(LabelID + "yopenhigh", rates[1].time,Time[0],rates[1].high,rates[1].high,YHL_LineColor,HL_LineWidth,STYLE_SOLID);
     DrawLine(LabelID + "yopenlow", rates[1].time,Time[0],rates[1].low,rates[1].low,YHL_LineColor,HL_LineWidth,STYLE_SOLID);
   
     DrawLine(LabelID + "topenhigh", rates[0].time,Time[0],rates[0].high,rates[0].high,THL_LineColor,HL_LineWidth,STYLE_SOLID);
     DrawLine(LabelID + "topenlow", rates[0].time,Time[0],rates[0].low,rates[0].low,THL_LineColor,HL_LineWidth,STYLE_SOLID);
    
    
      //yesterday
      DrawPrice(LabelID + "yopenprice",Time[0],rates[1].high,clrYellow,10,"YH"+ " (" + DoubleToStr(rates[1].high,Digits) + ")");
      DrawPrice(LabelID + "ycloseprice",Time[0],rates[1].low,clrYellow,10,"YL"+ " (" + DoubleToStr(rates[1].low,Digits) + ")");
    
      //today
      DrawPrice(LabelID + "topenprice",Time[0],rates[0].high,clrYellow,10,"TH" + " (" + DoubleToStr(rates[0].high,Digits) + ")");
      DrawPrice(LabelID + "tcloseprice",Time[0],rates[0].low,clrYellow,10,"TL" + " (" + DoubleToStr(rates[0].low,Digits) + ")");


     //pip diff
      string pips ;
     
      pips = DoubleToStr( (rates[0].high - rates[1].high) / Poin,2);
      

      DrawPipDiff(LabelID + "d1Hpipdiff",rates[0].time,rates[0].high,clrYellow,10,"YHTH PipDiff: " + pips,YHTH_Yposition);
     
      pips = DoubleToStr(  (rates[0].low - rates[1].low ) / Poin,2);

      DrawPipDiff(LabelID + "d1Lpipdiff",rates[0].time,rates[0].low,clrYellow,10,"YLTL Pip Diff:" + pips, YLTL_Yposition );
   
     
      //Draw THTC  ( today's high Today's close )
     
      
      double TH = rates[0].high;
      double TC = rates[0].close;
      
      double TL = rates[0].low;
      
      double YH = rates[1].high;
      double YL = rates[1].low;
      
      
      double DI1 =   (  ( ( TH-TC )/Poin)  / ((YH-TH)/Poin) )  *100;
      
      pips = DoubleToStr(  (rates[0].high - rates[0].close) / Poin,2);
       
      DrawPipDiff(LabelID + "d1thtc",Time[0] + timeX*10, Close[0],clrYellow,8,"THTC PiDi:" + pips + " / % Di " + DoubleToStr(DI1,2),THTC_Yposition+10);
      
      
      
      double DI2 =   (  ( (TC-TL)/Poin ) / ( (YL-TL)/Poin) ) * 100;
      
      
      //Draw  TLTC
      pips = DoubleToStr(  (rates[0].close - rates[0].low ) / Poin,2);
      
      
      DrawPipDiff(LabelID + "d1tltc",Time[0] + timeX*10, Close[0],clrYellow,8,"TLTC PiDi:" + pips + " / % Di " + DoubleToStr(DI2,2),THTC_Yposition-10);
      
   }
   
   
   //DailyOpen(0,lastbar);
   WindowRedraw();




}

void DrawPriceValues(string name, double price,color textcolor, int fontsize  )
{
   
  if (ObjectFind(name) ) ObjectDelete(name);
  
  ObjectCreate(name, OBJ_TEXT, 0, Time[0],price);
  ObjectSet(name, OBJPROP_ANGLE, 0);
  
  ObjectSetText(name,DoubleToStr(price,2) ,fontsize, "Corbel", textcolor);

}





//+------------------------------------------------------------------+
//| Draws a pips distance for SL or TP.                              |
//+------------------------------------------------------------------+
void DrawPrice(string name, datetime time1, double price1, color col, int fontSize, string text)
{
  
   int x, y;

   
	// Needed only for y, x is derived from the chart width.
   ChartTimePriceToXY(0, 0, time1, price1, x, y);
   DrawLabel(name,x,y-10,text,fontSize,"Arial",col,DoubleToStr(price1,Digits));
   
   
   
}


void DrawPipDiff(string name, datetime time1, double price1, color col, int fontSize, string text, int Yposition)
{
  
   int x, y;

   
	// Needed only for y, x is derived from the chart width.
   ChartTimePriceToXY(0, 0, time1, price1, x, y);
   DrawLabel(name,x,y-Yposition,text,fontSize,"Arial",col,DoubleToStr(price1,Digits));
   
   
   
}




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);
//--- justify text
//ObjectSet(name, OBJPROP_ANCHOR, 0);
//ObjectSetString(0, name, OBJPROP_TOOLTIP, tooltip);
//ObjectSet(name, OBJPROP_SELECTABLE, 0);
//---
  }
  
  
//+------------------------------------------------------------------+
//|  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);
                   

}  

