How to: multiple orders

 

I recently made my first EA by copying and pasting parts of a training EA from an MQL4 COURSE By Coders’ guru. Through trial and error, I finally got the code right and was able to perform a strategy tester in which trades triggered as desired. Unfortunately, the EA only initiates a trade if there are no existing orders out. I believe this is because of the "total = OrdersTotal()" "if(total<1)" portion of the code below. Basically, if no orders are out, the EA executes trades; however, it any other trade is already open, the EA reads total value as being >= 1 and it does not execute a trade.

Instead of doing this, I would like the EA to see if any orders with its assigned MagicNumber exist. If false, then execute trades. If true, then don't send any more trades but continue to manage the existing trade that is open. (The magic number for this EA is 11.)

I want the EA to execute trades as it does, but I also want to be able to make other trades on my own at the same time. As of now, this is not possible. Below is the portion of the code I think is causing this problem, and in bold are the parts I either want changed or don't fully understand.

If anyone knows what code could solve this problem, please let me know. Thanks for taking the time to read this. Your help is really appreciated.


total = OrdersTotal();

if(total < 1)
{
if( Var1 < BbUpper && Var2 > BbUpper && Bid > BbLower18)
{
ticket=OrderSend(Symbol(),OP_SELL,Lot,Bid,300,0,0,"Sell Triggered",11,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
if( Var1 > BbLower && Var2 < BbLower && Bid < BbUpper18)
{
ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,300,0,0,"Buy Triggered",11,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
{
if(OrderType()==OP_SELL) // if short position is open, should it be closed?
{
if (Bid <= BbLower18)
{
OrderClose(OrderTicket(),OrderLots(),Ask,300,Violet); // close position
return(0); // exit
}
}
if(OrderType()==OP_BUY) // if long position is open, should it be closed?
{
if (Bid >= BbUpper18)
{
OrderClose(OrderTicket(),OrderLots(),Bid,300,Violet); // close position
return(0); // exit
}
}
}
 

  1.     for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)            // Only my orders w/
        &&  OrderMagicNumber() == Magic.Number         // my magic number
        &&  OrderSymbol()      == Symbol() ){          // and period and symbol
            // trail stops, etc
            return;
        }
        // No open orders...

 
WHRoeder:



Thanks WHRoeder. I'll give that a try. Sorry for not using the SRC option, first time poster.
 

I modified the code a little and placed in most of your suggested code WHRoeder. It produces the same back test results as before, so that is good. However, the "total = OrdersTotal(); if(total < 1)" portion of the code is still a problem. This still prevents the EA from placing a trade if any other trade already exists. I only want the EA to stop itself from initiating a new trade if a trade with its magic number already exists. Can this be solved?

Here is the code as I have it now:

   total  = OrdersTotal(); 
   if(total < 1)
      {
         if( Var1 < BbUpper && Var2 > BbUpper && Bid > BbLower18)
            {
            OrderSend(Symbol(),OP_SELL,Lot,Bid,300,0,0,"Sell Triggered",11,0,Red);
            return(0);
            }
         if( Var1 > BbLower && Var2 < BbLower && Bid < BbUpper18)
            {
            OrderSend(Symbol(),OP_BUY,Lot,Bid,300,0,0,"Buy Triggered",11,0,Green);
            return(0);
            }
      }        
    for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) 
      {if (OrderSelect(pos, SELECT_BY_POS) && OrderMagicNumber() == MagicNumber)
         {if(OrderType()==OP_SELL)   // if short position is open, should it be closed?
            { 
               if (Bid <= BbLower18) 
                  {
                  OrderClose(OrderTicket(),OrderLots(),Ask,300,Violet); // close position
                  return(0); // exit
                  }         
            }
          if(OrderType()==OP_BUY)   // if long position is open, should it be closed?
            {
               if (Bid >= BbUpper18) 
                  {
                  OrderClose(OrderTicket(),OrderLots(),Bid,300,Violet); // close position
                  return(0); // exit
                  }         
            }
         }
      }   
  return;
   
 }


 
#include <common_functions.mqh>


int magic;


int init(){
   magic = makeMagicNumber(WindowExpertName() + Symbol() + Period());
}

int start(){
   if (getNumOpenOrders(-1, magic) == 0) {
   
   } else{
   
   }
}

 

Error message returned:

'common_functions.mqh' - cannot open the program file C:\Program Files\FXCM MT4 powered by BT\experts\Test Multiple2.mq4 (2, 1)

 
did you read the instructions? It belongs into the includes folder like any other include file too.
 

7bit - I must apologize for my mistakes. At first I was not aware that there were any instructions. I am very new to all of this. Thank you very much for your patience and your help.

The common_functions problem seems to be fixed now, thanks. I haven't been able to put the name of my EA in place of "WindowExpertName" since variable not defined errors come up when I try to do this. The name of the EA is "TestMultiple2", but when I put that in place of "WindowExpertName" I get:

'TestMultiple2' - function is not defined C:\Program Files\FXCM MT4 powered by BT\experts\TestMultiple2.mq4 (16, 28)

As of now, the EA makes an unlimited number of trades, not recognizing that it has already opened a trade and therefore should stop placing new orders. Here is the complete code as it stands now:

#include <common_functions.mqh>

extern double  Lot=0.1;
extern int     MagicNumber=11;
 
int magic;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
  
   magic = makeMagicNumber(WindowExpertName()+ Symbol() + Period());

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

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start(){
   if (getNumOpenOrders(-1, magic) == 0) {
   
   } else{
   
   }  
   double Var1, Var2, BbUpper, BbLower, BbUpper18, BbLower18;
     
   Var1 = iMA(NULL, PERIOD_H1,  1, 0, MODE_LWMA, PRICE_MEDIAN,  0);
   
   Var2 = iMA(NULL, PERIOD_H1,  1, 0, MODE_LWMA, PRICE_MEDIAN,  1);  
      
   BbUpper = iBands(NULL, PERIOD_H1,  25, 2, 5, PRICE_CLOSE, MODE_UPPER, 0);
     
   BbLower = iBands(NULL, PERIOD_H1,  25, 2, 5, PRICE_CLOSE, MODE_LOWER, 0); 
     
   BbUpper18 = iBands(NULL, PERIOD_H1,  22, 2, 2, PRICE_CLOSE, MODE_UPPER, 0);
      
   BbLower18 = iBands(NULL, PERIOD_H1,  22, 2, 2, PRICE_CLOSE, MODE_LOWER, 0); 
   
   
   if( Bid < Ask) //this is just for test purposes to initiate a trade and see if the EA can manage it from there 
                  //typically this would be (Var1 < BbUpper && Var2 > BbUpper && Bid > BbLower18)
            {
            OrderSend(Symbol(),OP_SELL,Lot,Bid,300,0,0,"Sell Triggered",11,0,Red);
            return(0);
            }
   if( Var1 > BbLower && Var2 < BbLower && Bid < BbUpper18)
            {
            OrderSend(Symbol(),OP_BUY,Lot,Bid,300,0,0,"Buy Triggered",11,0,Green);
            return(0);
            }
         
   for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) 
      {if (OrderSelect(pos, SELECT_BY_POS) && OrderMagicNumber() == MagicNumber)
         {if(OrderType()==OP_SELL)   // if short position is open, should it be closed?
            { 
               if (Bid <= BbLower18) 
                  {
                  OrderClose(OrderTicket(),OrderLots(),Ask,300,Violet); // close position
                  return(0); // exit
                  }         
            }
          if(OrderType()==OP_BUY)   // if long position is open, should it be closed?
            {
               if (Bid >= BbUpper18) 
                  {
                  OrderClose(OrderTicket(),OrderLots(),Bid,300,Violet); // close position
                  return(0); // exit
                  }         
            }
         } 
      } 
  }
       
     
 
//+------------------------------------------------------------------+

Any help would be appreciated.


 

1. WindowExpertName() will always return the name of the EA, no need to change anything here.

2. You now have a magic number but you should also use it now! Use it in OrderSend(), otherwise it won't make any sense. How else should the trades get their magic number if not in the OrderSend()?

3. Replace all OrderSend() with orderSendReliable() and all OrderClose() with orderCloseReliable()

 

STOP!

I just had another look at your code. It is time to stop this here. You are wasting my time. You should first learn some mql4 and at least how to read a program (this is easier than writing) or even understand what a program is. What do you think the simple if/else snippet that I posted does? Nothing? Something? Something else? Do you think it is ok to put it into the code just for decoration purposes? Or do you think it would do something the way you put it in there?

WHY didn't you simply ask what an if () {} else {} is and what it does instead?

This is too much for me. Seriously. I cannot really understand what you are trying to achieve with this thread here. You are just putting together code snippets you get from somewhere completely without a plan without even TRYING to think about it what they might do. You are wasting time. This will lead to nothing.

 
Well said, thanks for your advice. I will try learn this on my own. Sorry for wasting your time, and good luck trading.
Reason: