OrderClose 4051 error

 

Hi,

I am using the following code to close my trades. It manages to close about 60% of the open trades but get 4051 errors on the remaining ones. Would greatly appreciate any assistance in overcoming this.

----------------------------------------------------------- 

int TotalOrdersOpened = 0;
TotalOrdersOpened = OrdersTotal(); 

if "criteria ..."

{
    if (TotalOrdersOpened >= 1)
    {
        for (int count = 0; count < TotalOrdersOpened; count++)
        {
           RefreshRates();
           OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
           if (OrderType()==OP_BUY)
           {
               Ticket = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);
            }
        }
    } 

------------------------------------------------------------

Thx, 

cool_dude 

 
cool_dude:

Hi,

I am using the following code to close my trades. It manages to close about 60% of the open trades but get 4051 errors on the remaining ones. Would greatly appreciate any assistance in overcoming this.

----------------------------------------------------------- 

int TotalOrdersOpened = 0;
TotalOrdersOpened = OrdersTotal(); 

if "criteria ..."

{
    if (TotalOrdersOpened >= 1)
    {
        for (int count = 0; count < TotalOrdersOpened; count++)
        {
           RefreshRates();
           OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
           if (OrderType()==OP_BUY)
           {
               Ticket = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);
            }
        }
    } 

------------------------------------------------------------

Thx, 

cool_dude 

 

1. Please use SRC button when posting codes

 

2. The way you use OrderSelect and deleting order is wrong. Read this 2 topics by moderator RaptorUK https://www.mql5.com/en/forum/139592 and  https://www.mql5.com/en/forum/139654

 
phi.nuts:

1. Please use SRC button when posting codes

 

2. The way you use OrderSelect and deleting order is wrong. Read this 2 topics by moderator RaptorUK https://www.mql5.com/en/forum/139592 and  https://www.mql5.com/en/forum/139654


Oh brilliant!! It worked.

 Thx for the SRC tip. I am new to this. Also, is there any resource which explains how MT4 works? I had been through the online MQL4 book and it certainly does not explain the way trade orders work as you referred me to.

 
cool_dude:

Oh brilliant!! It worked.

 Thx for the SRC tip. I am new to this. Also, is there any resource which explains how MT4 works? I had been through the online MQL4 book and it certainly does not explain the way trade orders work as you referred me to.

Nope, AFAIK that's the only resources I know of:(.
 

May be use sleep() function after closing 1 order (Broker server may not accept too many close request on 1 tick)

The alternative way is to assign the "criteria ..." to a bool variable so it will be true for the next tick ... on each tick it will close your order until your TotalOrdersOpened= 0. When your TotalOrdersOpened= 0, make that bool variable = false;

 
bachapk:

May be use sleep() function after closing 1 order (Broker server may not accept too many close request on 1 tick)

The alternative way is to assign the "criteria ..." to a bool variable so it will be true for the next tick ... on each tick it will close your order until your TotalOrdersOpened= 0. When your TotalOrdersOpened= 0, make that bool variable = false;

There's no need for a sleep after closing/deleting 1 order. MT4 can only perform one trading task at a time, that's why there's IsTradeContextBusy() checking function (https://docs.mql4.com/check/IsTradeContextBusy).

Trade error 141 ("Too many request") happen because broker server receive more than one trade request from one account at the same time. This happen because user run several MT of the same account, and all those MT send trade request which may reach the server at the same time.

Reason: