Download MetaTrader 5
To add comments, please log in or register
Share your program via Code Base. Gain access to millions of traders!
Agent86
915
Agent86 2014.11.03 03:36 
I get this warning for OrderClose()
return value of 'OrderClose' should be checked


void macdclosetrade()
{
   
   double faster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);//MODE_MAIN
   double slower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
   double prevfaster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);//MODE_MAIN previous faster
   double prevslower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2); //MODE_SIGNAL previous slower
   

   for(int i = OrdersTotal()-1; i >= 0 ; i--)
      { 
      if(OrderSelect(i, SELECT_BY_POS))  
         if(OrderMagicNumber() == MagicNumber)        
            if(OrderSymbol()== Symbol())
               if(OrderType() == OP_BUY)          
                  if(prevfaster > prevslower && faster < slower) //if direction went down then close the order
                     OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue);   
                           Print(" MACD DOWN");           
      }               
            
}
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.11.03 03:47  
Same thing for OrderOpen.
honest_knave
968
Agent86
915
Agent86 2014.11.03 14:46  
I see that and I understand OrderClose() is a bool type
However I do not understand why there needs any checking if it returns true or false by default

I changed it to this and checking produces no errors but I do not completely understand why it would not return true or false without the checking ?

void macdclosetrade()
{
   
   double faster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);//MODE_MAIN
   double slower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
   double prevfaster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);//MODE_MAIN previous faster
   double prevslower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2); //MODE_SIGNAL previous slower
   

   for(int i = OrdersTotal()-1; i >= 0 ; i--)
      { 
      if(OrderSelect(i, SELECT_BY_POS))  
         if(OrderMagicNumber() == MagicNumber)        
            if(OrderSymbol()== Symbol())
               if(OrderType() == OP_BUY)          
                  if(prevfaster > prevslower && faster < slower) //if direction down close order
                     int result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue);
                        if(result == false)
                           Print("Order",OrderTicket(),"failed to close Error",GetLastError());   
                           Print(" MACD DOWN");           
      }               
            
}
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.11.03 15:01  
It would happen if broker is busy especially when there are news event or something like that.
whroeder1
13623
whroeder1 2014.11.03 15:25  
Agent86:  I changed it to this and checking produces no errors but I do not completely understand why it would not return true or false without the checking ?
  1. It does return "true or false without the checking." It is not an error, it is a compiler warning - reminding you that you should be checking them. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 
  2. What happens if it doesn't work? Temporary network disconnection, broker busy, code broken. It's your money you're going to loose. EA's must be coded to recover.
  3. But if you really don't care, assign it do a variable like you did or just cast it to void.
    Not compiled, not tested.
    (void) OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue);
    Not compiled, not tested.
Agent86
915
Agent86 2014.11.03 17:25  
WHRoeder:
  1. It does return "true or false without the checking." It is not an error, it is a compiler warning - reminding you that you should be checking them. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 
  2. What happens if it doesn't work? Temporary network disconnection, broker busy, code broken. It's your money you're going to loose. EA's must be coded to recover.
  3. But if you really don't care, assign it do a variable like you did or just cast it to void.
    Not compiled, not tested.Not compiled, not tested.


Perfect, thanks
Agent86
915
Agent86 2014.11.03 20:58  
However, It does not close my open orders

//+------------------------------------------------------------------+
//|                                                 functiontest.mq4 |
//|                                                 function testing |
//|                      Function Testing                            |
//+------------------------------------------------------------------+
#property copyright "Open Function Testing"


//---- input parameters
extern double LotSize=0.1;
extern int    TakeProfit=20;
extern int    StopLoss=10;
extern int    AB_distance=10;
extern int    MagicNumber=123486;

//+++++ user can change retrace and/or extension:
extern double fibo_retrace=0.500;  //Got something screwed up here
extern double fibo_extension=0.618;  //Got to fix this too
  
//reference: http://www.forexfibonacci.com/calculate_fibonacci_levels/04/

double val1;
double val2;

//+------------------------------------------------------------------+
//| put custom functions here first                                   |
//+------------------------------------------------------------------+




//++++ These are adjusted for 5 digit brokers.

int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
double mlots = AB_distance*10; //A to B distance normalizer

    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (Digits == 5 || Digits == 3)
   {    // Adjust for five (5) digit brokers.
      pips2dbl    = Point*10; pips2points = 10;
   } 
   else 
    {    
      pips2dbl    = Point;    pips2points =  1;
    }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
     
   
    
//---- 

//----
   return(0);
  }
  
  
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
   
    
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {

   static datetime PreviousTime=0;
      if(PreviousTime==Time[0])
         return(0);
      else PreviousTime=Time[0]; // Should prevent another this bar if it runs through and returns back to the top of start.
      
  macdclosetrade();
  macdsignaltrade(); 
                      

//+------------------------------------------------------------------+
//| end start function                                               |
//+------------------------------------------------------------------+                
   return(0);
   }    


//+------------------------------------------------------------------+
//|     My Functions (testing)                                       |
//+------------------------------------------------------------------+

void macdsignaltrade()
{
   double faster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);//MODE_MAIN
   double slower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
   double prevfaster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);//MODE_MAIN previous faster
   double prevslower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2); //MODE_SIGNAL previous slower
   //make one for shift 3 and 4 to evaluate the fractals
   
   if(prevfaster < prevslower && faster > slower)OrderEntry(0);
      //Print(" MACD UP");
   if(prevfaster > prevslower && faster < slower)OrderEntry(1);
      //Print(" MACD DOWN");
 
}


void engulfingsignal() //Bullish Engulfing Candle
{
   if(Open[2] > Close[2]        //bear candle identified
      && Open[1] < Close[1]  //bull candle identified
      && Open[1]<= Close[2]  //bull engulfing condition
      && Open[2] < Close[1]) Print(Close[1]," Bullish Engulfing Candle "," and engulfingsize = ",DoubleToStr(Close[1]-Open[1],Digits));  
      //DoubleToStr( Close[1]-Open[1],Digits) forums help
     
   if(Open[2]<Close[2]//bull candle identified
      && Open[1]>Close[1]//bear candle identified
      && Open[1]>= Close[2]  //bear engulfing condition
      && Open[2] > Close[1]) Print(Close[1]," Bearish Engulfing Candle "," and engulfingsize = ",DoubleToStr(Close[1]-Open[1],Digits));  
      //DoubleToStr( Close[1]-Open[1],Digits) forums help);
}

void martingale()
{
//future function
// to double lot sizes each time a trade loses
}

int fractaltrade()
{
//future function
// needs to determine the ABDC's and trade entries
return(0);
}


void macdclosetrade()
{
   
   double faster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);//MODE_MAIN
   double slower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
   double prevfaster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);//MODE_MAIN previous faster
   double prevslower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2); //MODE_SIGNAL previous slower
   

   for(int i = OrdersTotal()-1; i >= 0 ; i--)
   int result;
      { 
      if(OrderSelect(i, SELECT_BY_POS))  
         if(OrderMagicNumber() == MagicNumber)        
            if(OrderSymbol()== Symbol())
               if(OrderType() == OP_BUY)          
                  if(prevfaster > prevslower && faster < slower) //if direction goes down on a buy order then close the order
                     {
                      result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue);
                        if(result == false)
                           Print("Order",OrderTicket(),"failed to close Error",GetLastError());   
                           Print(" MACD DOWN CLOSE");
                     }
                     
                     
                  if(OrderType() == OP_SELL)           
                     if(prevfaster < prevslower && faster > slower) //if direction goes up on a sell order then close the order     
                        {
                         result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue);
                           if(result == false)
                              Print("Order",OrderTicket(),"failed to close Error",GetLastError());   
                              Print(" MACD UP CLOSE");
                        }   
                          
      }


} //end macdclosetrade()



int OrderEntry(int direction)
{     
   int ticket;
   if(direction==0)
      if(OrdersTotal()==0)
         ticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3*pips2points,Ask-(StopLoss*pips2dbl),Ask+(TakeProfit*pips2dbl),NULL,MagicNumber,0,Green);
         if(ticket<0) Alert("OrderSend Failed: ", GetLastError());
         
   if(direction==1)
      if(OrdersTotal()==0)
         ticket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,3*pips2points,Bid+(StopLoss*pips2dbl),Bid-(TakeProfit*pips2dbl),NULL,MagicNumber,0,Green);
         if(ticket<0) Alert("OrderSend Failed: ", GetLastError());
         
return(0);
}      

//+------------------------------------------------------------------+
/
To add comments, please log in or register