Help required with nested FOR loops and arrays (MQL5)

 
Hello everyone 

- The function below has been designed to get ENTRY & EXIT deals and their associated position IDS. This is working fine. 
- I have checked that the arrays are getting populated correctly and they build upon every iteration of their 'for' loops. 

PROBLEM: The issue is happening because for every OUTER loop there is an INNER loop which creates many duplicate entries (as shown below) 

2023.06.22 09:35:18.964 2023.06.01 04:00:01            [openDealTime] [symbolID] [openOrderID] [openDealID] [closeDealID] [positionID]
2023.06.22 09:35:18.964 2023.06.01 04:00:01   [0] 2023.06.01 03:00:02 "AUDJPY"               3            3             9            3
2023.06.22 09:35:18.964 2023.06.01 04:00:01            [openDealTime] [symbolID] [openOrderID] [openDealID] [closeDealID] [positionID]
2023.06.22 09:35:18.964 2023.06.01 04:00:01   [0] 2023.06.01 03:00:02 "AUDJPY"               3            3             9            3
2023.06.22 09:35:18.964 2023.06.01 04:00:01   [1] 2023.06.01 03:00:01 "AUDCAD"               2            2             8            2
2023.06.22 09:35:18.964 2023.06.01 04:00:01            [openDealTime] [symbolID] [openOrderID] [openDealID] [closeDealID] [positionID]
2023.06.22 09:35:18.964 2023.06.01 04:00:01   [0] 2023.06.01 03:00:02 "AUDJPY"               3            3             9            3
2023.06.22 09:35:18.964 2023.06.01 04:00:01   [1] 2023.06.01 03:00:01 "AUDCAD"               2            2             8            2
2023.06.22 09:35:18.964 2023.06.01 04:00:01   [2] 2023.06.01 03:00:04 "AUDUSD"               5            5             7            5 

- Now as the EA runs, for each OUTER loop there is its associated INNER loop. This then creates an entry that is duplicated upon every loop iteration. 

HELP REQD: 
- What I would like to do is to somehow create an array that holds ONLY single entries and NO duplicate ones.
- I have started my attempt (shown below) by trying to filter out duplicate entries but I keep getting the above output. 
- To simply assist (or point me in the right direction) how I might be able to remove the duplicate entries (based on the position ID) and maybe create a new array that could hold only the single positions?!?!? (maybe) 
- NOTE: I intend to run the function in my main EA after every new bar creation.  

Any help or assistance would be greatly appreciated - Many thanks in advance

void    _OutputTradesToCSVFile()
{
  //--- set the start and end date to request the history of deals
  datetime  startDate       = (TimeCurrent() - (PeriodSeconds(PERIOD_W1) * 2));   // from 2 weeks ago
  datetime  endDate         = TimeCurrent();                                      // till the current moment
  ulong     dealOutTicket   = -1;
  ulong     dealInTicket    = -1;
  // create TWO arrays to hold trades & unique trades 
  struct_TradeDetails tradesArray[];

  // RETRIEVE: ALL orders & deals for the previous TWO weeks 
  if(HistorySelect(startDate, endDate))             
  { // RETRIEVE: ONLY deals for the previous TWO weeks
    int totalDealsLoop = HistoryDealsTotal();  
    // LOOP: if "dealOutTicket = HistoryDealGetTicket(i)" AND the deal is an 'OUT' trade
    for( int i = totalDealsLoop; i >= 0; i-- )       
    {      
      if((dealOutTicket = HistoryDealGetTicket(i)) > 0 && 
                          HistoryDealGetInteger(dealOutTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT) 
      { // RETRIEVE: GET DEAL 'OUT' DATA
        ulong   positionID    = HistoryDealGetInteger(dealOutTicket, DEAL_POSITION_ID);  
        ulong   closeDealID   = HistoryDealGetInteger(dealOutTicket, DEAL_TICKET); 
        string  symbolID      = HistoryDealGetString(dealOutTicket, DEAL_SYMBOL);
        
        // RETRIEVE: ONLY deals with the previous CLOSING position ID & is an 'IN' trade
        for( int j = totalDealsLoop; j >= 0; j-- )
        { 
          if( (dealInTicket = HistoryDealGetTicket(j) ) > 0 && 
              HistoryDealGetInteger(dealInTicket, DEAL_ENTRY) == DEAL_ENTRY_IN && 
              HistoryDealGetInteger(dealInTicket, DEAL_POSITION_ID) == positionID)
          { // RETRIEVE: GET DEAL 'IN' DATA
            ulong     openOrderID     = HistoryDealGetInteger(dealInTicket, DEAL_ORDER);
            ulong     openDealID      = HistoryDealGetInteger(dealInTicket, DEAL_TICKET);
            datetime  openDealTime    = (datetime)HistoryDealGetInteger(dealInTicket, DEAL_TIME);
            
            // Create a new trade object
            struct_TradeDetails obj_trade;
            obj_trade.openDealTime    = openDealTime;
            obj_trade.symbolID        = symbolID;
            obj_trade.openOrderID     = openOrderID;
            obj_trade.openDealID      = openDealID;
            obj_trade.closeDealID     = closeDealID;
            obj_trade.positionID      = positionID;

            // Resize tradesArray[]
            ArrayResize( tradesArray, ArraySize(tradesArray) + 1 );                 

            // Add the trade values to the obj_trade object
            tradesArray[ArraySize(tradesArray) - 1] = obj_trade;

            ArrayPrint(tradesArray);
          }  
        }  
      } 
    }
    bool isDuplicate = false;  
    // 
    for( int k = 0; k < ArraySize(tradesArray); k++ )
    {
      for ( int l = 0; l < ArraySize(tradesArray); l++ )
      {
        if( tradesArray[k].positionID == tradesArray[l].positionID )
        {
          isDuplicate = true;
          break;
        }
      }

      if( !isDuplicate )
      {
        // Add the unique trade to uniqueTradesArray
        ArrayResize(tradesArray, ArraySize(tradesArray) + 1);
        tradesArray[ArraySize(tradesArray) - 1] = tradesArray[k];
      }   
    }
  }
}