Learning MQL4: Question about checking open orders

 

Hi,

I've been going through the online book and am now at the stage where I want to start some programming. I'm using the example in the book of 2 Moving Averages and have some questions about some of the code.

1. I would think that if I have the EA running in the USD/AUD and USD/JPY windows, then any open orders will be specific to the window the EA is running in. So if I call OrdersTotal(), and have one order open in each, the method OrdersTotal() if called from the USD/AUD window will only return the one order in the USD/AUD window and will not return the order open in the USD/JPY window?

2. Based off the answer above, is this the best way to check for open orders? I only want one order open per window for now.

 

for(int i=1; i>=OrdersTotal(); i++)          // Loop through orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If there is the next one
        {                                       // Analyzing orders:
         if (OrderSymbol()!=Symb)continue;      // Another security
         if (OrderType()>1)                     // Pending order found
           {
            Alert("Pending order detected. EA doesn't work.");
            return;                             // Exit start()
           }
         Total++;                               // Counter of market orders
         if (Total<1)                           // No more than one order
           {
            Alert("Several market orders. EA doesn't work.");
            return;                             // Exit start()
           }
         Ticket=OrderTicket();                  // Number of selected order
         Tip   =OrderType();                    // Type of selected order
         Price =OrderOpenPrice();               // Price of selected order
         SL    =OrderStopLoss();                // SL of selected order
         TP    =OrderTakeProfit();              // TP of selected order
         Lot   =OrderLots();                    // Amount of lots
        }
     }
 
GBen:

Hi,

I've been going through the online book and am now at the stage where I want to start some programming. I'm using the example in the book of 2 Moving Averages and have some questions about some of the code.

1. I would think that if I have the EA running in the USD/AUD and USD/JPY windows, then any open orders will be specific to the window the EA is running in. So if I call OrdersTotal(), and have one order open in each, the method OrdersTotal() if called from the USD/AUD window will only return the one order in the USD/AUD window and will not return the order open in the USD/JPY window?

2. Based off the answer above, is this the best way to check for open orders? I only want one order open per window for now.

 

1.  No,  OrdersTotal() does what it say in the documentation . . .  "Returns market and pending orders count."

 

You have to create a loop that loops through all the open orders,  you check the OrderSymbol() and/or OrderMagicNumber() and count the orders for your particular symbol. 

 
RaptorUK:

1.  No,  OrdersTotal() does what it say in the documentation . . .  "Returns market and pending orders count."

 

You have to create a loop that loops through all the open orders,  you check the OrderSymbol() and/or OrderMagicNumber() and count the orders for your particular symbol. 


Hi Raptor, Thanks for your reply, so basically, the idea is something like:

for(int i=0; i>=OrdersTotal(); i++) {                   // Loop through all open orders
   if (OrderSelect(i-1,SELECT_BY_POS)==true) {          // Select the open order at the position of the index
      if (OrderSymbol()== Symb){
         return true;                                   // Return true (meaning order open for the current security)
      }
   }
}

 Is it really that simple? I've had a look at what WHRoeder posted and find it hard to follow at my current beginner level even after spending a good hour goolgling!

 

Thanks. 

 
GBen:


Hi Raptor, Thanks for your reply, so basically, the idea is something like:

 Is it really that simple? I've had a look at what WHRoeder posted and find it hard to follow at my current beginner level even after spending a good hour goolgling!

Almost . . .  many things in coding start from 0 rather than 1,  the first order possition is 0 so if you have 5 orders they are at positions 0, 1, 2, 3 and 4  so the last one is OrdersTotal() - 1  so you need to adjust your loop and OrderSelect()  a little.

You don't need to say  . . . .

if ( OrderSelect(i, SELECT_BY_POS) == true )

 this will suffice . . .

if ( OrderSelect(i, SELECT_BY_POS) )

OrderSelect() returns a bool,  so will be true or false,  so you can say . . .

if ( true == true )
// or
if ( false == true )

 if you want for code clarity . . .  but there is no need.

 Your return will give you a compile error . . .

 
RaptorUK:

Almost . . .  many things in coding start from 0 rather than 1,  the first order possition is 0 so if you have 5 orders they are at positions 0, 1, 2, 3 and 4  so the last one is OrdersTotal() - 1  so you need to adjust your loop and OrderSelect()  a little.

You don't need to say  . . . .

 this will suffice . . .

OrderSelect() returns a bool,  so will be true or false,  so you can say . . .

 if you want for code clarity . . .  but there is no need.

 Your return will give you a compile error . . .

 

Ah, sorry about the wrong index, believe it or not I'm actually a developer (.net), noob mistake on my part :) 

Why would my return statement give a compile error? I was planning on making this code block to check for an open order its own function that returns a bool so I can re-use it in other EA's I create. Or do you mean the syntax is wrong? Should be:

return (true);

 Thanks for your help, there'll be more questions to come I'm sure! 

 
GBen:
RaptorUK:

Ah, sorry about the wrong index, believe it or not I'm actually a developer (.net), noob mistake on my part :) 

Why would my return statement give a compile error? I was planning on making this code block to check for an open order its own function that returns a bool so I can re-use it in other EA's I create. Or do you mean the syntax is wrong? Should be:

return (true);

Yes,  just the syntax. :-)