My EA is not closing the intended trades. It gets the wrong type of position ticket. How can i get the correct one?

      if(G1[GridArrayLevel].Grid_Buy_Pos==0 && CurrentLevel!=CurrentLevelCopy)
         //open buy order
         request.magic = g_4magic;
         request.action = TRADE_ACTION_DEAL;
         request.type = ORDER_TYPE_BUY;
         request.symbol = _Symbol;
         request.volume = TradeVolume;
         request.type_filling = ORDER_FILLING_IOC;
         request.price = G1[GridArrayLevel].Grid_Level; =0; = 0;
         request.deviation = 50;
         if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
            while(PositionSelect(_Symbol) == false);

I used the above code to store ticket of a position. the ticket stored was 210877044 while that shown beside position was 210921939.  The journal shows both of them related to same market order.

perhaps,because of this confusion my EA is not closing any trades.

moreover it worked fine while backtesting, and this error poped up during testing on a demo account.

what can i do to get the ticket number shown beside my position in trade-toolbox window.