Trade context is busy !?

 

It would be grateful if somebody tell me what problem caused a EA occasionally (about 50%) fails to open an order with the error #146.

According to stderror.mqh, it is Trade context is busy. Is it a problem of broker , my internet connection or EA ?

Thanks in advance.

Nomura

 

This error means that 2 or more eas are trying to open, close or modify their orders at the same time. There is one "stream" in metatrader for ea trading, so if one ea is busy talking to server, all the rest will have to wait until it finishes.

How to solve:

1. if you can code, just go to Error 146 ("Trade context busy") and How to Deal with It - MQL4 Articles, there is a description of this problem and functions to add to your code to avoid this happening in future.

2. If you cannot code, just use 1 ea per metatrader, or (if you want to run more than 1) make sure they will not trade too often. If you run 2 scalping eas at the same time, you will get quite a lot of errors 146.

 

Trade context busy

mrv:
This error means that 2 or more eas are trying to open, close or modify their orders at the same time. There is one "stream" in metatrader for ea trading, so if one ea is busy talking to server, all the rest will have to wait until it finishes.

How to solve:

1. if you can code, just go to Error 146 ("Trade context busy") and How to Deal with It - MQL4 Articles, there is a description of this problem and functions to add to your code to avoid this happening in future.

2. If you cannot code, just use 1 ea per metatrader, or (if you want to run more than 1) make sure they will not trade too often. If you run 2 scalping eas at the same time, you will get quite a lot of errors 146.

mrv

Thanks for your advice.

I just applied a simple EA to several currency pairs with 30m TF. As your advice, orders for different pairs were triggered in the same time. I think this caused the "Trade context busy".

I cannot code by myself and I can read simple coding only. I think I can learn much in this Forum especially from you.

Thanks with many thanks.

Nomura

 

I have been trying to add the template to my ea but end up with

'.' - unexpected token

I have never seen this what should I do?

Error 146 ("Trade context busy") and How to Deal with It - MQL4 Articles

I want to add this code to the PEA2 ea as since it opens on the new daily bar if I have multiple pairs there seems to be a problem with opening all positions at once. and I receive TradeDispatcher: trade context is busy error.

I don't know if I am going down the right track if not is there any thing else I could do to space out trades so all pairs open a position at the right time.

Cheers

Beno

Files:
pea2.mq4  10 kb
 

One Solution

One solution, although it should be used judiciously is to add the following code just before you enter your code to place an order (whether buy or sell). It will loop and check every 100ms until the trade context is no longer busy. A little more careful use would to just give yourself a maximum number of seconds to wait before you give up.

while(IsTradeContextBusy()) Sleep(100);

RefreshRates(); //In case you waited very long

//Set SL & TP then OrderSend

 

can i work 40 of them ?

how many expert advisors can be used at once on one platform?(for live trading)

 

Template fix required please Trade Context Busy

I have use this template Expert Advisor Builder for MetaTrader 4 but it produces trade context busy under some conditions.

could some one please add some code to this template to fix the trade context problem

#property copyright "Expert Advisor Builder"

#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = {EachTickMode};

extern double Lots = {Lots};

extern int Slippage = {Slippage};

extern bool UseStopLoss = {UseStopLoss};

extern int StopLoss = {StopLoss};

extern bool UseTakeProfit = {UseTakeProfit};

extern int TakeProfit = {TakeProfit};

extern bool UseTrailingStop = {UseTrailingStop};

extern int TrailingStop = {TrailingStop};

int BarCount;

int Current;

bool TickCheck = False;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit() {

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

//+------------------------------------------------------------------+

//| Variable Begin |

//+------------------------------------------------------------------+

{Var}

{VarBuy}

{VarSell}

{VarCloseBuy}

{VarCloseSell}

//+------------------------------------------------------------------+

//| Variable End |

//+------------------------------------------------------------------+

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy) |

//+------------------------------------------------------------------+

{SignalCloseBuy}

//+------------------------------------------------------------------+

//| Signal End(Exit Buy) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if(Bid - OrderOpenPrice() > Point * TrailingStop) {

if(OrderStopLoss() < Bid - Point * TrailingStop) {

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

} else {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Sell) |

//+------------------------------------------------------------------+

{SignalCloseSell}

//+------------------------------------------------------------------+

//| Signal End(Exit Sell) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

//+------------------------------------------------------------------+

//| Signal Begin(Entry) |

//+------------------------------------------------------------------+

{SignalBuy}

{SignalSell}

//+------------------------------------------------------------------+

//| Signal End |

//+------------------------------------------------------------------+

//Buy

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} else {

Print("Error opening BUY order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

//+------------------------------------------------------------------+

 
Beno:
I have use this template Expert Advisor Builder for MetaTrader 4 but it produces trade context busy under some conditions.

could some one please add some code to this template to fix the trade context problem

Just add this condition before enter/close a position:

if(!IsTradeContextBusy() && IsTradeAllowed())

FerruFx

 

67-17454

Try this code your'll have to rewrite it to operate within the standard MT framwork. The system tag is the Magic Number, each EA and each orcurrance of the same EA must have a different MagicNumber. Once the Trade Desk opens the order book for one EA it will block all others till the EA with control of the order book releases it. All other EAs will be kept at bay for a determine amount of time wating for their turn to access the order book, if that time has expired the EA will exit with out placing the order. With the next tick the attampt to place the order will again be trid. If this function returns true the book is locked to that EA and you can then refreash the price and do what ever you want to place your order when finished that EA must release the order book. No other ea can release it except the one that has control of it. Again you must rewrite it to standard MQ4 which should not be to difficult. If I had the time I would do it for you.

Keit

edit[ the second screen shot at the bottom you will see the control process that takes place with the oderbook (entries are read from bottom to top)]

 

context busy

I suffered a lot of "context busy", does anybody know how to avoid it?

Thanks.

 
pureflame:
I suffered a lot of "context busy", does anybody know how to avoid it? Thanks.

yeah, just restart mt4.

Reason: