Multiple EAs on multiple symbols and concurrent orders

 

Hi,

I have developed an EA that looks for a few price action setups. If a setup is found then the EA prepares an order and shows me the pre-filled order window for confirmation.

It works quite well on one symbol/chart at the time.

The fact is that I am using it on multiple symbols and, although it can find all the setups when the new bar is created, only one order at the time can be processed. This seems to be related to the fact that only one order window can be open in a terminal even if there are multiple charts open.

Do you have any advice on how to deal with it? I imagine I could automate the order, but I really want to have a look at the chart before sending the order.

Thanks for your help

Cheers

Rojj

 
Set a mutex around the orderSend. The newest terminal can send multiple orders but I still use the mutex to avoid the busy context.
 

Out of curiosity what is the advantage of adding a software mutex in the EA, when presumably the terminal has its own mutex? isnt retrying/waiting after error 146 sufficient?

I've read the article and understand that the article is not correct because it is not atomic. (That is another EA can slip in between IsTradeAllowed() and OrderSend(), so I get that.

However if OrderSend returns an error code isn't that sufficient? ie Just checking for Error 146 after the fact. and assuming terminal.exe is is managing it's own mutex,

Also if future builds are upgraded to allocate more threads to Order functions then this will use full capacity?

Are there other issues with OrderSend errors that make the additional software mutex more robust?


EDIT: I think I found on another forum that the MT4 client used to crash/lock up if two EA instances tried to send an order at the same time, so IsTradeContextAllowed() was born?

This post was circa 2009, so does anyone know if that bug was fixed (I'm *guessing* yes based on lack of recent horror stories) in which case IsTradeContextAllowed() and using software mutex, this may be a vestige of old MT4 client instability ?


EDIT: Finding more on this (2012)

IBFX has published the following regarding the error (Also seems to be standard text repeated on other brokers websites)

"You receive “Trade context is busy” when you try to process two or more orders at the same time. If you have placed a trade and do not wait until it processes completely and then try to place the same trade or a different trade the commands go into a loop and you get “Tread context is busy”. The only solution is to close down the platform and restart it."

Now just need to confirm if this issues persists in build 509? Looks like a software mutex is best practice until we know otherwise :)

 

Thanks both for the advice. I need to see which error i get. The retrying/waiting trick seems the most immediate and simple solution to implement.

 
ydrol:


EDIT: I think I found on another forum that the MT4 client used to crash/lock up if two EA instances tried to send an order at the same time, so IsTradeContextAllowed() was born?

This post was circa 2009, so does anyone know if that bug was fixed (I'm *guessing* yes based on lack of recent horror stories) in which case IsTradeContextAllowed() and using software mutex, this may be a vestige of old MT4 client instability ?


EDIT: Finding more on this (2012)

Now just need to confirm if this issues persists in build 509? Looks like a software mutex is best practice until we know otherwise :)



I understood that the terminal could now send up to 8 orders at the same time. Can't remember where I read that.

I can't find any mention of it by doing a search, maybe I misunderstood.

 
GumRai:

I understood that the terminal could now send up to 8 orders at the same time. Can't remember where I read that.

It was in the announcement post about one of the Builds, maybe 482 . . . ah yes, it was 482 . . .

https://www.mql5.com/en/forum/143892

"2. Terminal: Increased the number of allowed parallel trading operations for MQL4 programs - up to 8 parallel trade requests are allowed now. This ensures uninterrupted simultaneous trading of several scripts or Expert Advisors - it means that it is almost impossible to receive "Trade context is busy" error code in normal conditions."

 
RaptorUK:

It was in the announcement post about one of the Builds, maybe 482 . . . ah yes, it was 482 . . .

https://www.mql5.com/en/forum/143892

"2. Terminal: Increased the number of allowed parallel trading operations for MQL4 programs - up to 8 parallel trade requests are allowed now. This ensures uninterrupted simultaneous trading of several scripts or Expert Advisors - it means that it is almost impossible to receive "Trade context is busy" error code in normal conditions."


Thanks Raptor,

I thought that maybe I had imagined it :)

 

This is how I solved the problem

I set PlaceOrder to true for every new bar. Still working on some error control, but you get the point. It works fine so far.

Thanks for your advice

         while(PlaceOrder)
            {
            Alert("Trying to open order on: ",Symbol());
            Ticket = OrderSend(Symbol(),TypeOrder,NumberOfLots,NormalizeDouble(Entry,Digits),3,NormalizeDouble(StopLoss,Digits),NormalizeDouble(TakeProfit,Digits),comment);
            if(Ticket>0)
               {
               Alert("Order: ",Ticket);
               Ans=OrderModify(Ticket,OrderOpenPrice(),StopLoss,TakeProfit,0);//Modify it!
               if (Ans==true)                      // Got it! :)
                  {
                  Alert ("Order ",Ticket," is modified:)");
                  }
               break;
               }
            int Error=GetLastError();                 // Failed :(
            Alert("Error: ",Error);
            switch(Error)                             // Overcomable errors
               {
               case 135:Alert("The price has changed. Retrying..");
                  RefreshRates();                     // Update data
                  continue;                           // At the next iteration
               case 136:Alert("No prices. Waiting for a new tick..");
                  while(RefreshRates()==false)        // Up to a new tick
                  Sleep(1);                        // Cycle delay
                  continue;                           // At the next iteration
               case 146:Alert("Trading subsystem is busy. Retrying..");
                  Sleep(500);                         // Simple solution
                  RefreshRates();                     // Update data
                  continue;                           // At the next iteration
               }
            switch(Error)                             // Critical errors
               {
               case 2 : Alert("Common error.");
                  break;                              // Exit 'switch'
               case 5 : Alert("Outdated version of the client terminal.");
                  PlaceOrder=false;
                  break;                              // Exit 'switch'
               case 64: Alert("The account is blocked.");
                  PlaceOrder=false;
                  break;                              // Exit 'switch'
               case 133:Alert("Trading forbidden");
                  PlaceOrder=false;
                  break;                              // Exit 'switch'
               case 130:Alert("Invalid stops for some reasons");
                  PlaceOrder=false;
                  break;
               default: Alert("Occurred error ",Error);// Other alternatives   
               }
            Sleep(10000);
            }
 

I was looking at this code yesterday. If it goes off quote (error 136) it may potentially keep spinning in a while loop ? Could it spend all weekend in this state?

Does it handle ERR_NO_CONNECTION(6) the best way?

After refresh rates shouldn't it update the price (for a market order)?

Reason: