//+------------------------------------------------------------------+
//|                                           PreviousCandleHiLo.mq4 |
//|                                                            hairi |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "hairi"
#property link      "https://www.mql5.com/en/users/hairi"
#property version   "1.00"
#property description   "Previous Candle Hi-Lo"
#property strict
#property indicator_chart_window

//+------------------------------------------------------------------+
//| Input variable                                                   |
//+------------------------------------------------------------------+
input ENUM_LINE_STYLE   linestyle   = STYLE_DOT;         // Line Style
input color             M5_color    = clrAqua,           // Prev M5   color
                        M15_color   = clrMagenta,        // Prev M15  color
                        M30_color   = clrYellow,         // Prev M30  color
                        H1_color    = clrRed,            // Prev H1   color
                        H4_color    = clrGreen,          // Prev H4   color
                        D1_color    = clrBlue,           // Prev Day  color
                        W1_color    = clrGold,           // Prev Week color
                        MN1_color   = clrWhite;          // Prev MN1  color
                        
input bool              Show_M5     = false,
                        Show_M15    = true,
                        Show_M30    = false,
                        Show_H1     = true,
                        Show_H4     = true,
                        Show_D1     = true,
                        Show_W1     = false,
                        Show_MN1    = false; 
                        
                                               
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER;
extern string             DisplayID             = "PreviousCandleHiLo-BT";     
extern string             btn_Font              = "Tahoma";
extern int                btn_FontSize          = 10;           
extern color              btn_on_color          = clrLime;
extern color              btn_off_color         = clrRed;
extern color              btn_text_color        = clrWhite;
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrBlack;
extern int                button_x              = 500;          
extern int                button_y              = 0;          
extern int                btn_Width             = 140;        
extern int                btn_Height            = 20;          

bool           show_data            = true;
bool           recalc               = true;

int         CalcDay = 7;
int         CalcBar = 0;
string      Objname,
ObjIdentifier="INN_";

string  buttonId,IndicatorName,IndicatorObjPrefix ;
//+------------------------------------------------------------------+
string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int OnInit()
{
   IndicatorName = GenerateIndicatorName(DisplayID );
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(WindowExpertName());
   IndicatorDigits(2);

   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
   show_data = val != 0;

   buttonId = IndicatorObjPrefix+DisplayID;
   createButton(buttonId, DisplayID,btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_on_color);
   ObjectSetInteger(0,buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(0,buttonId, OBJPROP_XDISTANCE, button_x);
   
   return(INIT_SUCCEEDED);
}   
//+------------------------------------------------------------------+
int deinit2()
 {
     ObjectDeleteAll(); 
     
 return(0);
 }
//+------------------------------------------------------------------+
int deinit()                            
  {  
   deinit2();
   
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);  
       
    return(0);  
  }
//+------------------------------------------------------------------+
void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (0,buttonID);
      ObjectCreate    (0,buttonID,OBJ_BUTTON,0,0,0);
      ObjectSetInteger(0,buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BORDER_TYPE,BORDER_RAISED);
      ObjectSetInteger(0,buttonID,OBJPROP_XSIZE,width);
      ObjectSetInteger(0,buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (0,buttonID,OBJPROP_FONT,font);
      ObjectSetString (0,buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(0,buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(0,buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,9999);
}                               
//+------------------------------------------------------------------+
void handleButtonClicks()
{
   if (ObjectGetInteger(0, buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(0, buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
   }
}
//+------------------------------------------------------------------+
void OnChartEvent(const int ids, const long &lparam,const double &dparam,const string &sparam)
{   
   if (ids==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)
   {
   handleButtonClicks();
      
   if (show_data)
      { 
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_on_color);
     
      start();    
      }
      else
      {       
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_off_color);
      
      deinit2();
      }
   }  
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int start()
  {
   handleButtonClicks();
   recalc = false;
   
   int BarCheck=IndicatorCounted();
   if(BarCheck < 0) return (-1);
   if(BarCheck>0) BarCheck--;
   int TotalBar=Bars-BarCheck;

   for(int BarMeasure=0; BarMeasure<=240; BarMeasure++)
     {
      double M5_High = 0, M5_Low = 0;
      double M15_High = 0, M15_Low = 0;
      double M30_High = 0, M30_Low = 0;
      double H1_High = 0, H1_Low = 0;
      double H4_High = 0, H4_Low = 0;
      double D1_High = 0, D1_Low = 0;
      double W1_High = 0, W1_Low = 0;
      double MN1_High = 0, MN1_Low = 0;
      
      if(Show_M5)  PrevPeriod(PERIOD_M5,M5_Low,M5_High,"Prev M5 High","Prev M5 Low",linestyle,M5_color,1);
      if(Show_M15) PrevPeriod(PERIOD_M15,M15_Low,M15_High,"Prev M15 High","Prev M15 Low",linestyle,M15_color,1);
      if(Show_M30) PrevPeriod(PERIOD_M30,M30_Low,M30_High,"Prev M30 High","Prev M30 Low",linestyle,M30_color,1);
      if(Show_H1)  PrevPeriod(PERIOD_H1,H1_Low,H1_High,"Prev H1 High","Prev H1 Low",linestyle,H1_color,1);
      if(Show_H4)  PrevPeriod(PERIOD_H4,H4_Low,H4_High,"Prev H4 High","Prev H4 Low",linestyle,H4_color,1);
      if(Show_D1)  PrevPeriod(PERIOD_D1,D1_Low,D1_High,"Prev Day High","Prev Day Low",linestyle,D1_color,1);
      if(Show_W1)  PrevPeriod(PERIOD_W1,W1_Low,W1_High,"Prev Week High","Prev Week Low",linestyle,W1_color,1);
      if(Show_MN1) PrevPeriod(PERIOD_MN1,MN1_Low,MN1_High,"Prev MN1 High","Prev MN1 Low",linestyle,MN1_color,1);
     }
     
   if (show_data)
      {              
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_on_color);     
      }
      else
      {       
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_off_color);
      
      deinit2();
      }
        
   return (0);
  }

//+------------------------------------------------------------------+
//| Object Delete all function                                       |
//+------------------------------------------------------------------+
void ObjectDeleteAll()
  {
   Comment("");
   ObjectsDeleteAll(0,ObjIdentifier);
  }
  
//+------------------------------------------------------------------+
//| Draw line function                                               |
//+------------------------------------------------------------------+
void DrawShortHLine(string nameX,int time1,double P0,int style,color clr,int width)
  {
   string objName;

   datetime T0 = iTime(Symbol(),PERIOD_H1,time1);
   datetime T1 = iTime(Symbol(),PERIOD_M1,1);

   objName=nameX;
   if(ObjectFind(0,ObjIdentifier+objName)!=0)
      ObjectCreate(ObjIdentifier+objName,OBJ_TREND,0,T0,P0,T1,P0);
   else
     {
      ObjectDelete(ObjIdentifier+objName);
      ObjectCreate(ObjIdentifier+objName,OBJ_TREND,0,T0,P0,T1,P0);
     }
   ObjectSetInteger(0,ObjIdentifier+objName,OBJPROP_STYLE,style);
   ObjectSetInteger(0,ObjIdentifier+objName,OBJPROP_COLOR,clr);
   ObjectSetInteger(0,ObjIdentifier+objName,OBJPROP_WIDTH,width);
   ObjectSetInteger(0,ObjIdentifier+objName,OBJPROP_RAY,false);
   ObjectSetInteger(0,ObjIdentifier+objName,OBJPROP_BACK,true);
//ObjectSetString (0,name,name);
  }

//+------------------------------------------------------------------+
//| Draw complete line function                                      |
//+------------------------------------------------------------------+
void HLine(string name,string desc,int pShiftTime,double pPrice,int pStyle,color pColor,int pWidth) export
  {
   DrawShortHLine(name,pShiftTime,pPrice,pStyle,pColor,pWidth);
   CreatePriceLabel(name+" Label",desc,pPrice,pColor);
   CreatePriceFlag(name+" Price",pPrice,pColor);
  }

//+------------------------------------------------------------------+
//| scale the view                                                   |
//+------------------------------------------------------------------+
int ChartScaleGet() export
  {
   long result=-1;
   ChartGetInteger(0,CHART_SCALE,0,result);
   return((int)result);
  }

//+------------------------------------------------------------------+
//| Create price flag function                                       |
//+------------------------------------------------------------------+
void CreatePriceFlag(string name,double price,color col) export
  {
   datetime time=Time[0]+Period()*60;

   if(ObjectFind(0,ObjIdentifier+name)!=0)
     {
      ObjectCreate(ObjIdentifier+name,OBJ_ARROW_RIGHT_PRICE,0,time,price);
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_ANCHOR,ANCHOR_LEFT);
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_COLOR,col);
     }

   else ObjectMove(ObjIdentifier+name,0,time,price);

  }

//+------------------------------------------------------------------+
//| Create price label function                                      |
//+------------------------------------------------------------------+
void CreatePriceLabel(string name,string desc,double price,color col) export
  {
   int Chart_Scale,Bar_Width;
   Chart_Scale=ChartScaleGet();

   if(Chart_Scale==0) Bar_Width=64;
   else if(Chart_Scale == 1) Bar_Width = 32;
   else if(Chart_Scale == 2) Bar_Width = 16;
   else if(Chart_Scale == 3) Bar_Width = 9;
   else if(Chart_Scale == 4) Bar_Width = 5;
   else if(Chart_Scale == 5) Bar_Width = 3;
   else Bar_Width=2;

   datetime time=Time[0]+Period()*60*Bar_Width;

   if(ObjectFind(0,ObjIdentifier+name)!=0)
     {
      ObjectCreate(ObjIdentifier+name,OBJ_TEXT,0,time,price);
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_ANCHOR,ANCHOR_LEFT);
      ObjectSetString(0,ObjIdentifier+name,OBJPROP_TEXT,desc);
      ObjectSetString(0,ObjIdentifier+name,OBJPROP_FONT,"Arial");
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_FONTSIZE,8);
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_COLOR,col);
     }

   else ObjectMove(ObjIdentifier+name,0,time,price);
  }

//+------------------------------------------------------------------+
//| Previous period function                                         |
//+------------------------------------------------------------------+
void PrevPeriod(int period,double high,double low,string hiname,string lowname,int style,color col,int width)
  {
   PrevCandle(period,low,high);
   HLine(hiname,hiname,10,high,style,col,width);
   HLine(lowname,lowname,10,low,style,col,width);
  }

//+------------------------------------------------------------------+
//| Previous candle function                                         |
//+------------------------------------------------------------------+
void PrevCandle(int period,double &low,double &high)
  {
   low=iLow(_Symbol,period,1);
   high=iHigh(_Symbol,period,1);
  }
//+------------------------------------------------------------------+
