Closing all Pending Orders on new bar

Back to topics list To post a new topic, please log in or register
avatar
45
tparimore 2011.04.03 06:21
 

Could someone tell me why the code below only closes one open trade per bar? e.g. I have two open orders. On each new bar ClosePending() is called. However, only one will close this bar and the second closes the next bar? Thanks in advance.

//---------------------------Close Pending Orders-------------------------------

void ClosePending()
  {
  
   bool   result;
   
   for(i=0;i<OrdersTotal();i++)
     {
       OrderSelect(i,SELECT_BY_POS);
       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUYSTOP)
        {
          result=OrderDelete(OrderTicket());
          if(result==TRUE) Print("OP_BUYSTOP Deleted");
          else Print( "Error Deleting OP_BUYSTOP ");
        }
          
       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELLSTOP)
        {
          result=OrderDelete(OrderTicket());
          if(result==TRUE) Print("OP_SELLSTOP Deleted");
          else Print( "Error Deleting OP_SELLSTOP");
        }
       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUYLIMIT)
        {
          result=OrderDelete(OrderTicket());
          if(result==TRUE) Print("OP_BUYLIMIT Deleted");
          else Print( "Error Deleting OP_BUYLIMIT ");
        }
          
       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELLLIMIT)
        {
          result=OrderDelete(OrderTicket());
          if(result==TRUE) Print("OP_SELLLIMIT Deleted");
          else Print( "Error Deleting OP_SELLLIMIT ");
        }     
     }
   
   return(0);
  
  }

 
avatar
571
brewmanz 2011.04.03 10:50 #
 

because with i=0, you close first order with index [0].

second order changes from index[1] to index[0]

Then with next loop .. oops, no more orders.

Change

for(i=0;i<OrdersTotal();i++)

to

for(i=OrdersTotal()-1;i>=0;i--)

 
avatar
12998
WHRoeder 2011.04.03 22:19 #
 
Count down and always test return codes.
 for(int pos=OrdersTotal()-1; pos >= 0; i--) if(
    OrderSelect(i,SELECT_BY_POS)
&&  OrderMagicNumber() == MagicNumber   // Only my orders,
&&  OrderSymbol()      == Symbol()      // on my chart,
&&  OrderType()        > OP_SELL){      // pending only.
    static OP.text[]={  "OP_BUY",       "OP_SELL",
                        "OP_BUYLIMIT",  "OP_SELLLIMIT",
                        "OP_BUYSTOP",   "OP_SELLSTOP"   };
    if (OrderDelete(OrderTicket()))
        Print(OP.text[OrderType()]," Deleted");
    else Print( "Error Deleting ",OP.text[OrderType()]," ",GetLastError());
}

avatar
45
tparimore 2011.04.05 03:43 #
 
brewmanz:

because with i=0, you close first order with index [0].

second order changes from index[1] to index[0]

Then with next loop .. oops, no more orders.

Change

to


Duh... Thanks so much. This is why I ask the experts. Thanks again.
avatar
45
tparimore 2011.04.05 03:57 #
 
WHRoeder:
Count down and always test return codes.

Thanks for your help. Can you please explain OrderType() > OP_SELL ? Also the use of static OP.text[] . Your code is very efficient.
 
avatar
872
eevviill 2016.03.09 23:06 #
 
tparimore:

Thanks for your help. Can you please explain OrderType() > OP_SELL ? Also the use of static OP.text[] . Your code is very efficient.

Many programmers writes like >OP_SELL to show they high "skill" of codding. :))))

It is for use less PC resources. But code becomes bad readed.

 

 

OP_BUY

0

Buy operation

OP_SELL

1

Sell operation

OP_BUYLIMIT

2

Buy limit pending order

OP_SELLLIMIT

3

Sell limit pending order

OP_BUYSTOP

4

Buy stop pending order

OP_SELLSTOP

5

Sell stop pending order

Back to topics list  

To add comments, please log in or register