Help ..Proper use of IsTradeContextBusy()

 

I'd like advice.... I'm experiencing Trade Context is Busy errors, very occasionally ...(but always unwelcome) when Closing Multiple Orders. I've tried Sleep(XXXX) but this has two problems..

First) It wastes time when there is nothing wrong..

Two) It doesn't always cure Trade Context Busy errors.

I'd like to check for NO TradeContextBusy BEFORE trying to close a position.

In the first "Loop code"... will it check before EACH try to OrderClose...or only check once.... AND..... Should there also be a second if(!IsTradeContectBusy()) AFTER the Buy OrderClose() but before the Sell OrderClose()??

In the second code...."Straight through" one time close code...is the Syntax correct??

ADDITIONALLY......

Are ALL the RefreshRates() in these two codes a waste of time because I'm specifying MarketInfo(OrderSymbol()?? I think they may be....

Can I remove the Sleep() if I use the check for TradeContextBusy?? I see NO need for them.....

Thanks for your help...Bill


//---------LOOP CODE---------------

if (Closenow==True)
{
for(i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
if(!IsTradeContextBusy())
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Pink);
break;
Sleep(69);
RefreshRates();
//Close opened short positions
case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Pink);
Sleep(69);
RefreshRates();
}
if(result == false)
{
Sleep(3000);
}
}
}

//---------------------------------------------------------------------------------


//----------ONCE THROUGH CODE-------------------------

if (Closenow == True)
if (OT>0)
{
RefreshRates();
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY&&!IsTradeContextBusy())
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
RefreshRates();
if(OrderType()==OP_SELL&&!IsTradeContextBusy())
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
RefreshRates();
if(OrderType()>OP_SELL&&!IsTradeContextBusy()) //pending orders
OrderDelete(OrderTicket());
Sleep(60);
}
 
n8937g:

I'd like to check for NO TradeContextBusy BEFORE trying to close a position.

The standard recommendation on this forum is to use the code in https://www.mql5.com/en/articles/1412 - sections 5 and 6 of that article. (Without it, I've had very strange things happen in MT4.)


The disadvantage of this route is that it's effectively implementing co-operative multi-tasking between EAs, and your EA can still be taken down by another EA which doesn't implement something like this.

 

Hi jjc,

Yes, I read that before posting... I'm trying to simplify that idea. I run two EA's ...Same EA, different magic number, different pair...., but I had the occasional Trade Context Busy error

before trading on the second pair was added. I think I get the error because the EA when closing say 10 trades doesn't finish closing the 3rd position before trying to close the fourth. That's why

I'd like to check right before the OrderClose command for........... !IsTradeContextBusy() I'm not sure where the command to check for IsTradeContextBusy should be inserted in the loop for it

to get checked before each OrderClose. I will rewrite the EA's so only the EA on the 1st chart actually closes any open orders on the account. That way the two EA's aren't both sending OrderClose

at the same time. But I'd still expect to see the problem with the error unless I check right before trying to send the order that the Broker is ready to take it. Can you help? Would the additions

to the code I propose accomplish this??

Thanks

 

How would this work??? Will it check for" Is Trade Allowed" EVERY time before it tries to close an order?? Or just ONE time?? If it does check before trying to close multiple orders....will it check for "Is Trade Allowed" before every OP_SELL OrderCLose too?? Or just the OP_BUY OrderClose.....??? Thanks



if (Closenow==True)
{
for(i=OrdersTotal()-1;i>=0&&IsTradeAllowed();i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Pink);
break;

//Close opened short positions
case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Pink);
}

if(result == false)
{
Sleep(3000);
}
}
}
 

Old post I know, but why not use:

while(IsTradeContextBusy()) Sleep(10);
 
jasonxfield:

Old post I know, but why not use:

 

For your information, you can now send up to 8 (or 16 ? not sure) trading requests simultaneously (different EAs of course), so the chance of "Trade Context Busy" error is very low.
 
jasonxfield: Old post I know, but why not use:
while(IsTradeContextBusy()) Sleep(10);
  1. You must RefreshRates after Sleep before using any predefined variables.
  2. If the terminal looses connection, it can be several minutes before the loop terminates. You may want to recheck the signal before proceeding.
Reason: