//+------------------------------------------------------------------+
//|                                                BreakHotZones.mq4 |
//|                                    Copyright 2020, by. ganztrade |
//|code provided by. BlueRain & KiMiV      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 string Info1="If want use on XAUUSD/GOLD, set MarketOpen value = 1";
extern int    MarketOpen=0;
extern double TargetFactor = 2.000;
extern string Info2="If want see past candle, set to true";
extern bool   UsePastCandle = false;
extern bool   ShowHotZones = true;
extern int    NumberOfDays = 1;
extern string Info3="if Fibo set true, fibo based on Openprice to target factor line";
extern bool   UseCurrentDayFibo = false;
string SydneyBegin  = "01:00";        
string SydneyEnd    = "04:00";    
color  SydneyColor  = clrDarkGreen;           
string TokyoBegin   = "05:00";        
string TokyoEnd     = "08:00";        
color  TokyoColor   = clrIndigo;   
string EurBegin     = "09:00";      
string EurEnd       = "13:00";     
color  EurColor     = clrCadetBlue;      
string USABegin     = "15:00";      
string USAEnd       = "21:00";     
color  USAColor     = clrMidnightBlue;            
int    SizeFont     = 8;
string LabelID = "BreakHotZones";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
    for (int x=0; x<NumberOfDays; x++) {
    CreateObjects("SY"+x, SydneyColor);
    CreateObjects("TY"+x, TokyoColor);
    CreateObjects("EU"+x, EurColor);
    CreateObjects("US"+x, USAColor);
  }
//---
   return(INIT_SUCCEEDED);
  }
  
  
void OnDeinit(const int reason)
  {
//---  
    ObjectDelete(LabelID + "Fibo1");
    ObjectDelete(LabelID + "Fibo2");
    ObjectDelete(LabelID + "open");
    ObjectDelete(LabelID + "breakhigh");
    ObjectDelete(LabelID + "breaklow");
    ObjectDelete(LabelID + "tpbuy");
    ObjectDelete(LabelID + "tpsell");
    ObjectsDeleteAll(0,OBJ_RECTANGLE); 
    
    int limit=Bars;
    for(int i=limit;i>0;i--)
     {
    datetime bartime=iTime(NULL,PERIOD_M30,i);
    string   dt=TimeToString(bartime,TIME_DATE||TIME_MINUTES);
    ObjectDelete(dt+"Labels_1");
    ObjectDelete(dt+"Labels_2");
    ObjectDelete(dt+"Labels_3");
    ObjectDelete(dt+"Labels_4");
    ObjectDelete(dt+"Labels_5");}
//---
  } 
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| 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[])
  {
//---
  int limit=Bars-NumberOfDays;
  if(limit>Bars-10)
     limit=Bars-10;
  else
     limit=limit+1;
  
  for(int i=limit;i>0;i--)
     {
     datetime bartime=iTime(NULL,PERIOD_M30,i);
     string   dt=TimeToString(bartime,TIME_DATE||TIME_MINUTES);

     if( TimeHour(bartime ) == MarketOpen)
        {        
        double HighCandleOne=iHigh(NULL,PERIOD_M30,i);//--first candle open market
        double LowCandleOne=iLow(NULL,PERIOD_M30,i);//--first candle open market
        
        double OpenCandleTwo=iOpen(NULL,PERIOD_M30,i-1);//--second candle starting line
        
        double Range=HighCandleOne-LowCandleOne;//--based on first candle
                  
        double breakbuy=OpenCandleTwo+(Range);//--for offsett high line second candle
        double breaksell=OpenCandleTwo-(Range);//--for offsett low line second candle
        
        double tpbuy=OpenCandleTwo+(Range*TargetFactor);//--for offsett high line second candle
        double tpsell=OpenCandleTwo-(Range*TargetFactor);//--for offsett low line second candle
        
        datetime CandleTime=bartime+(24*PERIOD_M30*118);     
        
        if(UsePastCandle == true){        
              
        if(ObjectFind(dt+"Labels_1")!=0)
           {
           ObjectCreate(dt+"Labels_1",OBJ_TREND,0,bartime,OpenCandleTwo,CandleTime,OpenCandleTwo);
           ObjectSet(dt+"Labels_1",OBJPROP_COLOR,clrYellow);
           ObjectSet(dt+"Labels_1",OBJPROP_STYLE,STYLE_DOT);
           ObjectSet(dt+"Labels_1",OBJPROP_RAY_RIGHT,false);
           ObjectSet(dt+"Labels_1",OBJPROP_WIDTH,0);        
           }
           
        if(ObjectFind(dt+"Labels_2")!=0)
           {
           ObjectCreate(dt+"Labels_2",OBJ_TREND,0,bartime,breakbuy,CandleTime,breakbuy);
           ObjectSet(dt+"Labels_2",OBJPROP_COLOR,clrLime);
           ObjectSet(dt+"Labels_2",OBJPROP_STYLE,STYLE_DOT);
           ObjectSet(dt+"Labels_2",OBJPROP_RAY_RIGHT,false);
           ObjectSet(dt+"Labels_2",OBJPROP_WIDTH,0);
          
           }
        if(ObjectFind(dt+"Labels_3")!=0)
           {
           ObjectCreate(dt+"Labels_3",OBJ_TREND,0,bartime,breaksell,CandleTime,breaksell);
           ObjectSet(dt+"Labels_3",OBJPROP_COLOR,clrRed);
           ObjectSet(dt+"Labels_3",OBJPROP_STYLE,STYLE_DOT);
           ObjectSet(dt+"Labels_3",OBJPROP_RAY_RIGHT,false);
           ObjectSet(dt+"Labels_3",OBJPROP_WIDTH,0);         
           }
        if(ObjectFind(dt+"Labels_4")!=0)
           {
           ObjectCreate(dt+"Labels_4",OBJ_TREND,0,bartime,tpbuy,CandleTime,tpbuy);
           ObjectSet(dt+"Labels_4",OBJPROP_COLOR,clrGreen);
           ObjectSet(dt+"Labels_4",OBJPROP_STYLE,STYLE_DOT);
           ObjectSet(dt+"Labels_4",OBJPROP_RAY_RIGHT,false);
           ObjectSet(dt+"Labels_4",OBJPROP_WIDTH,0);
          
           }
        if(ObjectFind(dt+"Labels_5")!=0)
           {
           ObjectCreate(dt+"Labels_5",OBJ_TREND,0,bartime,tpsell,CandleTime,tpsell);
           ObjectSet(dt+"Labels_5",OBJPROP_COLOR,clrMaroon);
           ObjectSet(dt+"Labels_5",OBJPROP_STYLE,STYLE_DOT);
           ObjectSet(dt+"Labels_5",OBJPROP_RAY_RIGHT,false);
           ObjectSet(dt+"Labels_5",OBJPROP_WIDTH,0);         
           }
        }    
     }   
        
     datetime bartimez=iTime(NULL,PERIOD_M30,i - 1);
     string   dtz=TimeToString(bartimez,TIME_DATE||TIME_MINUTES);

     if( TimeHour(bartimez) == MarketOpen)
        {        
        double HighCandleOnez=iHigh(NULL,PERIOD_M30,i);//--first candle open market
        double LowCandleOnez=iLow(NULL,PERIOD_M30,i);//--first candle open market
        
        double OpenCandleTwoz=iOpen(NULL,PERIOD_M30,i-1);//--second candle starting line        
        
        double Rangez=HighCandleOnez-LowCandleOnez;//--based on first candle
                 
        double breakbuyz=OpenCandleTwoz+(Rangez);//--for offsett high line second candle
        double breaksellz=OpenCandleTwoz-(Rangez);//--for offsett low line second candle
        
        double tpbuyz=OpenCandleTwoz+(Rangez*TargetFactor);//--for offsett high tp line second candle
        double tpsellz=OpenCandleTwoz-(Rangez*TargetFactor);//--for offsett low tp line second candle  
        if(UsePastCandle == false){        
        
           DrawLine(LabelID + "open", bartimez,Time[0],OpenCandleTwoz,OpenCandleTwoz,clrYellow,0,STYLE_DOT);
           DrawLine(LabelID + "breakhigh", bartimez,Time[0],breakbuyz,breakbuyz,clrLime,0,STYLE_DOT);
           DrawLine(LabelID + "breaklow", bartimez,Time[0],breaksellz,breaksellz,clrRed,0,STYLE_DOT);    
           DrawLine(LabelID + "tpbuy", bartimez,Time[0],tpbuyz,tpbuyz,clrGreen,0,STYLE_DOT);
           DrawLine(LabelID + "tpsell", bartimez,Time[0],tpsellz,tpsellz,clrMaroon,0,STYLE_DOT);      
        if(UseCurrentDayFibo == true){   
           DrawFibo(LabelID + "Fibo1", bartimez,Time[0],tpbuyz,OpenCandleTwoz,clrOrange,0,0);  
           DrawFibo(LabelID + "Fibo2", bartimez,Time[0],tpsellz,OpenCandleTwoz,clrPink,0,0); }  
          }            
        }
     }
     
     if(ShowHotZones){
     datetime dthz=CurTime();
  
  for (int x=0; x<NumberOfDays; x++) {
  
    DrawObjects(dthz, "SY"+x, SydneyBegin, SydneyEnd);
    DrawObjects(dthz, "TY"+x, TokyoBegin, TokyoEnd);
    DrawObjects(dthz, "EU"+x, EurBegin, EurEnd);
    DrawObjects(dthz, "US"+x, USABegin, USAEnd);
     
    dthz=decDateTradeDay(dthz);
    
    while (TimeDayOfWeek(dthz)>5) dthz=decDateTradeDay(dthz);
    }
  }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void DrawLine(string name, datetime Time1, datetime Time2, double Price1, double Price2,color lcolor, int lwidth,int 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 DrawFibo(string name, datetime Time1, datetime Time2, double Price1, double Price2,color lcolor, int lwidth,int linestyle)
{ 
          ObjectDelete(name);            
          ObjectCreate(name,OBJ_FIBO,0,Time1,0,Time2,0);                      
          ObjectSet(name,OBJPROP_LEVELCOLOR,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);
          }
//+------------------------------------------------------------------+
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 DrawObjects(datetime dthz, string no, string tb, string te) {
  datetime t1, t2;
  double   p1, p2;
  int      b1, b2;

  t1=StrToTime(TimeToStr(dthz, TIME_DATE)+" "+tb);
  t2=StrToTime(TimeToStr(dthz, TIME_DATE)+" "+te);
  b1=iBarShift(NULL, 0, t1);
  b2=iBarShift(NULL, 0, t2);
  p1=High[Highest(NULL, 0, MODE_HIGH, b1-b2, b2)];
  p2=Low [Lowest (NULL, 0, MODE_LOW , b1-b2, b2)];
  ObjectSet(no, OBJPROP_TIME1 , t1);
  ObjectSet(no, OBJPROP_PRICE1, p1);
  ObjectSet(no, OBJPROP_TIME2 , t2);
  ObjectSet(no, OBJPROP_PRICE2, p2);
}
