difficulty with void statement

 
I have corrected all the warnings except this one below. Usually the void function doesn't require a return operator,
so how do I add the if statement and extra pair of { } to the order close as the return value needs to be checked according to the meta editor. (post 600 build)


void closeAllFastEMATrades(string reason)
  {
   int cnt;
   for(cnt=OrdersTotal(); cnt>=0; cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderMagicNumber()==magicNumber)
           {
            if(OrderType()==OP_BUY)
              {
               Print(reason+" Closing BUY @ "+OrderProfit()+":ask="+Ask+":bid="+Bid);
               if(OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue))
                 {
                 }
               else if(OrderType()==OP_SELL) 
                 {
                  Print(reason+" Closing SELL @ "+OrderProfit()+":ask="+Ask+":bid="+Bid);
                  HERE----------->OrderClose(OrderTicket(),OrderLots(),Ask,3,Blue);

                 }
              }
           }
        }
     }
  }

Any clues gratefully accepted:)

Regards

Patrick

 

Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.

 
NotUnctuous:
I have corrected all the warnings except this one below. Usually the void function doesn't require a return operator,
so how do I add the if statement and extra pair of { } to the order close as the return value needs to be checked according to the meta editor. (post 600 build)


Any clues gratefully accepted:)

Regards

Patrick

void closeAllFastEMATrades(string reason)
  {
   for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()==magicNumber)
        {
         Print(reason+" Closing ",OrderType()==OP_BUY ? "BUY" : "SELL"," @ "+OrderProfit()+":ask="+Ask+":bid="+Bid);
         bool result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Blue);
        }
     }
  }
 
You code says "if it's a buy order try to close it. If the close fails and it is a sell order try to close it." Obviously not what you want.
if(OrderType()==OP_BUY)
  {
   Print(reason+" Closing BUY @ "+OrderProfit()+":ask="+Ask+":bid="+Bid);
   if(OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue))
     {
     }
   else if(OrderType()==OP_SELL) 
     {
      Print(reason+" Closing SELL @ "+OrderProfit()+":ask="+Ask+":bid="+Bid);
      HERE----------->OrderClose(OrderTicket(),OrderLots(),Ask,3,Blue);

     }
  }
If it's a buy order try to close it (or issue an error message,) otherwise close the sell (or issue.)
if(OrderType()==OP_BUY)                                                   
  {                                                                       
   Print(reason+" Closing BUY @ "+OrderProfit()+":ask="+Ask+":bid="+Bid); 
   if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue))
     {                                                                    
       Print("buy close failed "...);
     }                                                                    
} // buy
//   else if(OrderType()==OP_SELL)  Unnecessary test unless you open
else //  pending orders. Must be a sell
  {                                                                       
   Print(reason+" Closing SELL @ "+OrderProfit()+":ask="+Ask+":bid="+Bid);
   if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Blue))
     {                                                                    
       Print("sell close failed "...);
     }                                                                    
  }                                                                       
Or just use OrderClosePrice() instead of Bid/Ask and just close it.
 
NotUnctuous:
I have corrected all the warnings except this one below. Usually the void function doesn't require a return operator,
so how do I add the if statement and extra pair of { } to the order close as the return value needs to be checked according to the meta editor. (post 600 build)


Any clues gratefully accepted:)

Regards

Patrick


Hi NotUnctuous,

Getting warnings or errors about not all paths returning a value isn't about the return operator especially in a void function but more to do with sections of code possible unclear.

Try this and see if you get any warnings or errors:

//+------------------------------------------------------------------+
void closeAllFastEMATrades(string reason)
  {
   
   for(int cnt=0;cnt<OrdersTotal()-1;cnt++)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false)
         Print("Failed to select orders, error : "+GetLastError());//check for order select fail
      //if(OrderSymbol()!=Symbol || OrderMagicNumber()!=magicNumber)continue;   
      if(OrderMagicNumber()!=magicNumber)continue;
      if(OrderType()==OP_BUY)
        {
         if(OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue)==false)
            Print("Failed to close order , error : "+IntegerToString(GetLastError(),0,0));//check for order close
         else Print(reason+" Closing BUY @ "+DoubleToString(OrderTakeProfit(),Digits)+
            " :ask = "+DoubleToString(Ask,Digits)+" :bid = "+DoubleToString(Bid,Digits));
        }
      if(OrderType()==OP_SELL)
        {
         if(OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue)==false)
            Print("Failed to close order , error : "+IntegerToString(GetLastError(),0,0));//check for order close
         else Print(reason+" Closing BUY @ "+DoubleToString(OrderTakeProfit(),Digits)+
            " :ask = "+DoubleToString(Ask,Digits)+" :bid = "+DoubleToString(Bid,Digits));
        }
     }
  }
//+------------------------------------------------------------------+

Make sure when you print a double you convert it to string, integers not converted to string will also give a warning.

Also, Print before closing an order will delay the execution of the order so send order first then print parameters if you want to.

Hope it helps.

 
Thank you angevoyageur, WHRoeder and thrdel for your help.
Reason: