Stop Trading Today when Loss or Consecutive Loss - page 2

 
anuj71 #:
void TodayLossControlling(double todayPL)
void CheckConsecutiveLossControl(CHistOrder &arr[])

Remove these two functions for now. Just get a sorted array of today's orders. After this, it will not be difficult to write code that gets the data from the prepared array.

 

Try 

// ===============================================================
//                   LOSS CONTROL FUNCTION
// ===============================================================
void LossControl()
  {
// If both protections disabled, skip function
   if(TodayMaxLoss <= 0 && ConsecutiveTradeLoss <= 0)
      return;

// -----------------------------------------
// Detect new day → reset protections
// -----------------------------------------
   datetime todayStart = StrToTime(TimeToString(TimeCurrent(), TIME_DATE));

   if(todayStart != gPausedDate)
     {
      TodayLossEAPaused = false;
      ConsecutiveLossPaused = false;
     }

// -----------------------------------------
// EARLY EXIT IF EA IS ALREADY PAUSED
// -----------------------------------------
   if(TodayLossEAPaused || ConsecutiveLossPaused)
      return;

// -----------------------------------------
// Variables to calculate for today only
// -----------------------------------------
   double todayPL = 0;
   int ConsecutiveLoss = 0;

// -----------------------------------------
// Scan only today's orders (fast loop)
// -----------------------------------------
   for(int i = OrdersHistoryTotal() - 1; i >= 0; i--)
     {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
         continue;
      if(OrderMagicNumber() != Magic)
         continue;

      datetime ct = OrderCloseTime();

      // Stop when reaching older day (yesterday or earlier)
      if(ct < todayStart)
         break;

      // ---- Today’s total P/L ----
      todayPL += OrderProfit() + OrderSwap() + OrderCommission();

      // ---- Consecutive loss counter ----
      if(OrderProfit() < 0)
         ConsecutiveLoss++;
      else
         ConsecutiveLoss = 0;
     }

// ==============================================================
// DAILY LOSS CONTROL
// ==============================================================
   if(TodayMaxLoss > 0)
     {
      double maxLossAllowed =
         (TodayCutLossType == BalancePercentage)
         ? (AccountBalance() * TodayMaxLoss / 100.0)
         : TodayMaxLoss;

      if(todayPL <= -MathAbs(maxLossAllowed))
        {
         TodayLossEAPaused = true;   // lock EA for the rest of day
         gPausedDate = todayStart;
        }
     }

// ==============================================================
// CONSECUTIVE LOSS CONTROL
// ==============================================================
   if(ConsecutiveLoss >= ConsecutiveTradeLoss && ConsecutiveTradeLoss > 0)
     {
      ConsecutiveLossPaused = true;  // lock EA for the rest of day
      gPausedDate = todayStart;
     }
  }