Doubt concerning error 146,trade context is busy

 

I read that this can happen when an EA is doing an operation, and another one ALSO wants to do the same kind of thing.


My doubt is... if I have a single EA, working across multiple charts, will it trip all over itself as if it were multiple EAs?


Thanks!

 
Apagador:

I read that this can happen when an EA is doing an operation, and another one ALSO wants to do the same kind of thing.

My doubt is... if I have a single EA, working across multiple charts, will it trip all over itself as if it were multiple EAs?

Thanks!

Yes it will.  Code a mutex into your EA so only one instance tries to use the trade context at any one time . .  avoid the problem in the first place rather than trying to sort the ensuing mess.
 

there's no need for a mutex in trade functions. IsTradeContextBusy() already implements a mutex like behaviour by itself. write your trade functions like this:

int OrderSendEx(..., ..., ...) {
   while (true) {
      if (IsStopped()) 
         return(catch("OrderSendEx()   module is going to stop..."));

      if (IsTradeContextBusy()) {
         if (__LOG) log("OrderSendEx()   trade context busy, retrying...");
         Sleep(300);                                                             
         continue;
      }

      ...
      ...                             // parameter validation, assignment etc.
      ...

      int ticket = OrderSend(symbol, type, lots, price, slippagePoints, stopLoss, takeProfit, comment, magicNumber, expires, markerColor);
      if (ticket > 0) 
         return(ticket);               // regular exit

      int error = GetLastError();

      if (error == ERR_TRADE_CONTEXT_BUSY) {
         if (__LOG) log("OrderSendEx()   trade context busy, retrying...");
         Sleep(300);                                                             
         continue;
      }
      if (error == ERR_REQUOTE) {
         continue;                                                               

      return(catch("OrderSendEx()   error = "+ error));
   }
}

it's important to check two times for ERR_TRADE_CONTEXT_BUSY. 1st before (IsTradeContextBusy) and 2nd after execution (if (error == ERR_TRADE_CONTEXT_BUSY)). in the little time between the 1st check and the OrderSend() call somebody else might get access to the trade context.

regards

 
paulepanke: there's no need for a mutex in trade functions. IsTradeContextBusy() already implements a mutex like behaviour by itself. write your trade functions like this:

There is NO mutex with isTradeContextBusy. The moment between checking and the orderSend, another EA could start to open a trade and your orderSend fails. All your code does is retry on error. Why bother to check, just orderSend and retry.

What about if you want to reduce the lotsize depending on currently open order risk. A mutex is required. I posted a mutex here.

 
WHRoeder:

There is NO mutex with isTradeContextBusy. The moment between checking and the orderSend, another EA could start to open a trade and your orderSend fails. All your code does is retry on error. Why bother to check, just orderSend and retry.

What about if you want to reduce the lotsize depending on currently open order risk. A mutex is required. I posted a mutex here.

-> What about if you want to reduce the lotsize depending on currently open order risk.

unrelated to the threads topic but anyway: i pass a reduced lotsize depending on currently open order risk to the function. OrderSend() should not handle money or risk managament issues, it should handle sending of orders.

-> A mutex is required.

i understand your statement as "i think a mutex is required". you'r free to do so but a simple example proves you wrong:
if you solely rely on a self defined mutex it wouldn't be possible to run any third party EA in parallel to your code or any other EA because third party code will definitely use different mutex names then you and yours or the third party EA's trading will sooner or later fail. this is for sure, 100%.
the only way to avoid this is by not using a mutex. a mutex can only solve this problem as long as all code is written by yourself. if you think differently you are free to do so.
 

Thanks guys.  I am now just retrying if it fails, after a moments (sleep).

However, I found out that the people who will be using the EA have no qualms about using OTHER EAs at the same time. So... I am screwed anyway, because I can't go around changing every EA they have a mind to use. I actually think I got so many errors concerning this matter because while they tested the EA I am coding, they were also using another EA, to me of unknown origin.

So... will going to the trouble of coding the mutex be really worth it, I ask myself...

 
Apagador:


So... will going to the trouble of coding the mutex be really worth it, I ask myself...

Yes if you want to run this EA on multiple pairs/timeframes without other EAs being run on the same terminal . . . adding the mutex is not a big deal,  there is an article about it and WHRoedeer's code also works.