//+------------------------------------------------------------------+
//|                                           InsideBarIndicator.mq4 |
//|                                  Copyright © 2008, Robert Bayles |
//|                                                                  |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2008, Robert Bayles"
#property link      "b.bayles@hotmail.com"
#property indicator_chart_window

extern string Note1 = "<--      Number of bars to test?     -->";
extern int intNumberOfBars = 100;
extern string Note2 = "<--      Indicator Line Length?      -->";
extern int intLineLength = 5;
extern string Note3 = "<--       Offset for Lines (Pips)?     -->";
extern int intLineOffset = 3;
//extern string Note4 = "<--        Line Style (0 to 4)?        -->";
//extern int intLineStyle = 2;
extern string Note5 = "<--        Line Width (1 to 5)?        -->";
extern int intLineWidth = 1;
extern string Note6 = "<--             Line Colors?              -->";
extern color colBuyLine = SeaGreen;
extern color colSellLine = Tomato;
extern string Note7 = "<--   Pop Up Alerts (True/False)?    -->";
extern bool bolAlerts = true;

datetime dtCurrentTime = 0;
double dblPoints;




//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+


int init()
{

   // Set point value...needed if broker uses 3 or 5 decimals in price
   GetPoints();
   // Verify that specified number of bars exists on chart
   if (intNumberOfBars > Bars) intNumberOfBars = Bars;

return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+

int deinit()
{
      DeleteObjects();

return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator start() function                                |
//+------------------------------------------------------------------+
int start()
{
   if (GetNewBar()== true)
   {
     DeleteObjects();
     DrawObjects();
     if (bolAlerts == true) ShowAlert(); 
   } // End if

return(0);

}  // End Function start()


//+------------------------------------------------------------------+
//| Function DeleteObjects()
//+------------------------------------------------------------------+

void DeleteObjects()
{
   for (int i=1; i <= intNumberOfBars; i++)
   {
      if (ObjectFind("IBBUY" + i) != -1)
      {
         ObjectDelete("IBBUY" + i);
         ObjectDelete("IBSELL" + i);
      }
   } // Next i
   
} //  End Function DeleteObjects()


//+------------------------------------------------------------------+
//| Function DrawObjects()   Create indicator bars
//+------------------------------------------------------------------+

void DrawObjects()
{
   // Loop through bars
   for ( int i = 1; i < intNumberOfBars; i++)
   {
      // Check to see if inside bar exists
      if(iHigh(NULL, 0, i) < iHigh(NULL, 0, i+1) && iLow(NULL, 0,i) > iLow(NULL, 0, i+1))
      {
         // Correct indicator line length if number of bars showing are less than specified length
         int intLength = intLineLength;
         if ( i < intLineLength) intLength = i+1;
         
         // Draw buy indicator
         ObjectCreate("IBBUY" + i, OBJ_TREND, 0, Time[i+1], iHigh(NULL, 0, i+1)+(intLineOffset * dblPoints), Time[(i+1) - intLength], iHigh(NULL, 0, i+1)+ (intLineOffset * dblPoints));
         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+1], iLow(NULL, 0, i+1)-(intLineOffset * dblPoints), Time[(i+1) - intLength], iLow(NULL, 0, i+1)- (intLineOffset * dblPoints));
         ObjectSet("IBSELL" + i, OBJPROP_RAY, false);
         ObjectSet("IBSELL" + i, OBJPROP_WIDTH, intLineWidth);
//         ObjectSet("IBSELL" + i, OBJPROP_STYLE, intLineStyle);
         ObjectSet("IBSELL" + i, OBJPROP_COLOR, colSellLine);
      } // End if
   } // Next i
    
} // End function DrawObjects()
       

//+------------------------------------------------------------------+
//| Function GetNewBar()
//+------------------------------------------------------------------+

bool GetNewBar()
{
   // Check for a new bar
   if (dtCurrentTime != Time[0])
   {
   dtCurrentTime = Time[0];
   return(true);
   }
   else return(false);
   
} // End Function GetNewBar()


//-------------------------------------------------------------------+
// Function GetPoints()  Correct point value if broker uses 3 or 5
//                       digits in price
//-------------------------------------------------------------------+

double GetPoints()
{
   if (Digits == 3 || Digits == 5) dblPoints = Point * 10;
   else dblPoints = Point;
   return(dblPoints);
   
}  // End Function GetPoints()


//+------------------------------------------------------------------+
//| Function ShowAlert()
//+------------------------------------------------------------------+

void ShowAlert()
{
   if(iHigh(NULL, 0, 1) < iHigh(NULL, 0, 2) && iLow(NULL, 0, 1) > iLow(NULL, 0, 2))
   {
      Alert("Inside bar on " + Symbol());
   } // End if
   
} // End function ShowAlert()

//+------------------------------------------------------------------+
//| End of program
//+------------------------------------------------------------------+

