Some questions about CTrade PositionOpen()

 

I was using MqlTradeRequest and OrderSend() to open my positions. But now I am thinking of updating my code and to use CTrade and PositionOpen() instead.

But I have a couple of doubts about it. (Deviation, MagicNumber and How to handle errors).


In the following code:
Is it properly structured? (Deviation and MagicNumber)

If an error occurs in the PositionOpen() function, is there a possibility that a misconfigured position will be opened? For example, without stop loss? I will need close it using PositionClose()?

Thank you so much!!


   ulong Send(double pVolume, double pStopLoss, double pTakeProfit)
   {
         trade.SetTypeFilling(ORDER_FILLING_FOK);
         trade.SetExpertMagicNumber(magicNumber);
         trade.SetDeviationInPoints(1);
         
         bool res = trade.PositionOpen
         (
               Symbol(),                                 
               ORDER_TYPE_BUY,                           
               pVolume,                                  
               SymbolInfoDouble(Symbol(), SYMBOL_ASK),   
               pStopLoss,                                
               pTakeProfit,                              
               IntegerToString(magicNumber)              
          );
          
         if(!res) 
         {
            PrintFormat("PositionOpen Error: <<INVALID PARAMETERS>>");
            return -1;
         }
          
         ulong errorCode = trade.ResultRetcode();
         if(errorCode!=TRADE_RETCODE_DONE)
         {
            PrintFormat("PositionOpen Error: ", errorCode);
            return -1;         
         }
          
         return trade.ResultDeal();//tiket
   }
Reason: