Download MetaTrader 5

Question regarding OrderClose error 4051 (read all documentation!)

To add comments, please log in or register
davmartin
33
davmartin  

Hi,


Trying to understand why I got the OrderClose error 4051 (invalid ticket for OrderClose function). 

I did read all the documentation I could find on google searching for the error code

Also read https://www.mql5.com/en/forum/139592 .


But because I am writing my first EA I don't fully understand all the information provided. 


Please see the code: 

// Exit Trading Position Long

if (Fixed_Target_Long > Bid) 
   {   
    Ticket = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Red);
    return(0);
   }
      
// Exit Trading Position Short

if (Fixed_Target_Long < Ask) 
   {   
    Ticket2 = OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
    return(0);
   }


Fixed_Target_Long = I just set it at 50 pips to try it out. 


Appreciate all the help!!

Dave

What are Function return values ? How do I use them ?
What are Function return values ? How do I use them ?
  • 2012.05.20
  • www.mql5.com
I see many, many people posting code and asking questions which can usually be answered by simply checking the return values from a few Functions a...
Drazen Penic
2699
Drazen Penic  
  1. You must call OrderSelect() before you call OrderClose() or any other OrderXXX() function. Otherwise, result of the OrderClose() and other functions is undefined.
  2. OrderClose() returns boolean which indicates if an order is closed or not. It definitely does not return ticket number.
  3. Use OrderClosePrice() instead of Bid and Ask. It automatically knows which price to use depending on order type - buy or sell.
  4. How is variable "Slippage" defined in your code? What's its value?


  //Simplified example
  if(OrderSelect(idx, SELECT_BY_POS)) 
    { 
     if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red))
       return(0);
     else
       // Order not closed, do error handling and recovery here
       Print("OrderClose() returned the error of ",GetLastError());
    } 


davmartin
33
davmartin  
Drazen Penic:
  1. You must call OrderSelect() before you call OrderClose() or any other OrderXXX() function. Otherwise, result of the OrderClose() and other functions is undefined.
  2. OrderClose() returns boolean which indicates if an order is closed or not. It definitely does not return ticket number.
  3. Use OrderClosePrice() instead of Bid and Ask. It automatically knows which price to use depending on order type - buy or sell.
  4. How is variable "Slippage" defined in your code? What's its value?




Thanks Drazen for the information.


Still trying to make it work :)

To add comments, please log in or register