Cycling through orders (using magic numbers)

 

Hopefully someone can help.

managing more than one order at a time has been tough for me to get my head around.


could someone tell me why both of these orders are not closing?

it only closes the first.

i understand that this is not the first time its been asked but all the posts just have random pieces of code which i can never successfully add together without living in errorville.


long and short open simultaneously. i want them both to close on minute 25. I guess something is stopping it loop through them.

Thanks in advance if anyone helps.

<Code deleted as reposted later>

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
To obtain the current market information there are several functions: SymbolInfoInteger(), SymbolInfoDouble() and SymbolInfoString(). The first parameter is the symbol name, the values of the second function parameter can be one of the identifiers of ENUM_SYMBOL_INFO_INTEGER, ENUM_SYMBOL_INFO_DOUBLE and ENUM_SYMBOL_INFO_STRING. Some symbols...
 
yaya2222:

Hopefully someone can help.

managing more than one order at a time has been tough for me to get my head around.

Post code by using Alt+S

code

 
for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      
      if(OrderType()<=OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==16384) 
        {
        if(Minute()==25)
              {
              
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;
              }
       
       
        }
       if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==16385) 
        {
        if(Minute()==25)
              {
              
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;
              }
 
yaya2222:

try this, not tested!

for(cnt=0; cnt<total; cnt++)
  {
   if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
      break;
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==16385)
     {
      if(Minute()>=25)
        {
         if(OrderType()==OP_BUY)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Violet))
               Print("OrderClose error ",GetLastError());
            return;
           }
         if(OrderType()==OP_SELL)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Violet))
               Print("OrderClose error ",GetLastError());
            return;
           }
        }
     }
  }
 

Thanks Kenneth,


Ok so how different would the code be if I wanted to close the Buy at minute 25 and the Sell at minute 26?

I used two different numbers for the orders also.

 
  1. 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 (non-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 programming forum
    2. For In First Out (FIFO rules — US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 ACCOUNT_FIFO_CLOSE

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - 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, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

  2. No need for two magic numbers — one (and maybe the order's type is sufficient).
 

If there were quite a few orders open would it also be the same situation.

I will have around 6 positions open at once.

I was trying to simplify my question by only using two orders in my example.

 

If 4 orders were opened at once for example. 2 buys 2 sells.

buy 1 closes at minute 10

sell 1 closes at minute 20

buy 2 closes at minute 30

sell 2 closes at minute 40


what im trying to understand is how to cycle through orders and check if they meet criteria.

 

Thanks to everyone that contributed.


I'll continue my hunt for the answer. cheers for pointing me in the right direction.

Reason: