Confused about "Trade context is busy" - Pls help

 

Dear Coders,

After fully read and tried to integrate this article Articles -> Examples -> Error 146 ("Trade context busy") and How to Deal with It. to my EA, the solution really works nicely for

OrderSend ()
but not 
OrderDelete()
The full code of this paragraph is below - Please take a look and help me point out something maybe wrong
if (newbar5!=iBarShift(NULL,0,0)) //Run once when new bar appears
    {
      int totalorders1 = OrdersTotal();
      for(int i1=totalorders1-1;i1>=0;i1--)
      {
        OrderSelect(i1, SELECT_BY_POS);
        if (OrderSymbol()==Symbol() && (OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)) 
         {
           if(TradeIsBusy() < 0) return(-1); //Code from TradeContext.mq4 extension file
           OrderDelete(OrderTicket()); //The trade should be closed but it doesn't
           TradeIsNotBusy(); //Code from TradeContext.mq4 extension file
         } 
       break;
      }
     newbar5=iBarShift(NULL,0,0);    
    }
 
What is your break; meant to do ?
 

try

if (OrderSymbol()==Symbol() && (OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)) 
   {
   if (!IsTradeContextBusy())
   OrderDelete(OrderTicket()); 
   else
   Alert ("Last Error is ", GetLastError());
   } 
 
RaptorUK:
What is your break; meant to do ?


Hi RaptorUK,

break; // Terminate the loop when first result found

 
qjol:

try


Hi qjol,

Your code didn't work. Actually, it worked on its own but when OrderDelete works then OrderSend can't due to "Trade context is busy" error.

I think the problem maybe the calls from TradeContext.mq4 can't work on a loop for or where to put these lines

if(TradeIsBusy() < 0) return(-1); //Code from TradeContext.mq4 extension file
           OrderDelete(OrderTicket()); //The trade should be closed but it doesn't
           TradeIsNotBusy(); //Code from TradeContext.mq4 extension file
 
InvestDad:


Hi RaptorUK,

break; // Terminate the loop when first result found

Some suggestions.

1) Take out the break. If you want break after 1st result then put inside the above IF statement.

2) For Pending Orders Delete, you must check for Freeze Level.

3) Instead of Return when TradeContestBusy, try using Sleep with Random Intervals instead.

4) Create Nest for OrderSelect and Check_If_True. Just for good measures.

 
InvestDad:


Hi RaptorUK,

break; // Terminate the loop when first result found

No it doesn't . . . it terminates the loop when any order is found, BUY, SELL, any Symbol (any Magic Number if you had checked for them).

Try this . . .

if (newbar5!=iBarShift(NULL,0,0)) //Run once when new bar appears
    {
      int totalorders1 = OrdersTotal();
      for(int i1=totalorders1-1;i1>=0;i1--)
      
                               // OrderSelect return true or false, it has been brought into the  if  statement

        if (OrderSelect(i1, SELECT_BY_POS) && OrderSymbol()==Symbol() && (OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)) 
         {
           if(TradeIsBusy() < 0) return(-1); //Code from TradeContext.mq4 extension file
           if (!OrderDelete(OrderTicket())) //The trade should be closed but it doesn't  // <--- did the OrderDelete work ?  test and report errors
             {
                Print("OrderDelete failed: ticket ",OrderTicket(), " Error: ", GetLastError() );
             }
           TradeIsNotBusy(); //Code from TradeContext.mq4 extension file
           break;
         } 
       
      
     newbar5=iBarShift(NULL,0,0);    
    }

 
RaptorUK:

No it doesn't . . . it terminates the loop when any order is found, BUY, SELL, any Symbol (any Magic Number if you had checked for them).

Try this . . .


Hello RaptorUK,

I put your suggested code to test in a live market.

I will report here if the problem is fixed or not :)

Great THANKS as always, great coders !!!

 
You should use the Strategy Tester, you don't have to wait so long for a trade using the ST.
 
RaptorUK:
You should use the Strategy Tester, you don't have to wait so long for a trade using the ST.


Hi RaptorUK,

I think I figured out the reason why my code didn't work because conditional statement was in wrong place. Your explaination enlightened me.

My code:

for(int i1=totalorders1-1;i1>=0;i1--)
      {
        OrderSelect(i1, SELECT_BY_POS);
        if (OrderSymbol()==Symbol() && (OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)) 
         {
           if(TradeIsBusy() < 0) return(-1); //Code from TradeContext.mq4 extension file
           OrderDelete(OrderTicket()); //The trade should be closed but it doesn't
           TradeIsNotBusy(); //Code from TradeContext.mq4 extension file
         } 
       break; // Loop FOR will be terminated right after first BUY pending order detected (reason why my code didn't work)
With Strategy Tester, I can't test all currency pairs at the same time so I don't know how best solution for trade context busy is going to work.
But with last few hours of the live market, your code seems work well and it must because I really why it should work now... lol
Great help, RaptorUK, thanks again !!
 
InvestDad:


With Strategy Tester, I can't test all currency pairs at the same time so I don't know how best solution for trade context busy is going to work.

Good point. . . :-)