//+------------------------------------------------------------------+
//|                                           Custom_Zone_Candle.mq4 |
//|                                                            Dadas |
//|                                              fx-nvatc.comeze.com |
//+------------------------------------------------------------------+
#property copyright "Dadas"
#property link      "fx-nvatc.comeze.com"

#property indicator_chart_window

extern bool Show_Custom_Zone_Candle = true;
extern int   Custom_Zone_Candle_Tf  = 1440;
extern int   Custom_Zone_Candle_Index=1;
extern bool Pause_Custom_Zone_Candle = false;
extern color Custom_Zone_Candle_HL_Color = Moccasin; 
extern color Custom_Zone_Candle_HH_Color = Moccasin; 
extern color Custom_Zone_Candle_Color = Gainsboro;
extern bool  Custom_Zone_Candle_Bckg = true ;

int   Custom_Zone_Candle_Frame = 2 ;
int Custom_Zone_Candle_Offset = 1;

datetime Custom_Zone_Candle_time1,Custom_Zone_Candle_time2;
double Custom_Zone_Candle_price1,Custom_Zone_Candle_price2,Custom_Zone_Candle_price3,
Custom_Zone_Candle_price4,Custom_Zone_Candle_price5,Custom_Zone_Candle_price6;
// int Custom_Zone_Candle_Index;

//Candle Index section:
extern bool Show_Candle_Index = true;
extern color Candle_Index_ArrowColor = Black;

int   Candle_Index_ArrowCode = 158;
int Candle_Index_Arrow;
double Candle_Index_price;
//----

// CZC_Time_Lines section:
extern bool Show_CZC_Time_Lines = true;
extern color CZC_Time_Lines_Color = LimeGreen;
extern int   CZC_Time_Lines_Style = 2;
extern int   CZC_Time_Lines_Width = 0;
datetime CZC_Time_Lines_Open_Hour,CZC_Time_Lines_Close_Hour,CZC_Time_Lines_Next_Close_Hour;
//----
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
// Candle Index section:

   Candle_Index_Arrow = Bars;

  for(int Candle_Index_i = 0 ; Candle_Index_i < Candle_Index_Arrow ; Candle_Index_i++)
  {
      Candle_Index_CreateArrow("Candle_Index = "+Candle_Index_i ,Candle_Index_ArrowColor,Candle_Index_ArrowCode);
  }
//----

// Master_Start_Line section:
if(Show_CZC_Time_Lines==true)
  {
  ObjectCreate("CZC_Time_Lines_Open", OBJ_VLINE, 0, 0, 0); 
  ObjectCreate("CZC_Time_Lines_Close", OBJ_VLINE, 0, 0, 0); 
  ObjectCreate("CZC_Time_Lines_Next_Close", OBJ_VLINE, 0, 0, 0);    
  } 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
//Rectangles section:
if(Pause_Custom_Zone_Candle==false)
{
  ObjectDelete("Custom_Zone_Candle"); 
  ObjectDelete("Custom_Zone_Candle_HL");   
  ObjectDelete("Custom_Zone_Candle_HH");  
}  
//----

//Candle Index section:
  Candle_Index_Arrow = Bars;
  for(int Candle_Index_i = 0 ; Candle_Index_i < Candle_Index_Arrow ; Candle_Index_i++)
  {
     ObjectDelete("Candle_Index = "+Candle_Index_i);      
  }  
 //----  
 
 // CZC_Time_Lines section:
  ObjectDelete("CZC_Time_Lines_Open");  
  ObjectDelete("CZC_Time_Lines_Close"); 
  ObjectDelete("CZC_Time_Lines_Next_Close");    
//----  
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
if(Show_Custom_Zone_Candle==true)
  {

Custom_Zone_Candle_price1 = iHigh(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
Custom_Zone_Candle_price2 = iLow(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
if(iOpen(NULL,Custom_Zone_Candle_Tf,Custom_Zone_Candle_Index)<iClose(NULL,Custom_Zone_Candle_Tf,Custom_Zone_Candle_Index))
  {
Custom_Zone_Candle_price3 = iHigh(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
Custom_Zone_Candle_price4 = iClose(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
Custom_Zone_Candle_price5 = iOpen(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
Custom_Zone_Candle_price6 = iLow(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
  }
if(iOpen(NULL,Custom_Zone_Candle_Tf,Custom_Zone_Candle_Index)>iClose(NULL,Custom_Zone_Candle_Tf,Custom_Zone_Candle_Index))
  {
Custom_Zone_Candle_price3 = iHigh(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
Custom_Zone_Candle_price4 = iOpen(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
Custom_Zone_Candle_price5 = iClose(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
Custom_Zone_Candle_price6 = iLow(NULL, Custom_Zone_Candle_Tf, Custom_Zone_Candle_Index);
  }
Custom_Zone_Candle_time1 = iTime(NULL, Period(), Bars); // Custom_Zone_Candle_Index);
Custom_Zone_Candle_time2 = iTime(NULL, Custom_Zone_Candle_Tf, 0)+Custom_Zone_Candle_Tf*60*Custom_Zone_Candle_Offset;
//----
    if(Pause_Custom_Zone_Candle==false)
     {
     ObjectDelete("Custom_Zone_Candle");
     
     }
     
     ObjectCreate("Custom_Zone_Candle", OBJ_RECTANGLE,0,Custom_Zone_Candle_time1,Custom_Zone_Candle_price1,Custom_Zone_Candle_time2,Custom_Zone_Candle_price2);
     ObjectSet("Custom_Zone_Candle", OBJPROP_BACK,Custom_Zone_Candle_Bckg);
     ObjectSet("Custom_Zone_Candle", OBJPROP_COLOR,Custom_Zone_Candle_Color); 
     ObjectSet("Custom_Zone_Candle", OBJPROP_WIDTH,Custom_Zone_Candle_Frame);
     
     if(Pause_Custom_Zone_Candle==false)
     {
     ObjectDelete("Custom_Zone_Candle_HL");
     }
     
     ObjectCreate("Custom_Zone_Candle_HL", OBJ_RECTANGLE,0,Custom_Zone_Candle_time1,Custom_Zone_Candle_price3,Custom_Zone_Candle_time2,Custom_Zone_Candle_price4);
     ObjectSet("Custom_Zone_Candle_HL", OBJPROP_BACK,Custom_Zone_Candle_Bckg);
     ObjectSet("Custom_Zone_Candle_HL", OBJPROP_COLOR,Custom_Zone_Candle_HL_Color); 
     ObjectSet("Custom_Zone_Candle_HL", OBJPROP_WIDTH,Custom_Zone_Candle_Frame);
     
     if(Pause_Custom_Zone_Candle==false)
     { 
     ObjectDelete("Custom_Zone_Candle_HH");
     }
     
     ObjectCreate("Custom_Zone_Candle_HH", OBJ_RECTANGLE,0,Custom_Zone_Candle_time1,Custom_Zone_Candle_price5,Custom_Zone_Candle_time2,Custom_Zone_Candle_price6);
     ObjectSet("Custom_Zone_Candle_HH", OBJPROP_BACK,Custom_Zone_Candle_Bckg);
     ObjectSet("Custom_Zone_Candle_HH", OBJPROP_COLOR,Custom_Zone_Candle_HH_Color); 
     ObjectSet("Custom_Zone_Candle_HH", OBJPROP_WIDTH,Custom_Zone_Candle_Frame);     
  }     
//----

// Candle Index section:
if(Show_Candle_Index==true)
  {
    Candle_Index_Arrow = Bars;
  
  if(Candle_Index_IsNewBar())
   {
   
     for(int Candle_Index_i = 0 ; Candle_Index_i < Candle_Index_Arrow ; Candle_Index_i++)
     {
      Candle_Index_price = Low[Candle_Index_i]-5*Point ;
         
      Candle_Index_SetArrow("Candle_Index = "+Candle_Index_i ,Candle_Index_price,Time[Candle_Index_i]);      
     }   
   }
   }
 //----  
 
 // CZC_Time_Lines section:
if(Show_CZC_Time_Lines==true)
  { 
  CZC_Time_Lines_Open_Hour = iTime(NULL,Custom_Zone_Candle_Tf,Custom_Zone_Candle_Index);
  ObjectSet("CZC_Time_Lines_Open", OBJPROP_BACK,true);
  ObjectSet("CZC_Time_Lines_Open", OBJPROP_WIDTH,CZC_Time_Lines_Width);
  ObjectSet("CZC_Time_Lines_Open", OBJPROP_COLOR,CZC_Time_Lines_Color);
  ObjectSet("CZC_Time_Lines_Open", OBJPROP_STYLE,CZC_Time_Lines_Style);
  ObjectSet("CZC_Time_Lines_Open", OBJPROP_TIME1,CZC_Time_Lines_Open_Hour);
  
  CZC_Time_Lines_Close_Hour = iTime(NULL,Custom_Zone_Candle_Tf,Custom_Zone_Candle_Index-1);
  ObjectSet("CZC_Time_Lines_Close", OBJPROP_BACK,true);
  ObjectSet("CZC_Time_Lines_Close", OBJPROP_WIDTH,CZC_Time_Lines_Width);
  ObjectSet("CZC_Time_Lines_Close", OBJPROP_COLOR,CZC_Time_Lines_Color);
  ObjectSet("CZC_Time_Lines_Close", OBJPROP_STYLE,CZC_Time_Lines_Style);
  ObjectSet("CZC_Time_Lines_Close", OBJPROP_TIME1,CZC_Time_Lines_Close_Hour);
  
  CZC_Time_Lines_Next_Close_Hour = iTime(NULL,Custom_Zone_Candle_Tf,Custom_Zone_Candle_Index-2);
  ObjectSet("CZC_Time_Lines_Next_Close", OBJPROP_BACK,true);
  ObjectSet("CZC_Time_Lines_Next_Close", OBJPROP_WIDTH,CZC_Time_Lines_Width);
  ObjectSet("CZC_Time_Lines_Next_Close", OBJPROP_COLOR,CZC_Time_Lines_Color);
  ObjectSet("CZC_Time_Lines_Next_Close", OBJPROP_STYLE,CZC_Time_Lines_Style);
  ObjectSet("CZC_Time_Lines_Next_Close", OBJPROP_TIME1,CZC_Time_Lines_Next_Close_Hour); 
  }
//----
   return(0);
  }
//+------------------------------------------------------------------+
// Candle Index section:
//+------------------------------------------------------------------+
void Candle_Index_CreateArrow(string Candle_Index_name,color Candle_Index_c,int Candle_Index_code)
{
    ObjectCreate(Candle_Index_name ,OBJ_ARROW,0,0,0);
    ObjectSet(Candle_Index_name, OBJPROP_ARROWCODE,Candle_Index_code);
    ObjectSet(Candle_Index_name ,OBJPROP_COLOR,Candle_Index_c);
}
//+------------------------------------------------------------------+
void Candle_Index_SetArrow(string Candle_Index_name,double Candle_Index_price,datetime Candle_Index_time)
{
    ObjectSet(Candle_Index_name, OBJPROP_PRICE1,Candle_Index_price);
    ObjectSet(Candle_Index_name, OBJPROP_TIME1,Candle_Index_time);
}

//+------------------------------------------------------------------+

bool Candle_Index_IsNewBar()
{ 
  static datetime Candle_Index_prevTime1 = -1;
  
  if(Candle_Index_prevTime1 != Time[6])
  { 
   Candle_Index_prevTime1 = Time[6]; 
       
   return(true);  
  } 

  return(false); 
}
//+------------------------------------------------------------------+