//+------------------------------------------------------------------+
//|                             Candle_InsideBar_HL_LEV.mq4 
//+------------------------------------------------------------------+

#property copyright "AHGDP"
#property link      "Candle_InsideBar_HL_LEV TEST PROBA ZA ALARM"  //--- This is modified. From someone, inside bar alert indi to outside bar with lines indi, and my little adjustment for an alert 

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 CLR_NONE
#property indicator_color2 CLR_NONE
#property indicator_color3 CLR_NONE
#property indicator_color4 CLR_NONE

extern int    intLineLength       = 8;
extern color  colBuyLine          = Yellow;
extern color  colSellLine         = Pink;

extern int    intLineStyle        = 0;
extern int    intLineWidth        = 2;

extern color  Candleup            = LimeGreen  ;
extern color  Candledn            = Red;

extern bool   bolAlerts          = False;
extern int    CountBars          = 300 ;

extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "out";
extern string             btn_Font              = "Arial";      //"Arial Black"
extern int                btn_FontSize          = 8;                            
extern color              btn_text_ON_color     = clrWhite;
extern color              btn_text_OFF_color    = clrRed;
extern string             btn_pressed           = "out off";            
extern string             btn_unpressed         = "out on";
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrDarkGray;
extern int                button_x              = 900;                                 
extern int                button_y              = 0;                                   
extern int                btn_Width             = 60;                                 
extern int                btn_Height            = 20;                    


bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix ,buttonId ;


double  TelBy,Dodji;
datetime dtCurrentTime = 0;
double dblPoints;

int    WickSizeAuto = 1;
int    BodySizeAuto;
bool   Deinitialized;  
int    Bar_Width, Chart_Scale;

double Buff1W[];
double Buff2W[];
double Buff1B[];
double Buff2B[];
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}

int init()
{
//---------------CANDLE AUTO OR NOT--------------------------------------------------
     Deinitialized = false; 
  Chart_Scale = ChartScaleGet();
                  
        if(Chart_Scale == 0) {BodySizeAuto = 1;  }
  else {if(Chart_Scale == 1) {BodySizeAuto = 1;  }      
  else {if(Chart_Scale == 2) {BodySizeAuto = 2;  } 
  else {if(Chart_Scale == 3) {BodySizeAuto = 3;  }
  else {if(Chart_Scale == 4) {BodySizeAuto = 6;  }
  else {BodySizeAuto = 13;}
   }}}}
//+--------------------------------------------------------------------------------
   
   SetIndexStyle (0,DRAW_HISTOGRAM, STYLE_SOLID, WickSizeAuto  , Candledn  );
   SetIndexBuffer(0,Buff1W);
   SetIndexStyle (1,DRAW_HISTOGRAM, STYLE_SOLID, WickSizeAuto  , Candleup  );
   SetIndexBuffer(1,Buff2W);
   SetIndexStyle (2,DRAW_HISTOGRAM, STYLE_SOLID, BodySizeAuto  , Candledn  );
   SetIndexBuffer(2,Buff1B);
   SetIndexStyle (3,DRAW_HISTOGRAM, STYLE_SOLID, BodySizeAuto  , Candleup  );
   SetIndexBuffer(3,Buff2B);
  
   GetPoints();
   if (CountBars > Bars) CountBars = Bars;
   
 IndicatorShortName("Candle_OutsideBar_HL_LEV bt");
  IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(WindowExpertName());
   IndicatorDigits(Digits);
   
   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
   show_data = val != 0;

   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix+btn_text;
   createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x);
   
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int deinit()
  {
  ObjectsDeleteAll(0,"out");
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
   
    DeleteObjects();
    return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (ChartID(),buttonID);
      ObjectCreate    (ChartID(),buttonID,OBJ_BUTTON,0,0,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XSIZE,width);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (ChartID(),buttonID,OBJPROP_FONT,font);
      ObjectSetString (ChartID(),buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YDISTANCE,9999);
}

bool recalc = true;

void handleButtonClicks()
{
   if (ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
      start();
   }
}


//
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)                                                    
  {
   handleButtonClicks();
  Chart_Scale = ChartScaleGet();
//  init();  
  }
//
//

int start()
{
//template code 
   handleButtonClicks();
   recalc = false;
   
   start2();
   
         SetIndexStyle(0,DRAW_HISTOGRAM);
         SetIndexStyle(1,DRAW_HISTOGRAM);
         SetIndexStyle(2,DRAW_HISTOGRAM);
         SetIndexStyle(3,DRAW_HISTOGRAM);
         DrawObjects();
   
   if (show_data)
   {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_unpressed);
   }
   else
   {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_pressed);
         SetIndexStyle(0,DRAW_NONE);
         SetIndexStyle(1,DRAW_NONE);
         SetIndexStyle(2,DRAW_NONE);
         SetIndexStyle(3,DRAW_NONE);
         DeleteObjects();
           
   }
   return(0);
}

int start2()
{
//=================== CANDLE COLOR  ==============================================
  int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
     SetIndexDrawBegin(0,Bars-CountBars);
     SetIndexDrawBegin(1,Bars-CountBars);
     SetIndexDrawBegin(2,Bars-CountBars);
     SetIndexDrawBegin(3,Bars-CountBars);
     SetIndexDrawBegin(4,Bars-CountBars);

//======================================================================================   
   for (int i=0; i<limit; i++)
   {
        Buff1B[i] = 0; Buff2B[i] = 0;    Buff1W[i] = 0; Buff2W[i] = 0;
        
     if (Open[i] < Close[i]) 
            { if   (      ( MathMax(High[i],Low[i] )  >  MathMax(High[i+1],Low[i+1]) ) 
                     &&   ( MathMin(High[i],Low[i] )  <  MathMin(High[i+1],Low[i+1]) )
                   )
                  {         Buff1W[i] = Low[i];
                            Buff2W[i] = High[i];
                            Buff1B[i] = Open[i];
                            Buff2B[i] = Close[i];
             }    }
     
     if (Open[i] > Close[i]) 
            { if   (      ( MathMax(High[i],Low[i] )  >  MathMax(High[i+1],Low[i+1]) ) 
                     &&   ( MathMin(High[i],Low[i] )  <  MathMin(High[i+1],Low[i+1]) )
                   )
                  {         Buff1W[i] = High[i];
                            Buff2W[i] = Low[i];
                            Buff1B[i] = Open[i];
                            Buff2B[i] = Close[i];
    }       }     } 
//=========================================================================================      
      
   if (GetNewBar()== true)
   {
     DeleteObjects();
     DrawObjects();
     if (bolAlerts == true) ShowAlert(); 
   } 
return(0);
}  
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

void DeleteObjects()
 {
   for (int i=1; i <= CountBars; i++)
   {
       if (  ObjectFind  ("IBBUY" + i) != -1)
          {  ObjectDelete("IBBUY" + i);
             ObjectDelete("IBSELL" + i);
  }  }  } 
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void DrawObjects()
{
   for ( int i = 1; i < CountBars; i++)
   {
        if ( MathAbs(Low[i]-High[i]) > Dodji*Point )
       
      if (   (MathMax(High[i],Low[i])  > MathMax(High[i+1],Low[i+1])) 
          && (MathMin(High[i],Low[i])  < MathMin(High[i+1],Low[i+1]))
         )
   {
         int intLength = intLineLength;
         if ( i < intLineLength) intLength = i+1;   
         
         // Draw buy indicator                                              //   Time[(i+1) - intLength]             Time[0]+Period()*60*10
         ObjectCreate("IBBUY" + i, OBJ_TREND, 0, Time[i], MathMax(High[i],Low[i]), Time[i]+Period() * (60 * intLineLength), MathMax(High[i],Low[i]));
         ObjectSet   ("IBBUY" + i, OBJPROP_RAY, false);
         ObjectSet   ("IBBUY" + i, OBJPROP_WIDTH, intLineWidth);
         ObjectSet   ("IBBUY" + i, OBJPROP_STYLE, intLineStyle);
         ObjectSet   ("IBBUY" + i, OBJPROP_COLOR, colBuyLine);
         
         // Draw sell indicator 
         ObjectCreate("IBSELL" + i, OBJ_TREND, 0, Time[i], MathMin(High[i],Low[i]), Time[i]+Period() * (60 * intLineLength), MathMin(High[i],Low[i]));
         ObjectSet   ("IBSELL" + i, OBJPROP_RAY, false);
         ObjectSet   ("IBSELL" + i, OBJPROP_WIDTH, intLineWidth);
         ObjectSet   ("IBSELL" + i, OBJPROP_STYLE, intLineStyle);
         ObjectSet   ("IBSELL" + i, OBJPROP_COLOR, colSellLine);
         
  }   }  }      
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
bool GetNewBar()
{
   if (dtCurrentTime != Time[0])
   {   dtCurrentTime = Time[0];
       return(true);
   }
   else return(false);  
} 
//-------------------------------------------------------------------+
//-------------------------------------------------------------------+
double GetPoints()
{
   if (Digits == 3 || Digits == 5) dblPoints = Point * 10;
   else dblPoints = Point;
   return(dblPoints); 
}  
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void ShowAlert()
{                    //-- changed < in to > so I have an alert when "mother" candle appears rather than inside bar
   if(iHigh(NULL, 0, 1) > iHigh(NULL, 0, 2) && iLow(NULL, 0, 1) < iLow(NULL, 0, 2))//------------------------- I have changed this section for an alert, please corect it! Down is an original code.
   {  Alert("Test " + Symbol());
   }   
} 
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+

//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+
int ChartScaleGet()
  {
  
   
  long result = -1;
  ChartGetInteger(0,CHART_SCALE,0,result);
  return((int)result);
  } 
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+ 



//+------------------------------------------------------------------+
//|                             Candle_InsideBar_HL_LEV.mq4 
//+------------------------------------------------------------------+

//#property copyright "AHGDP"
//#property link      "Candle_InsideBar_HL_LEV"

//#property indicator_chart_window
//#property indicator_buffers 4
//#property indicator_color1 CLR_NONE
//#property indicator_color2 CLR_NONE
//#property indicator_color3 CLR_NONE
//#property indicator_color4 CLR_NONE

//extern int    intLineLength       = 8;
//extern color  colBuyLine          = Yellow;
//extern color  colSellLine         = Pink;

//extern int    intLineStyle        = 0;
//extern int    intLineWidth        = 2;

//extern color  Candleup            = LimeGreen  ;
//extern color  Candledn            = Red;

//extern bool   bolAlerts          = False;
//extern int    CountBars          = 300 ;


//double  TelBy,Dodji;
//datetime dtCurrentTime = 0;
//double dblPoints;

//int    WickSizeAuto = 1;
//int    BodySizeAuto;
//bool   Deinitialized;  
//int    Bar_Width, Chart_Scale;

//double Buff1W[];
//double Buff2W[];
//double Buff1B[];
//double Buff2B[];
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//int init()
//{
//---------------CANDLE AUTO OR NOT--------------------------------------------------
//     Deinitialized = false; 
//  Chart_Scale = ChartScaleGet();
                  
//        if(Chart_Scale == 0) {BodySizeAuto = 1;  }
//  else {if(Chart_Scale == 1) {BodySizeAuto = 1;  }      
//  else {if(Chart_Scale == 2) {BodySizeAuto = 2;  } 
//  else {if(Chart_Scale == 3) {BodySizeAuto = 3;  }
//  else {if(Chart_Scale == 4) {BodySizeAuto = 6;  }
//  else {BodySizeAuto = 13;}
//   }}}}
//+--------------------------------------------------------------------------------
   
//   SetIndexStyle (0,DRAW_HISTOGRAM, STYLE_SOLID, WickSizeAuto  , Candledn  );
//   SetIndexBuffer(0,Buff1W);
//   SetIndexStyle (1,DRAW_HISTOGRAM, STYLE_SOLID, WickSizeAuto  , Candleup  );
//   SetIndexBuffer(1,Buff2W);
//   SetIndexStyle (2,DRAW_HISTOGRAM, STYLE_SOLID, BodySizeAuto  , Candledn  );
//   SetIndexBuffer(2,Buff1B);
//   SetIndexStyle (3,DRAW_HISTOGRAM, STYLE_SOLID, BodySizeAuto  , Candleup  );
//   SetIndexBuffer(3,Buff2B);
  
//   GetPoints();
//   if (CountBars > Bars) CountBars = Bars;
   

//return(0);
//}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//int deinit()
//  {
//    DeleteObjects();
//    return(0);
//  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//int start()
//{
//=================== CANDLE COLOR  ==============================================
//  int limit;
//   int counted_bars=IndicatorCounted();
//   if(counted_bars<0) return(-1);
//   if(counted_bars>0) counted_bars--;
//   limit=Bars-counted_bars;
   
//     SetIndexDrawBegin(0,Bars-CountBars);
//     SetIndexDrawBegin(1,Bars-CountBars);
//     SetIndexDrawBegin(2,Bars-CountBars);
//     SetIndexDrawBegin(3,Bars-CountBars);
//     SetIndexDrawBegin(4,Bars-CountBars);

//======================================================================================   
//   for (int i=0; i<limit; i++)
//   {
//        Buff1B[i] = 0; Buff2B[i] = 0;    Buff1W[i] = 0; Buff2W[i] = 0;
        
//     if (Open[i] < Close[i]) 
//            { if   (      ( MathMax(High[i],Low[i] )  >  MathMax(High[i+1],Low[i+1]) ) 
//                     &&   ( MathMin(High[i],Low[i] )  <  MathMin(High[i+1],Low[i+1]) )
//                   )
//                  {         Buff1W[i] = Low[i];
//                            Buff2W[i] = High[i];
//                            Buff1B[i] = Open[i];
//                            Buff2B[i] = Close[i];
//             }    }
     
//     if (Open[i] > Close[i]) 
//            { if   (      ( MathMax(High[i],Low[i] )  >  MathMax(High[i+1],Low[i+1]) ) 
//                     &&   ( MathMin(High[i],Low[i] )  <  MathMin(High[i+1],Low[i+1]) )
//                   )
//                  {         Buff1W[i] = High[i];
//                            Buff2W[i] = Low[i];
//                            Buff1B[i] = Open[i];
//                            Buff2B[i] = Close[i];
//    }       }     } 
//=========================================================================================      
      
//   if (GetNewBar()== true)
//   {
//     DeleteObjects();
//     DrawObjects();
//     if (bolAlerts == true) ShowAlert(); 
//   } 
//return(0);
//}  
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

//void DeleteObjects()
// {
//   for (int i=1; i <= CountBars; i++)
//   {
//       if (  ObjectFind  ("IBBUY" + i) != -1)
//          {  ObjectDelete("IBBUY" + i);
//             ObjectDelete("IBSELL" + i);
//  }  }  } 
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//void DrawObjects()
//{
//   for ( int i = 1; i < CountBars; i++)
//   {
//        if ( MathAbs(Low[i]-High[i]) > Dodji*Point )
       
//      if (   (MathMax(High[i],Low[i])  > MathMax(High[i+1],Low[i+1])) 
//          && (MathMin(High[i],Low[i])  < MathMin(High[i+1],Low[i+1]))
//         )
//   {
//         int intLength = intLineLength;
//         if ( i < intLineLength) intLength = i+1;   
         
         // Draw buy indicator                                              //   Time[(i+1) - intLength]             Time[0]+Period()*60*10
//         ObjectCreate("IBBUY" + i, OBJ_TREND, 0, Time[i], MathMax(High[i],Low[i]), Time[i]+Period() * (60 * intLineLength), MathMax(High[i],Low[i]));
//         ObjectSet   ("IBBUY" + i, OBJPROP_RAY, false);
//         ObjectSet   ("IBBUY" + i, OBJPROP_WIDTH, intLineWidth);
//         ObjectSet   ("IBBUY" + i, OBJPROP_STYLE, intLineStyle);
//         ObjectSet   ("IBBUY" + i, OBJPROP_COLOR, colBuyLine);
         
         // Draw sell indicator 
//         ObjectCreate("IBSELL" + i, OBJ_TREND, 0, Time[i], MathMin(High[i],Low[i]), Time[i]+Period() * (60 * intLineLength), MathMin(High[i],Low[i]));
//         ObjectSet   ("IBSELL" + i, OBJPROP_RAY, false);
//         ObjectSet   ("IBSELL" + i, OBJPROP_WIDTH, intLineWidth);
//         ObjectSet   ("IBSELL" + i, OBJPROP_STYLE, intLineStyle);
//         ObjectSet   ("IBSELL" + i, OBJPROP_COLOR, colSellLine);
         
//  }   }  }      
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//bool GetNewBar()
//{
//   if (dtCurrentTime != Time[0])
//   {   dtCurrentTime = Time[0];
//       return(true);
//   }
//   else return(false);  
//} 
//-------------------------------------------------------------------+
//-------------------------------------------------------------------+
//double GetPoints()
//{
//   if (Digits == 3 || Digits == 5) dblPoints = Point * 10;
//   else dblPoints = Point;
//   return(dblPoints); 
//}  
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//void ShowAlert()
//{
//   if(iHigh(NULL, 0, 1) < iHigh(NULL, 0, 2) && iLow(NULL, 0, 1) > iLow(NULL, 0, 2))
//   {  Alert("Inside bar on " + Symbol());
//   }   
//} 
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+
//void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)                                                    
//  {
//  Chart_Scale = ChartScaleGet();
//  init();  
//  }
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+
//int ChartScaleGet()
//  {
//  long result = -1;
//  ChartGetInteger(0,CHART_SCALE,0,result);
//  return((int)result);
//  } 
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+ 
