How do you check if the operation was successful?

 

Hi, i'm trying to do a reverse position, but for some reason if i keep clicking to reverse sometimes it just closes the position and doesn't reverse .. i think i have to check if the order was successful before allowing it to open the other order ... this return for me error retcode 10004 or 100021


But even though I know this, I don't even know how I do the checks to change this in this code, if someone can give me an example I would appreciate it


My code:


bool ReversePosition()
{
//trade is CTrade -- ASK AND BID Declaration in the body
  int i=PositionsTotal()-1;
  double totalvolume = 0;
  int TYPEPOS = PositionGetInteger(POSITION_TYPE);
  
   while(i>=0)
     {

         if(TYPEPOS == POSITION_TYPE_BUY)
           {

            if(trade.PositionClose(PositionGetSymbol(i)))
              {
               i--;
               totalvolume = NormalizeDouble(PositionGetDouble(POSITION_VOLUME),2);
               trade.Sell(totalvolume,_Symbol,BID,0,0,"Sell");
               totalvolume = 0;
               
              }
              
           }

         if(TYPEPOS == POSITION_TYPE_SELL)
           {

            if(trade.PositionClose(PositionGetSymbol(i)))
              {
               i--;
              totalvolume = NormalizeDouble(PositionGetDouble(POSITION_VOLUME),2);
              trade.Buy(totalvolume,_Symbol,ASK,0,0,"Buy");
               totalvolume = 0;
              }
           }
           
        }



     
   return(true);
}
 
this6767:

Hi, i'm trying to do a reverse position, but for some reason if i keep clicking to reverse sometimes it just closes the position and doesn't reverse .. i think i have to check if the order was successful before allowing it to open the other order ... this return for me error retcode 10004 or 100021


But even though I know this, I don't even know how I do the checks to change this in this code, if someone can give me an example I would appreciate it


My code:


retcode return 100021 [no quotes]


deviation i set = 100 but return 100021 again