is there a limit of how much resources can an EA use?

 
Im creating an expert advisor that calculates stats of each magic number in Orderhistory() and printing them , the EA is working well on a small account with less that 30 magic numbers in order history .
however i tried this EA on an account with more than 100 magic numbers in OrderHistory() and a lot of trades and the calculations are not executed successfully , its printing 0 .
i will share my code down below
#property copyright "Copyright 2023, Quant-bot."
#property link      "https://www.quant-bot.com"
#property version   "1.00"
#property strict
input int timeinS = 60;
input double profactor = 1;
input int PFperiod = 2;
input int DrawdownPercentage = 5;
input int NumberofEAs = 350;
int OnInit()
  {
   EventSetTimer(timeinS);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTimer()
  {
   testequity();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void testequity()
  {
   int totalOrders = OrdersHistoryTotal();
   if(totalOrders == 0)
     {
      Print("No historical orders found.");
      return;
     }
   int magicNumbersWritten[];
   ArrayResize(magicNumbersWritten, NumberofEAs);
   int uniqueMagicNumbers[];
   int uniqueMagicNumbersCount = 0;
   ArrayResize(uniqueMagicNumbers, uniqueMagicNumbersCount + 1);
   for(int i = 0; i < totalOrders; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
         int currentMagicNumber = OrderMagicNumber();

         ArrayResize(uniqueMagicNumbers, uniqueMagicNumbersCount + 1);

         bool isUnique = true;

         // Check if the magic number has already been identified
         for(int j = 0; j < uniqueMagicNumbersCount; j++)
           {
            if(uniqueMagicNumbers[j] == currentMagicNumber)
              {
               isUnique = false;
               break;
              }
           }
         if(isUnique)
           {
            uniqueMagicNumbers[uniqueMagicNumbersCount] = currentMagicNumber;
            uniqueMagicNumbersCount++;
           }
        }
     }

   if(uniqueMagicNumbersCount == 0)
     {
      Print("No unique magic numbers found.");
      return;
     }

// Loop through all unique magic numbers and calculate metrics
   for(int i = 0; i < uniqueMagicNumbersCount; i++)
     {
      int currentMagicNumber = uniqueMagicNumbers[i];
      double e3 = 0.0;

      for(int s = totalOrders - 1; s >= 0; s--)
        {
         if(OrderSelect(s, SELECT_BY_POS, MODE_HISTORY))
           {
            if(OrderMagicNumber() == currentMagicNumber)
              {
               // Calculate profit factor for the last two trades with the same magic number
               double grossWin = 0.0;
               double grossLoss = 0.0;
               double profitFactor = 0.0;
               double halae = OrderProfit();

               // Loop through the last two trades with the same magic number
               for(int j = s - 1; j >= s - PFperiod && j >= 0; j--)
                 {
                  if(OrderSelect(j, SELECT_BY_POS, MODE_HISTORY))
                    {
                     if(OrderMagicNumber() == currentMagicNumber)
                       {
                        double orderProfit = OrderProfit();
                        double orderCommission = OrderCommission();
                        double orderSwap = OrderSwap();

                        if(orderProfit > 0)
                          {
                           grossWin += orderProfit + orderCommission + orderSwap;
                          }
                        else
                          {
                           grossLoss += orderProfit + orderCommission + orderSwap;
                          }
                       }
                    }
                 }

               // Calculate profit factor for the last two trades
               if(grossLoss == 0 && grossWin != 0)
                 {
                  profitFactor = grossWin / 1;
                 }
               else
                  if(grossLoss != 0)
                    {
                     profitFactor = grossWin / -grossLoss;
                    }

               if(profitFactor >= profactor)
                 {
                  e3 += halae;
                 }
              }
           }
        }
      Print("Magic Number: ", currentMagicNumber, ", e3: ", e3);
     }
  }
//+------------------------------------------------------------------+
 
nidalzd:
Im creating an expert advisor that calculates stats of each magic number in Orderhistory() and printing them , the EA is working well on a small account with less that 30 magic numbers in order history .
however i tried this EA on an account with more than 100 magic numbers in OrderHistory() and a lot of trades and the calculations are not executed successfully , its printing 0 .
i will share my code down below

I guess the issue is with the way you process and store your data.

Create a structure for the data you want to >accumulate<, representing your statistical analysis.

Use a hash map (std_lib has one) based on your magic numbers, and create a hash map containing these structures.

Now go through history >once< and store/accumulate your data for each entry in the structure. Get the structure by the magic number from the history entry, access the hash map with it.

This will make your loops much shorter in runtime. Currently you have exponential runtime. Doing it described way will reduce it to 2 times.

Result is the same, but runtime is much shorter.

Addendum:
Kill the timer once invoked, set it again before leaving the function.

This helps with extensive calls and runtime overlappings.

EDIT:

You can even break it down to one pass on all history data. And on subsequent calls, you can do incremental updates.

If you do it properly, this can be very efficient organized.

You need to understand how hash maps work.
Alternatively you could also use a red black tree to do it. But I would guess a hash map should be sufficient, as you won't be frequently adding new magic numbers, but only in the initial run.
 
nidalzd:
// Loop through the last two trades with the same magic number                for(int j = s - 1; j >= s - PFperiod && j >= 0; j--)

Beside the performance issue as noted by Dominik, you have a logical bug here :

// Loop through the last two trades with the same magic number
               for(int j = s - 1; j >= s - PFperiod && j >= 0; j--)