Hi, I want to check if order with magic number == 1 is open. Can somebody help me??

 

B

You could use this function (or some variation of it) to count orders by type

int OpenTradesForMNandPairType(int iMN, string sSymbol, int iType)
{
int icnt, itotal, retval;

retval=0;
itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++)
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
       if (OrderMagicNumber() == iMN)
        if (OrderSymbol() == sSymbol)
         if (OrderType() == iType)
           {
             retval++;
           }

     }

return(retval);
}

Good Luck

-BB-

 
BarrowBoy:

B

You could use this function (or some variation of it) to count orders by type

Good Luck

-BB-


thanks, but still have problem if order is open or pending. I need to know if is open....

OrderType is usefull to check if order is buy or sell.

mabe you have the answer - how check it?

 
BorysekPL:


OrderType is usefull to check if order is buy or sell.

Try this example;

if (OrdersTotal() > 0) {
if (OrderType() <= 1) { //https://docs.mql4.com/trading/OrderType
Comment("It's An Open Order");
}
else
Comment("It's A Pending Order");
}
OPForex
 
BorysekPL:


thanks, but still have problem if order is open or pending. I need to know if is open....

OrderType is usefull to check if order is buy or sell.

mabe you have the answer - how check it?

int OrderType( ) 
Returns order operation type for the currently selected order. It can be any of the following values:
OP_BUY - buying position,
OP_SELL - selling position,
OP_BUYLIMIT - buy limit pending position,
OP_BUYSTOP - buy stop pending position,
OP_SELLLIMIT - sell limit pending position,
OP_SELLSTOP - sell stop pending position.
Note: order must be selected by OrderSelect() function.  

OrderType() does show if an order is open or pending!

if you have an order from the MODE_TRADES pool then it will be one of these 6 types. If it is not either of OP_BUY or OP_SELL then it is a pending order.

OPForex's test of OrderType() <= 1 relies on the #define'd symbols having particular values and is not a "robust" coding style.

You can do it this way ...

   int total= OrdersTotal();
   for( n=0; n<total; n++ ){ // count how many open orders we have for ALGO trades only
      if( !OrderSelect(n,SELECT_BY_POS,MODE_TRADES) )
         continue;

      if( OrderSymbol()!=Symbol() )
         continue;
      
      if( OrderMagicNumber()!=MAGICNUMBER )
         continue;
         
      if( OrderType()== OP_BUY ){
         // it's a BUY order
      }   
      else if( OrderType()== OP_SELL ){
         // it's a sell order
      }
      else{
         // it is a pending order
      }
   }

or if you prefer lots of indenting

   int total= OrdersTotal();
   for( n=0; n<total; n++ ){ // count how many open orders we have for ALGO trades only
      if( OrderSelect(n,SELECT_BY_POS,MODE_TRADES) ){
         if( OrderSymbol()==Symbol() ){
            if( OrderMagicNumber()==MAGICNUMBER ){
               if( OrderType()== OP_BUY || OrderType()== OP_SELL ){
                  // it's a BUY or SELL order
               }   
               else{
                  // it is a pending order
               }
            }
         }
      }
   }

or if you love over-complicated logical expressions

   int total= OrdersTotal();
   for( n=0; n<total; n++ ){ // count how many open orders we have for ALGO trades only
      if( OrderSelect(n,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICNUMBER ){
         if( OrderType()== OP_BUY || OrderType()== OP_SELL ){
            // it's a BUY or SELL order
         }   
         else{
            // it is a pending order
         }
      }
   }

There is no speed penalty with any of the approaches. It is a question of what you are more comfortable with.


I prefer the first method. Putting too many tests on each line make debugging harder.

 
dabbler:
I prefer the first method. Putting too many tests on each line make debugging harder.
Or even more concise (and without the negative/inverted logic):
   for(int pos=OrdersTotal()-1; pos>=0; pos-- ) if( // Select my orders
      OrderSelect(pos,SELECT_BY_POS)
   && OrderSymbol()     ==Symbol()
   && OrderMagicNumber()==MAGICNUMBER ){
      if( OrderType() <= OP_SELL){} // open order
      else{                       } // pending         
   }
 

Thank you..

A theme is closed

I've got everythink what I've been looking for:)

Reason: