//+------------------------------------------------------------------+
//|                             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 ;


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()
{                    //-- 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());
   }   
} 
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+
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);
  } 
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+ 



//+------------------------------------------------------------------+
//|                             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);
//  } 
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+ 
