return value of orderclose should be checked

Enivid

Administrator
Staff member
Nov 30, 2008
18,720
1,394
144
Odesa
www.earnforex.com
You can solve it by checking the return value of OrderClose, obviously! For example, like this:
MQL4:
bool res=OrderClose(Ticket,Lots,CurrentPrice,Slippage,Red);
      //If the close was successful print the resul and exit the function
      if(res){
         Print("TRADE - CLOSE SUCCESS - Order ",Ticket," closed at price ",CurrentPrice);
         break;
      }
      //If the close failed print the error
      else{
         int Error=GetLastError();
         string ErrorText=GetLastErrorText(Error);
         Print("ERROR - CLOSE FAILED - error closing order ",Ticket," return error: ",Error," - ",ErrorText);
      }
 

hayseed

Master Trader
Jul 27, 2010
1,059
263
149
usa
how can i solve this warning.....

return value of orderclose should be checked,
//----

it always helps if you include the function......otherwise we often are shooting in the dark......

notice in enivids code it begins with, bool res=OrderClose( ........

quite often people, including me, might not use the bool res= part....... they will use only the bare OrderClose( ........

not using the bool res =, or something similar, will give the warning you mentioned....... so that is my shot in the dark.....

in the code below, there are two versions of void closeallprofitable()........

the first version will return the error...... the second version will not......

the only difference is result =.........

enivids code also includes the getlasterror() part, which is a good idea......h

//-----




Code:
void closeallprofitable()
  {
//---
   Print("profit alert ea closed at "+DoubleToStr(profit,0)+" $");

 
   if(!enablecloseall) {Alert("please enable close all");}
 
   if(enablecloseall)
   {

  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    if(OrderSelect(i, SELECT_BY_POS) == true)
   
    if(OrderProfit() < minimumprofit)      continue;
   
    bool result = false;

    if((OrderProfit() > minimumprofit) && OrderType() == OP_BUY)
     {
     OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
     }
   
   
    if((OrderProfit() > minimumprofit) && OrderType() == OP_SELL)
     {
     OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
     }
    }
   }
 
 
   }


   
//------
//------


void closeallprofitable()
  {
//---
   Print("profit alert ea closed at "+DoubleToStr(profit,0)+" $");

 
   if(!enablecloseall) {Alert("please enable close all");}
 
   if(enablecloseall)
   {

  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    if(OrderSelect(i, SELECT_BY_POS) == true)
   
    if(OrderProfit() < minimumprofit)      continue;
   
    bool result = false;

    if((OrderProfit() > minimumprofit) && OrderType() == OP_BUY)
     {
     result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
     }
   
   
    if((OrderProfit() > minimumprofit) && OrderType() == OP_SELL)
     {
     result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
     }
    }
   }
 
 
   }


   
//------
//------
 

TomDominic

Trader
Apr 30, 2023
40
1
14
26
Kenya
You can solve it by checking the return value of OrderClose, obviously! For example, like this:
MQL4:
bool res=OrderClose(Ticket,Lots,CurrentPrice,Slippage,Red);
      //If the close was successful print the resul and exit the function
      if(res){
         Print("TRADE - CLOSE SUCCESS - Order ",Ticket," closed at price ",CurrentPrice);
         break;
      }
      //If the close failed print the error
      else{
         int Error=GetLastError();
         string ErrorText=GetLastErrorText(Error);
         Print("ERROR - CLOSE FAILED - error closing order ",Ticket," return error: ",Error," - ",ErrorText);
      }

You can solve it by checking the return value of OrderClose, obviously! For example, like this:
MQL4:
bool res=OrderClose(Ticket,Lots,CurrentPrice,Slippage,Red);
      //If the close was successful print the resul and exit the function
      if(res){
         Print("TRADE - CLOSE SUCCESS - Order ",Ticket," closed at price ",CurrentPrice);
         break;
      }
      //If the close failed print the error
      else{
         int Error=GetLastError();
         string ErrorText=GetLastErrorText(Error);
         Print("ERROR - CLOSE FAILED - error closing order ",Ticket," return error: ",Error," - ",ErrorText);
      }
thanks,,,and i belief it applies to other warnings for other functions,like orderdelete....
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,720
1,394
144
Odesa
www.earnforex.com
thanks,,,and i belief it applies to other warnings for other functions,like orderdelete....
Yes, of course. They all may fail and it's a good idea to let the trader know about why it failed. Or it's even a better idea to code it so that it tries to Open/Close/Delete/Modify an order in a loop for up to N attempts before it gives up.
 

TomDominic

Trader
Apr 30, 2023
40
1
14
26
Kenya
helloo...
iam getting warnings whenever i compile my code from this lines ...
1.OrderSelect(ticket, SELECT_BY_TICKET);
2. OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
3. OrderDelete(ticket,Red);
and i have been trying to correct it but iam not getting it the right way please heelp me to rectify it.
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,720
1,394
144
Odesa
www.earnforex.com
The reason you are getting the warnings is that you aren't checking the return values of those functions. Don't just call it like this:
OrderSelect(ticket, SELECT_BY_TICKET);
Check if there is an error in each call. Like this:
MQL4:
if (!OrderSelect(ticket, SELECT_BY_TICKET))
{
    Print("Error selecting order with ticket #", ticket, ": ", GetLastError());
    continue; // If this is in the cycle and you want to avoid working with the order that you failed to select.
}
 
  • 👍
Reactions: TomDominic