Check the state of a market order

 

Hi, I am a beginner in writing EAs with MQL4 and I encountered a question that confused me. If I send an order of AUDUSD pair like the following:

OrderSend(Symbol(), OP_BUY, NormalizeDouble(lot_size, Digits), Ask, 3, 0, 0, "LONG", MAGICMA, 0, Green);

How could I know whether the market order has been successfully executed or not?

If the position is successfully opened, would the price be between Ask - 3 and Ask +3?

If it failed to open a new position, when and how could I know?

It will be super helpful for me if somebody could answer these questions clearly, since my algorithm needs to monitor the state of the order to decide what to do next.

 

Check the returned value of the OrderSend function

https://docs.mql4.com/en/trading/ordersend

OrderSend - Trade Functions - MQL4 Reference
OrderSend - Trade Functions - MQL4 Reference
  • docs.mql4.com
Returns number of the ticket assigned to the order by the trade server or -1 if it fails. To get additional error information, one has to call the GetLastError() function. At opening of a market order (OP_SELL or OP_BUY), only the latest prices of Bid (for selling) or Ask (for buying) can be used as open price. If operation is performed with a...
 
bizl13: If the position is successfully opened, would the price be between Ask - 3 and Ask +3?

If it failed to open a new position, when and how could I know?

  1. On a dealing desk broker it would be Ask ± 3 × Points. On ECN brokers the spread is ignored.
  2. Check your return codes for errors, and report them including GLE/LE, your variable values, and the market. That way we would know that at least you are calling your code.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

 
bizl13:

Hi, I am a beginner in writing EAs with MQL4 and I encountered a question that confused me. If I send an order of AUDUSD pair like the following:

OrderSend(Symbol(), OP_BUY, NormalizeDouble(lot_size, Digits), Ask, 3, 0, 0, "LONG", MAGICMA, 0, Green);

How could I know whether the market order has been successfully executed or not?

If the position is successfully opened, would the price be between Ask - 3 and Ask +3?

If it failed to open a new position, when and how could I know?

It will be super helpful for me if somebody could answer these questions clearly, since my algorithm needs to monitor the state of the order to decide what to do next.

int ticket=0;


ticket=OrderSend(Symbol(), OP_BUY, NormalizeDouble(lot_size, Digits), Ask, 3, 0, 0, "LONG", MAGICMA, 0, Green);
if(ticket<1)
  {
   Print("Order send error BUY order, errcode : ",GetLastError());// Fetch error code here and print the reason
   return;                                                        // in experts journal.  
  }
else
   Print("Order BUY sent successfully");// If order successfully sent, print it in experts journal
 
Thanks for everyone's comment. It really helps!