Closing Multiple orders of multiple symbols....

 

hi ...the following code snippet i created to close orders if the total profit of orders reach 500.....these orders are of many different symbols...but this snippet only closes some orders, not all ...why this happens??....and i even tried the same with while loop instead of if, but that closes all orders as well as keeps closing newly created orders...wat might be the prob??...hw do i rectify it??...any help??

if(Amount>=500)
{
for (i = 0; i <OrdersTotal()-1; i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
{
bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Red);
if (ret == false)
Print("OrderClose() error - ", ErrorDescription(GetLastError()));
}
}
}

 

Please use this to post code . . . it makes it easier to read.



In your for loop you MUST count down NOT up . . . otherwise you will miss orders.

 
kmnatarajan:

hi ...the following code snippet i created to close orders if the total profit of orders reach 500.....these orders are of many different symbols...but this snippet only closes some orders, not all ...why this happens??....and i even tried the same with while loop instead of if, but that closes all orders as well as keeps closing newly created orders...wat might be the prob??...hw do i rectify it??...any help??

   if( Amount>=500 ){
      for( int try=0; try<3; try++ ){  // repeat until all orders closed as some can fail to close by requote etc
         int total = OrdersTotal();
         for( int i = total-1; i >=0; i--){
            if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true ){
               bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Red);
               if( ret == false )
                  Print("OrderClose() error - ", ErrorDescription(GetLastError()));
            }
         }
         
         if( OrdersTotal()==0 )
            break;
      }
   }
 
kmnatarajan:

hi ...the following code snippet i created to close orders if the total profit of orders reach 500.....these orders are of many different symbols...but this snippet only closes some orders, not all ...why this happens??....and i even tried the same with while loop instead of if, but that closes all orders as well as keeps closing newly created orders...wat might be the prob??...hw do i rectify it??...any help??

Or you could try it this way. Notice that "real" code needs more error trapping than you have included.

   if( Amount>=500 ){
      
      int failCount=0;  // don't get stuck in an infinite loop of failed trades
      
      while( OrdersTotal() > 0 ){
         if( OrderSelect(0, SELECT_BY_POS, MODE_TRADES) == true ){
               bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Red);
               if( ret == false ){
                  Print("OrderClose() error - ", ErrorDescription(GetLastError()));
                  failCount++;
               }
               else{
                  failCount=0;
               }
         }
         else{
            failCount++;
         }
         
         if( failCount > 10 ){
            Print("I'm a failure");
            break;
         }
      }
   }
 
dabbler:

I think you will find that the OrderClose() function is for closed orders, and tells you what it closed at.

OrderClose . . . "Closes opened order. If the function succeeds, the return value is true. If the function fails, the return value is false. "
 

thanks raptoruk and dabbler....i got it worked.....u all are great legends dude...thanks.....!!!

 

Different Symbols means also different bid or ask price for closing the trade

double price = NormalizeDouble( Bid, Digits ); // this gives the bid price of chart not always closing symbol

 
deVries:

Different Symbols means also different bid or ask price for closing the trade

double price = NormalizeDouble( Bid, Digits ); // this gives the bid price of chart not always closing symbol

Just use OrderClosePrice()
 
RaptorUK:
Just use OrderClosePrice()


OK will trie this out I was using this worked also well

pBid = MarketInfo(OrderSymbol(), MODE_BID);
pAsk = MarketInfo(OrderSymbol(), MODE_ASK);

 
RaptorUK:
Just use OrderClosePrice()

Yes! I had thought the OP had made a mistake. The MT4 help is not very helpful for that command. I tried it and it worked as you said. Simplifies several of my scripts.

Thanks :-)

(I went back and corrected my earlier posts).

 
dabbler:

Yes! I had thought the OP had made a mistake. The MT4 help is not very helpful for that command. I tried it and it worked as you said. Simplifies several of my scripts.

Thanks :-)

You are welcome . . . it's something I learned for another member on this Forum so just passing it on . . . ;-)
Reason: