what is the logic behind the following Expert advisor:

//+------------------------------------------------------------------+
//|                                           DailyRangeBreakout.mq5 |
//|                        One trade per day – simple range breakout |
//+------------------------------------------------------------------+
#property version   "1.01"
#property script_show_inputs

#include <Trade\Trade.mqh>

//---- INPUTS
input double LotSize        = 0.01;   // fixed lot
input int    RangeEndHour   = 8;      // 08:00 broker time
input int    RangeEndMinute = 0;
input int    BufferPoints   = 10;     // extra buffer beyond range
input double RiskReward     = 2.0;    // TP = SL * RR
input ulong  MagicNumber    = 111222;

//---- GLOBALS
CTrade       trade;
datetime     gToday;                 // date of current bar (00:00)
datetime     gRangeEnd;              // today 08:00
double       gRangeHigh, gRangeLow;  // high / low inside range window
bool         gRangeFixed  = false;   // true after 08:00
bool         gTradeDone   = false;   // true once one order triggered

//+------------------------------------------------------------------+
//| Helper: midnight of the given time                               |
//+------------------------------------------------------------------+
datetime Midnight(datetime t)
{
   MqlDateTime dt;
   TimeToStruct(t, dt);
   dt.hour = dt.min = dt.sec = 0;
   return StructToTime(dt);
}

//+------------------------------------------------------------------+
//| Init                                                             |
//+------------------------------------------------------------------+
int OnInit()
{
   trade.SetExpertMagicNumber(MagicNumber);
   trade.SetDeviationInPoints(3);
   Print("DailyRangeBreakout EA started – Magic ", MagicNumber);
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Deinit                                                           |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   trade.PositionClose(_Symbol);      // optional safety
}

//+------------------------------------------------------------------+
//| Main tick                                                        |
//+------------------------------------------------------------------+
void OnTick()
{
   datetime now  = TimeCurrent();
   datetime mid  = Midnight(now);

   // ---------- NEW DAY RESET ---------- 
   if(mid != gToday)
   {
      gToday       = mid;
      gRangeEnd    = mid + RangeEndHour*3600 + RangeEndMinute*60;
      gRangeHigh   = 0;
      gRangeLow    = 1e9;
      gRangeFixed  = false;
      gTradeDone   = false;

      // delete yesterday’s pending orders
      for(int i=OrdersTotal()-1;i>=0;i--)
         if(OrderSelect(OrderGetTicket(i)))
            if(OrderGetInteger(ORDER_MAGIC)==MagicNumber &&
               OrderGetString(ORDER_SYMBOL)==_Symbol)
               trade.OrderDelete(OrderGetTicket(i));
   }

   // ---------- INSIDE RANGE WINDOW ---------- //
   if(now < gRangeEnd)
   {
      gRangeHigh = fmax(gRangeHigh, iHigh(_Symbol, PERIOD_CURRENT, 0));
      gRangeLow  = fmin(gRangeLow , iLow (_Symbol, PERIOD_CURRENT, 0));
      return;                         // still building range
   }

   // ---------- RANGE FIXED, PLACE PENDING ORDERS ONCE ---------- 
   if(!gRangeFixed && OrdersTotal()==0)
   {
      double buf   = BufferPoints * _Point;
      double buyP  = gRangeHigh + buf;
      double sellP = gRangeLow  - buf;
      double slPts = (gRangeHigh - gRangeLow + 2*buf);
      double tpPts = slPts * RiskReward;

      trade.BuyStop(LotSize, buyP, _Symbol,
                    buyP - slPts, buyP + tpPts,
                    ORDER_TIME_GTC, 0, "RangeBuy");

      trade.SellStop(LotSize, sellP, _Symbol,
                     sellP + slPts, sellP - tpPts,
                     ORDER_TIME_GTC, 0, "RangeSell");

      gRangeFixed = true;
   }

   // ---------- AFTER TRIGGER: DELETE OPPOSITE PENDING ---------- 
   if(!gTradeDone)
   {
      bool buyFilled  = false;
      bool sellFilled = false;

      for(int i = OrdersTotal()-1; i>=0; i--)
      {
         ulong tk = OrderGetTicket(i);
         if(!OrderSelect(tk)) continue;

         if(OrderGetInteger(ORDER_MAGIC)!=MagicNumber ||
            OrderGetString(ORDER_SYMBOL)!=_Symbol)
            continue;

         ENUM_ORDER_TYPE type = (ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
         double price = OrderGetDouble(ORDER_PRICE_OPEN);

         if(type == ORDER_TYPE_BUY_STOP &&
            SymbolInfoDouble(_Symbol, SYMBOL_BID) >= price)
         {
            buyFilled = true;
            break;
         }
         if(type == ORDER_TYPE_SELL_STOP &&
            SymbolInfoDouble(_Symbol, SYMBOL_ASK) <= price)
         {
            sellFilled = true;
            break;
         }
      }

      // ---- delete opposite pending order ---- 
      if(buyFilled || sellFilled)
      {
         gTradeDone = true;
         for(int i = OrdersTotal()-1; i>=0; i--)
         {
            ulong tk = OrderGetTicket(i);
            if(!OrderSelect(tk)) continue;

            if(OrderGetInteger(ORDER_MAGIC)==MagicNumber &&
               OrderGetString(ORDER_SYMBOL)==_Symbol)
            {
               ENUM_ORDER_TYPE t = (ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
               if((buyFilled  && t == ORDER_TYPE_SELL_STOP) ||
                  (sellFilled && t == ORDER_TYPE_BUY_STOP))
                  trade.OrderDelete(tk);
            }
         }
      }
   }
}
//+------------------------------------------------------------------+