//+------------------------------------------------------------------+
//|                                        Last10CandlesBox.mq4      |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "10 Candles Box Indicator"
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window

// Input parameters
input color BoxColor = clrDodgerBlue;     // Box color
input int BoxWidth = 2;                    // Box line width
input ENUM_LINE_STYLE BoxStyle = STYLE_SOLID; // Box line style
input bool ShowBackground = true;          // Show background fill
input color BackgroundColor = clrLightBlue; // Background color
input int BackgroundTransparency = 80;     // Background transparency (0-100)

string boxName = "Last10CandlesBox";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // Delete the box object
   ObjectDelete(0, boxName);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   // Calculate highest and lowest of last 10 candles
   double highest = High[1];
   double lowest = Low[1];
   
   for(int i = 1; i <= 10; i++)
   {
      if(High[i] > highest) highest = High[i];
      if(Low[i] < lowest) lowest = Low[i];
   }
   
   // Get the time of candle 10 (start of box) and candle 1 (end of box)
   datetime startTime = Time[10];
   datetime endTime = Time[0]; // Current candle
   
   // Delete old box if exists
   if(ObjectFind(0, boxName) >= 0)
   {
      ObjectDelete(0, boxName);
   }
   
   // Create rectangle box
   ObjectCreate(0, boxName, OBJ_RECTANGLE, 0, startTime, highest, endTime, lowest);
   ObjectSetInteger(0, boxName, OBJPROP_COLOR, BoxColor);
   ObjectSetInteger(0, boxName, OBJPROP_WIDTH, BoxWidth);
   ObjectSetInteger(0, boxName, OBJPROP_STYLE, BoxStyle);
   ObjectSetInteger(0, boxName, OBJPROP_BACK, ShowBackground);
   
   if(ShowBackground)
   {
      ObjectSetInteger(0, boxName, OBJPROP_FILL, true);
      
      // Apply transparency to background color
      int alpha = (int)((100 - BackgroundTransparency) * 255 / 100);
      color bgColor = (color)((alpha << 24) | (BackgroundColor & 0xFFFFFF));
      ObjectSetInteger(0, boxName, OBJPROP_BGCOLOR, bgColor);
   }
   
   ObjectSetInteger(0, boxName, OBJPROP_SELECTABLE, false);
   ObjectSetInteger(0, boxName, OBJPROP_SELECTED, false);
   ObjectSetInteger(0, boxName, OBJPROP_RAY_RIGHT, true); // Extend to current price
   
   return(rates_total);
}
//+------------------------------------------------------------------+