//*
//* Two Days Open Close Lines
//* BlueRain
//* 10/1/2020

#property copyright "BlueRain"

#property indicator_chart_window

extern string UniqueID = ".";
extern int    CandleIndex = 5;
extern color H_LineColor = clrBlue;
extern color L_LineColor = clrRed;
extern int   HL_LineWidth = 2;
extern ENUM_LINE_STYLE LineStyle = STYLE_SOLID;

string LabelID = "DCHL";
 
double timeX = 0;

   
int index = CandleIndex;
  


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{

   LabelID = LabelID + UniqueID;
   
  
    
  //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()
{

   //increase if new bar 
   if ( isNewBar() == true )   index++;
    

   deleteObjects();
   
    
   
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied = CopyRates(_Symbol,0,index - 2,1,rates);


   if ( copied > 0)
   {
    
    //draw 2 lines
     DrawLine(LabelID + "high", rates[0].time,Time[0]+ timeX*2,rates[0].high,rates[0].high,H_LineColor,HL_LineWidth,LineStyle);
     DrawLine(LabelID + "low", rates[0].time,Time[0] + timeX*2,rates[0].low,rates[0].low,L_LineColor,HL_LineWidth,LineStyle);
    
     //draw candle No
     
     DrawPipNumbers(index - 1);
     
    
      
    }
   
   
   //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);
                   

}  

void DrawPipNumbers(int i)
{
  string name = LabelID + IntegerToString(i);
  
  double atr=iATR(NULL,0,100,i); 
  
  ObjectDelete(name);
  ObjectCreate(name, OBJ_TEXT, 0, Time[i-1], High[i-1]+ atr/2);
  ObjectSet(name, OBJPROP_ANGLE, 0);
  ObjectSetText(name,IntegerToString(i),10, "Corbel", clrRed);

}


bool isNewBar()
  {

   static datetime BarTime = TimeCurrent();

   bool res=false;
   if(BarTime!=Time[0])
     {
      BarTime=Time[0];
      res=true;
     }
//----
   return(res);
  }

 