//+------------------------------------------------------------------+
//|                        TheStratBasicLive.mq5                     |
//| Real-time Strat Labeling (1 = Inside, 2 = Directional, 3 = Outside) |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "Dummy"
#property indicator_type1   DRAW_NONE
#property indicator_color1  clrWhite

input color  InsideBarColor      = clrRed;
input color  DirectionalBarColor = clrWhite;
input color  OutsideBarColor     = clrAqua;
input int    FontSize            = 10;
input string FontName            = "Arial";
input bool   ShowAboveCandle     = true;
input int    BarsToLabel         = 100; // NEW: Number of recent bars to label

double dummyBuffer[];

//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0, dummyBuffer, INDICATOR_DATA);
   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
void DeleteAllStratLabels()
{
   int total = ObjectsTotal(0);
   for (int i = total - 1; i >= 0; i--)
   {
      string name = ObjectName(0, i);
      if (StringFind(name, "StratLabel_") == 0)
         ObjectDelete(0, name);
   }
}

//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{
   if (rates_total < 2)
      return 0;

   if (prev_calculated == 0)
      DeleteAllStratLabels();

   // Load price data
   double high[], low[];
   datetime time[];

   if (CopyHigh(NULL, 0, 0, rates_total, high) <= 0) return 0;
   if (CopyLow(NULL, 0, 0, rates_total, low) <= 0) return 0;
   if (CopyTime(NULL, 0, 0, rates_total, time) <= 0) return 0;

   // Calculate the range of bars to label
   int start = MathMax(rates_total - BarsToLabel, 1);
   for (int i = start; i < rates_total; i++)
   {
      string labelId = "StratLabel_" + IntegerToString(i);
      ObjectDelete(0, labelId);

      double currHigh = high[i];
      double currLow  = low[i];
      double prevHigh = high[i - 1];
      double prevLow  = low[i - 1];

      string label = "1";
      color labelColor = InsideBarColor;

      bool brokeHigh = currHigh > prevHigh;
      bool brokeLow  = currLow < prevLow;

      if (brokeHigh && brokeLow)
      {
         label = "3";
         labelColor = OutsideBarColor;
      }
      else if (brokeHigh || brokeLow)
      {
         label = "2";
         labelColor = DirectionalBarColor;
      }

      double barRange = currHigh - currLow;
      double yOffset = ShowAboveCandle ? currHigh + barRange * 0.3 : currLow - barRange * 0.3;

      if (!ObjectCreate(0, labelId, OBJ_TEXT, 0, time[i], yOffset))
         continue;

      ObjectSetInteger(0, labelId, OBJPROP_COLOR, labelColor);
      ObjectSetInteger(0, labelId, OBJPROP_FONTSIZE, FontSize);
      ObjectSetInteger(0, labelId, OBJPROP_SELECTABLE, false);
      ObjectSetInteger(0, labelId, OBJPROP_HIDDEN, true);
      ObjectSetString(0, labelId, OBJPROP_FONT, FontName);
      ObjectSetString(0, labelId, OBJPROP_TEXT, label);
   }

   return rates_total;
}
