Order Failed Modify Buy [Position Already Closed]

 

I am using the below example code from Metaquote; however, I am getting the message [Position already closed]  and it would not enter the Stop Loss and Take Profit for my order. Below is my screen capture of the Trade Journal.

 

 

 

  sOP = "BUY";

      clOP = Aqua;

      l_cmd_36 =0;

      getT2 = ORDER_TYPE_BUY;

      l_price_0 = b1_symbol.Ask();

      l_price_8 = Ask - (b1_sl*b1_symbol.Point());

      l_price_16 = Ask + (b1_tp*b1_symbol.Point()); 

bool result = OrderSendMQ4(b1_symbol.Name(), l_cmd_36, OrderSize(), l_price_0, Slippage, l_price_8, l_price_16, sOP, Magic1);

 

 

bool OrderSendMQ4( string symbol_in, int cmd_in, double volume_in, double price_in, int slippage_in, 
                  double stoploss_in, double takeprofit_in, string comment_in=NULL, int magic_in=0) 
{
            MqlTradeRequest mrequest;  // To be used for sending our trade requests
            MqlTradeResult mresult;    // To be used to get our trade results
            MqlTradeCheckResult m_check_result;
            MqlTick latest_price; 
            
            
           if(!SymbolInfoTick(_Symbol,latest_price))
           {
            Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
            return(false);
           }
           
            ZeroMemory(mrequest);
            mrequest.action = TRADE_ACTION_DEAL;
            if(cmd_in==ORDER_TYPE_BUY)
            {                                  // immediate order execution
               mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // latest ask price         
               mrequest.type = ORDER_TYPE_BUY;               
            }
            if(cmd_in==ORDER_TYPE_SELL)
            {
               mrequest.price = NormalizeDouble(latest_price.bid,_Digits);           // latest ask price    
               mrequest.type = ORDER_TYPE_SELL;                                 
            }   
            mrequest.symbol = _Symbol;                                            // currency pair
            mrequest.volume = volume_in;                                                 // number of lots to trade
            mrequest.magic = magic_in;                                             // Order Magic Number
            mrequest.type_filling = ORDER_FILLING_FOK;                             // Order execution type
            mrequest.deviation=100;                                                // Deviation from current price
            mrequest.comment=comment_in;
            
            string action,result;
            //--- order check
            if(!OrderCheck(mrequest,m_check_result))
              {
               mresult.retcode=m_check_result.retcode;
               //printf(__FUNCTION__+": %s [%s]",FormatRequest(action,mrequest),FormatRequestResult(result,mrequest,mresult));
               Alert(__FUNCTION__ + " order check failed, " + mresult.retcode);               
               //--- copy return code
              }
              
              
               if (OrderSend(mrequest, mresult)==true)
               {
                  Alert("order has been successfully "); 
                  
                  if(takeprofit_in>0 && stoploss_in>0)
                  {
                     mrequest.action = TRADE_ACTION_SLTP;
                     mrequest.symbol = _Symbol;
                     mrequest.tp=takeprofit_in;
                     mrequest.sl=stoploss_in;
                     
                     if (OrderSend(mrequest, mresult)==true)
                     {
                        Alert("Order has been successfully set takeprofit/stoploss"); 
                     }           
                     else
                     {
                        Alert("Order failed to set takeprofit/stoploss, error:",GetLastError());
                        //orderSendFaildReason(mresult); 
                        ResetLastError();
                        return(false);    
                     }
                  }
               }           
               else
               {
                  Alert("order failed, error:",GetLastError());
                  ResetLastError();
                  return(false);    
               }
               
              return(true);
              
}
 
waterhorse:

I am using the below example code from Metaquote; however, I am getting the message [Position already closed]  and it would not enter the Stop Loss and Take Profit for my order. Below is my screen capture of the Trade Journal.

...

               if (OrderSend(mrequest, mresult)==true)

Return Value

In case of a successful basic check of structures (index checking) returns true. However, this is not a sign of successful execution of a trade operation. For a more detailed description of the function execution result, analyze the fields of result structure.

  • You don't need to send SL/TP after the order is open, set your SL/TP with your order.
 
angevoyageur:
  • You don't need to send SL/TP after the order is open, set your SL/TP with your order.
Thanks Alain for you help. I did a quickfix for now i.e. Sleep(1000); before modifying the SL and TP and it seems to be OK. 
Reason: