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.
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; } }
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; } } }
I think you will find that the OrderClose() function is for closed orders, and tells you what it closed at.
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
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()
OK will trie this out I was using this worked also well
pBid = MarketInfo(OrderSymbol(), MODE_BID);
pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
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).
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 :-)

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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()));
}
}
}