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

saurabhsuman003  
      if(G1[GridArrayLevel].Grid_Buy_Pos==0 && CurrentLevel!=CurrentLevelCopy)
         //open buy order
        {
         ZeroMemory(request);
         ZeroMemory(result);
         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;
         request.sl =0;
         request.tp = 0;
         request.deviation = 50;
         OrderSend(request,result);
         if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
           {
            do
               Sleep(100);
            while(PositionSelect(_Symbol) == false);
            G1[GridArrayLevel].Grid_Buy_Pos_Ticket=result.deal;
            G1[GridArrayLevel].Grid_Buy_Pos=1;
           }
        }


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.

Reason: