Download MetaTrader 5
To add comments, please log in or register
Join MQL5 Cloud Network. Improve your rating!
fulltilt
367
fulltilt 2012.02.12 13:43 

all pending orders should be deleted if an active order is available ...

what I'm doing wrong?

for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
OrderSelect(pos, SELECT_BY_POS)
&&  OrderMagicNumber()  == Magic
&&  OrderSymbol()       == Symbol()
){
nOrders++;
}


if (OrdersTotal() > 0 && OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
{
OrderSelect(pos,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_SELLSTOP)
{
OrderDelete(OrderTicket());
Print("Deleting SELL_STOP"," Ordertype:",OrderType());
return(1);
}
if(OrderType()==OP_BUYSTOP)
{
OrderDelete(OrderTicket());
Print("Deleting BUY_STOP"," Ordertype:",OrderType());
return(1);
}
}


Tjipke de Vries
6298
Tjipke de Vries 2012.02.12 14:14  
fulltilt:

all pending orders should be deleted if an active order is available ...

what I'm doing wrong?



for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
OrderSelect(pos, SELECT_BY_POS)
&&  OrderMagicNumber()  == Magic
&&  OrderSymbol()       == Symbol()
){
nOrders++;                                     //count your buy and sell separate
}


if (OrdersTotal() > 0 && OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)                                  //OrdersTotal()  is not the same as nOrders    
{
OrderSelect(pos,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_SELLSTOP)
{
OrderDelete(OrderTicket());
Print("Deleting SELL_STOP"," Ordertype:",OrderType());
return(1);
}
if(OrderType()==OP_BUYSTOP)
{
OrderDelete(OrderTicket());
Print("Deleting BUY_STOP"," Ordertype:",OrderType());
return(1);
}
}
The deleting of pendingtrades has to be done also inside a loop
whroeder1
14236
whroeder1 2012.02.12 15:02  
int nOpenOrders = 0;
    for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == Magic.Number                 // my magic number
    &&  OrderSymbol()       == chart.symbol                 // and my pair
    &&  OrderType()         <= OP_SELL                      // and only open orders.
    ){ nOpenOrders++; }
if (nOpenOrders > 0)
    for(iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == Magic.Number                 // my magic number
    &&  OrderSymbol()       == chart.symbol                 // and my pair
    &&  OrderType()         >  OP_SELL                      // and only pending orders.
    ){ if(!OrderDelete(OrderTicket()) Alert("OrderDelete failed: ", GetLastError()); }
Tjipke de Vries
6298
Tjipke de Vries 2012.02.12 15:19  
WHRoeder:


int nOpenOrders = 0;
int EAtrades =0;
    for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == Magic.Number                 // my magic number
    &&  OrderSymbol()       == chart.symbol)                // and my pair
        {
         if(OrderType() <= OP_SELL)    { nOpenOrders++; }                      // only open orders.
         EAtrades++;                                                                   // all trades this EA
        }

if (nOpenOrders > 0 && EAtrades > nOpenOrders)
    for(iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == Magic.Number                 // my magic number
    &&  OrderSymbol()       == chart.symbol                 // and my pair
    &&  OrderType()         >  OP_SELL                      // and only pending orders.
    ){ if(!OrderDelete(OrderTicket()) Alert("OrderDelete failed: ", GetLastError()); }
not needed doing the loop again if there is no pending trade
dabbler
1103
dabbler 2012.02.12 16:59  
fulltilt:

all pending orders should be deleted if an active order is available ...

what I'm doing wrong?

pending orders could occur earlier or later in the list than the active order. This code compiles but is not guaranteed to be bug free ...

int Magic = 3;

int start(){

   bool killPending=false;
   for( int pos = OrdersTotal()-1; pos >= 0 ; pos-- ){
      if( OrderSelect(pos, SELECT_BY_POS) ){
         if( OrderMagicNumber()==Magic &&  OrderSymbol()==Symbol() ){
            if( OrderType()== OP_BUY || OrderType()==OP_SELL ){  // we have a valid open order
               killPending=true;
               break;   // we have found what we want so why bother carrying on
            }
         }
      }
   }

   if( killPending ){
      for( pos = OrdersTotal()-1; pos >= 0 ; pos-- ){
         if( OrderSelect(pos, SELECT_BY_POS) ){
            if( OrderMagicNumber()==Magic &&  OrderSymbol()==Symbol() ){
               if( OrderType()==OP_SELLSTOP ){
                  OrderDelete(OrderTicket());
                  Print("Deleting SELL_STOP"," Ordertype:",OrderType());
               }
            
               if( OrderType()==OP_BUYSTOP ){
                  OrderDelete(OrderTicket());
                  Print("Deleting BUY_STOP"," Ordertype:",OrderType());
               }
               // ... other types,  buy_limit etc
            }
         }
      }
   }
   return( 0 );
}

Of course the order delete could fail, so more testing is required.

Tjipke de Vries
6298
Tjipke de Vries 2012.02.12 18:12  
dabbler:

pending orders could occur earlier in the list than the active order. This code compiles but is not guaranteed to be bug free ...

Of course the order delete could fail, so more testing is required.


Your solution makes it with every new tick checking all open orders again to close pending trades if an active order is available ...

So it is not a faster solution..... you take also a whole loop to check every tick

Also why checking type of pending if you wanna close them all ?

With return( 1 ); I think you are ending the loop before you checked them all

with

if (nOpenOrders > 0 && EAtrades > nOpenOrders)
it's pending orders can only be deleted if one of the pending trades is triggerred to a normal sell or a buy and there are still some pending trades open
dabbler
1103
dabbler 2012.02.12 18:28  
deVries:


With return( 1 ); I think you are ending the loop before you checked them all

Damn, that was in the original code and I missed it. Well spotted. (Now fixed)
dabbler
1103
dabbler 2012.02.12 18:34  
deVries:

Also why checking type of pending if you wanna close them all ?

The OP had separate messages for each type. If that's what he wants he can have it. I agree that checking if the order is not a buy or a sell would be quicker and simpler.
dabbler
1103
dabbler 2012.02.12 18:37  
deVries:

Your solution makes it with every new tick checking all open orders again to close pending trades if an active order is available ...

I'm not sure what point you are making here. On every tick we have to check all open orders in case one is relevant. A pending order could have been converted to a market order so the OrdersTotal would not have changed. There is no need to check for OrdersTotal greater than 0 because the first test in the For loop does that already. What am I missing?
Simon Gniadkowski
Moderator
17994
Simon Gniadkowski 2012.02.12 19:01  

Why not . . .

int Magic = 3;
string type[] = {"", "", "OP_BUYLIMIT", "OP_SELLLIMIT", "OP_BUYSTOP", "OP_SELLSTOP" };

int start()
   {
   bool killPending = false;
   for( int pos = OrdersTotal()-1; pos >= 0 ; pos-- )
      {
      if( OrderSelect(pos, SELECT_BY_POS) )
         {
         if( OrderMagicNumber() == Magic &&  OrderSymbol() == Symbol() )
            {
            if( OrderType() < OP_BUYLIMIT && !killPending)  // we have a valid open order
               {
               killPending = true;
               pos = OrdersTotal()
               }
            if (killPending)
               {
               if (OrderType() > OP_SELL && OrderType() <= OP_SELLSTOP )
                  {
                  if OrderDelete(OrderTicket())
                     Print("Deleted order: ", type[OrderType()] );
                  
                  }
               }
            }
         }
      }
   }
   return( 0 );
}

Not tested or tried to compile it . . .

Tjipke de Vries
6298
Tjipke de Vries 2012.02.12 19:08  
dabbler:
I'm not sure what point you are making here. On every tick we have to check all open orders in case one is relevant. A pending order could have been converted to a market order so the OrdersTotal would not have changed. There is no need to check for OrdersTotal greater than 0 because the first test in the For loop does that already. What am I missing?


Thanks for asking. We improve our coding only that way.... Also I can...

What I mean that is that in this part of the coding given

int nOpenOrders = 0;
int EAtrades =0;
    for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == Magic.Number                 // my magic number
    &&  OrderSymbol()       == chart.symbol)                // and my pair
        {
         if(OrderType() <= OP_SELL)    { nOpenOrders++; }                      // only open orders.
         EAtrades++;                                                                   // all trades this EA
        }

You check in one time not only if there are open buys and/or sells (nOpenOrders)

but also the total with pending include (EAtrades)

Only when nOpenOrders > 0 and when the total trades > nOpenOrders you have to delete the pending

In that case the check is done atonce

If all pending are deleted the next tick this loop for deleting pending will not be done again.....

I think it is faster...

/ /12
To add comments, please log in or register