Forex



Go Back   Forex Trading > Discussion Areas > Setup Questions
Forex Forum Register More recent Blogs Calendar Advertising Others Help






Register
Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.

From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.

Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
  • Elite Section
    Get access to private discussions, specialized support, indicators and trading systems reported every week.
  • Advanced Elite Section
    For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
See more

 
 
Thread Tools
 
Old 09-18-2007, 10:11 AM
Member
 
Join Date: Dec 2006
Posts: 40
Nomura is on a distinguished road
Question 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
 
Old 09-18-2007, 10:35 AM
mrv mrv is offline
Member
 
Join Date: Oct 2006
Location: Currently in UK
Posts: 90
mrv is on a distinguished road
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
 
Old 09-18-2007, 12:28 PM
Member
 
Join Date: Dec 2006
Posts: 40
Nomura is on a distinguished road
Thumbs up Trade context busy

Quote:
Originally Posted by mrv View Post
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
 
Old 10-03-2008, 11:48 AM
Beno's Avatar
Senior Member
 
Join Date: Aug 2006
Location: London
Posts: 516
Beno is on a distinguished road
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
Attached Files
File Type: mq4 PEA2.mq4 (9.5 KB, 30 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
 
Old 10-17-2008, 05:53 PM
Senior Member
 
Join Date: Apr 2006
Location: Oregon
Posts: 116
67-17454 is on a distinguished road
Thumbs up 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
 
Old 10-18-2008, 01:09 AM
cockeyedcowboy's Avatar
Senior Member
 
Join Date: Nov 2005
Posts: 480
cockeyedcowboy is on a distinguished road
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)]
Attached Images
File Type: jpg OrderDesk.jpg (167.7 KB, 268 views)
File Type: jpg GBO 1.30.jpg (244.2 KB, 264 views)

Last edited by cockeyedcowboy; 10-18-2008 at 01:50 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
 
Old 03-27-2009, 09:26 AM
Member
 
Join Date: Mar 2008
Location: San Francisco,CA
Posts: 92
sfmain is on a distinguished road
can i work 40 of them ?

how many expert advisors can be used at once on one platform?(for live trading)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
 
Old 09-28-2009, 04:42 PM
Beno's Avatar
Senior Member
 
Join Date: Aug 2006
Location: London
Posts: 516
Beno is on a distinguished road
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
Code:
#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);
}
//+------------------------------------------------------------------+
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
 
Old 10-01-2009, 11:53 AM
Senior Member
 
Join Date: Feb 2007
Posts: 986
FerruFx is on a distinguished road
Quote:
Originally Posted by Beno View Post
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
__________________
FerruFx / www.ervent.net - Professional Coding Services (EAs/Indicators/Alerts)

BBVPS.com - Reliable Windows VPS For MT4 Hosting
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
 

Bookmarks

Tags
"trade context is busy", trade context is busy, TradeDispatcher: trade context is busy
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
To trade or not to trade Indicators? yaniv_av Indicators - Metatrader 4 1 03-27-2007 02:01 PM


All times are GMT. The time now is 11:10 AM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.