 
#property copyright ""
#property link      ""

//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 0

//---- constants 
#define  ShortName         "Inside Bar"
#define  OLabel            "IB"
#define  Shift             1

//---- indicator parameters
extern string St_Ex                 = "------- Indicator Settings";
extern int    MinRange              = 1;
extern int    MaxRange              = 4;
extern int    MaxHistoryBars        = 500;
extern string Se_Ex3                = "------- Drawing Boxes";
extern color  BoxColor              = Teal;
extern int    BoxSize               = 2;
extern string Alerts_ex             = "------- Alerts";
extern string AlertCaption          = "My Alert";
extern bool   DisplayAlerts         = false;
extern bool   EmailAlerts           = false;
extern bool   SoundAlerts           = false;
extern string SoundFile             = "alert.wav";

//---- Alerts
static datetime TimeStamp;
static int AlertCount = 1;
static bool AlertTriggered = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   // Delete old Objects 
   DeleteObjects();
   
   // Name and labels
   IndicatorShortName(ShortName);
   
   // Comment
   Comment("");
   
   // Bye
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int DeleteObjects()
{
   int obj_total=ObjectsTotal();
   for(int i = obj_total - 1; i >= 0; i--)
     {
       string label = ObjectName(i);
       if(StringFind(label, OLabel)==-1) continue;
       ObjectDelete(label); 
     }     
   return(0);
}
int deinit()
{
   DeleteObjects();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
     // More vars here too...
     int start = 1;
     int limit;
     int counted_bars = IndicatorCounted();

     // check for possible errors
     if(counted_bars < 0) 
        return(-1);
        
     // Only check these
     limit = Bars - 1 - counted_bars;
     
     // Check the signal foreach bar
     for(int i = limit; i >= start; i--)
     {       
         // Ignore more than X past bars 
         if(i > MaxHistoryBars) continue;  
         
         // No Alert 
         AlertTriggered = false;
         
         // Some Vars I need
         double l_highest;
         double l_lowest;
         double l_high;
         double l_low;
         int l_shift;
         
         //-- Draw
         for(int rn = MaxRange; rn >= MinRange; rn--)
         { 
            // Local vars
            l_highest = DHighest(i,rn);
            l_lowest  = DLowest(i,rn);
            l_shift = i + rn;
            
            // High/Low of first bar 
            l_high = High[l_shift];
            l_low  = Low[l_shift];
            
            // Print
            if(l_high > l_highest && l_low < l_lowest) 
            {
               AlertTriggered = true;
               if(DrawRectangle(i, rn+1, BoxColor, false))
                  break;
            } 
         }  
     }
     
   // Alerts
   if(TimeStamp != Time[0])
   {
         if(AlertTriggered && AlertCount == 0)
         {
            if(DisplayAlerts == true) { Alert( ShortName +" ("+ AlertCaption +") ["+ Symbol() +"] Inside Bar"); }
            if(EmailAlerts == true)   { SendMail(ShortName +" ("+ AlertCaption +") ["+ Symbol() +"]", "["+ Symbol() +"] Inside Bar"); }
            if(SoundAlerts == true)   { PlaySound(SoundFile); }
         } 
         TimeStamp = Time[0];
         AlertCount = 0;
   }
   
   // Bye Bye
   return(0);
}

bool DrawRectangle(int shift, int pshift, color vcolor, bool track = true)
{
   // Time
   datetime x1 = Time[shift];
   datetime x2 = Time[shift+pshift-1];
   
   // Name
   string label = OLabel +"Rect-"+ x2;
   if(ObjectFind(label) != -1) ObjectDelete(label);
    
   // Distance
   int dist = shift + pshift;
   
   // Highest and lowest
   double rhigh = iHigh(Symbol(),Period(),iHighest(Symbol(), Period(), MODE_HIGH, pshift-1, shift+1));
   double rlow  = iLow(Symbol(),Period(),iLowest(Symbol(), Period(), MODE_LOW, pshift-1, shift+1));
   
   // Create
   ObjectCreate(label, OBJ_RECTANGLE, 0, x1, rlow, x2, rhigh);
   ObjectSet(label, OBJPROP_COLOR, vcolor);
   ObjectSet(label, OBJPROP_WIDTH, BoxSize);
   ObjectSet(label, OBJPROP_BACK, false);
   return(true);
}

double DHighest(int shift, int period)
{
   return(iHigh(Symbol(),Period(),iHighest(Symbol(), Period(), MODE_HIGH, period,shift)));  
}

double DLowest(int shift, int period)
{
   return(iLow(Symbol(),Period(),iLowest(Symbol(), Period(), MODE_LOW, period, shift)));
}