OrderClose did not close trades completely

 

Hi, I am new to MT4. I tried to write a script to close all trades based on the magic number. I have 5 trades running, when I execute the following script to close all 5 trades, the script only manage to close 3. I am not sure what is wrong with my coding. Can anyone help to advise or correct my codes? Many thanks.

int start()

{

int MagicNumber = 55555;

int Slippage = 10;

bool closed;

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

{

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

while(IsTradeContextBusy()) Sleep(2000);

RefreshRates();

if (OrderType() == OP_BUY && OrderMagicNumber()== MagicNumber)

{

closed = OrderClose(OrderTicket(),OrderLots(),

NormalizeDouble(MarketInfo(OrderSymbol(),

MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS)),Slippage,White);

}

if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber)

{

closed = OrderClose(OrderTicket(),OrderLots(),

NormalizeDouble(MarketInfo(OrderSymbol(),

MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS)),Slippage,White);

}

}

}

 

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

Read today's posts . . . about counting down not up . . it's explained there.
 
ok, will take note of it ....
 
RaptorUK:

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

Read today's posts . . . about counting down not up . . it's explained there.


I don't seem to find the post. Can you please be specific about the subject title/topic ? Thanks.

 
Try this
 
novalight:


I don't seem to find the post. Can you please be specific about the subject title/topic ? Thanks.

. . or here: https://forum.mql4.com/43040#513618
 

Hi WHRoeder and RaptorUK,

Thanks for the pointers. I changed my code to close order from the highest number to 0, it works ! ! ! ! Thank you very much for your help and advise.

 
novalight:

Hi WHRoeder and RaptorUK,

Thanks for the pointers. I changed my code to close order from the highest number to 0, it works ! ! ! ! Thank you very much for your help and advise.

Glad to hear you are making progress . . . :-)
Reason: