Help on Error Loading 2nd Coding Condition

 

Hi all, still quite new to MQL here and have already hit a road block.

I can't seem to figure out what went wrong here as I am unable to load the condition if(total>1);

My objective is to trigger a trade and once a trade is triggered, the system will detect that there is already a trade running and it will based on that and set a second condition for the next trade via the condition if(total>1); i.e. I can't seem to get the output "Hello World Part 1" to show.

Also, when I complied the file, it returns "return value of OrderSelect should be check.

Appreciate some enlightenment on this matter. Thanks.


//---- input parameters
extern double    TakeProfit = 100.0;
extern double    StopLoss = 100.0;
extern double    Recovery = 50.0; 

extern double    Lots=0.1;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
  
  
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+

int start()
{
   int ticket, total;
   
   if(Bars<100)
   {
     Print("bars less than 100");
     return(0);
   }
   if(TakeProfit < 10)
   {
      Print("TakeProfit less than 10");
      return(0); // check TakeProfit
   }
   
//+------------------------------------------------------------------+   
//-- Setting the Parameters - Start --------------------------------//
//+------------------------------------------------------------------+
   
   double MA_Fast          = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0);               //  
   double MA_Slow          = iMA(NULL,0,133,0,MODE_SMA,PRICE_CLOSE,0);              //  
         
//+------------------------------------------------------------------+
//-- Setting the Parameters - End ----------------------------------//   
//+------------------------------------------------------------------+
//-- Triggering of Trades - Start ----------------------------------//   
//+------------------------------------------------------------------+    
   
   total  = OrdersTotal(); 
   
   //------------------------------------------------------------------------------------------------------------------------------------
   //------------------------------------------------------------------------------------------------------------------------------------
   
   if(total < 1) // 1st trade - Used to determine the inital direction
   {
      // Long - MA is Crossed Up
      if(MA_Fast > MA_Slow) 
      {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLoss*Point,Ask+TakeProfit*Point,"My EA",12345,0,Green);  
      }
      
      // Short - MA is Crossed Down
      if(MA_Fast < MA_Slow) 
      {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,Bid-TakeProfit*Point,"My EA",12345,0,Red);
      } 
  
   }
   
   //------------------------------------------------------------------------------------------------------------------------------------
   //------------------------------------------------------------------------------------------------------------------------------------
   
   if ( total > 1 )
   {Print("Hello World Part 1");
      for(int ad=0; ad<=OrdersTotal(); ad++)
      {
         OrderSelect(ad, SELECT_BY_POS, MODE_TRADES);
         
         if( (ad==0) && (OrderType()==OP_BUY) )
         {
            Print("Hello World Part 2");
         }
         
         else if( (ad==0) && (OrderType()==OP_SELL) )
         {
            Print("Hello World Part 3");
         }
         return(0);
      }
      return(0);
   }
   return(0);
}

//+------------------------------------------------------------------+
 
  1. When you post code please use the SRC button! Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2.          ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,Bid-TakeProfit*Point,"My EA",12345,0,Red);
    
             OrderSelect(ad, SELECT_BY_POS, MODE_TRADES)
    Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  3. for(int ad=0; ad<=OrdersTotal(); ad++){
             OrderSelect(ad, SELECT_BY_POS, MODE_TRADES);
    If there are n orders they are positioned zero to n-1. The last iteration will fail but you don't check.

  4. total  = OrdersTotal(); 
       if(total < 1)
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
  5. if( (ad==0) && (OrderType()==OP_BUY) )
    Why do you expect your order to be only in the first position.

  6. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    2. For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    3. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.