MT4 : Journal shows more OrderSend() calls with TradeContextBusy than the EA logged in its logfile

 
I wrote an EA for Metatrader4 and it logs everything that the EA does in a log file that is opened like that:


filehandle=FileOpen("EA_LogFile.csv",FILE_CSV|FILE_WRITE|FILE_SHARE_READ,'\t');


before I call OrderSend() I check the following conditions:


if (IsExpertEnabled() && IsConnected() && !IsTradeContextBusy() && IsTradeAllowed())

{ int ticket = OrderSend( ... ); }
  • when the OrderSend() returns a ticket, I'm fine.
  • But with each OrderSend() call that returns „0“ I count the tries, and at the next tick the EA tries again to open that order. After 10 tries, the trade will be deleted. All these tries are logged in the logfile with the FileWrite(filehandle, …); command.


The point is, that in the logfile very often there's just one try. So it looks like the OrderSend() returned a ticked at its first call. But when i look at the journal there are more OrderSend() calls listed with  "Trade context is busy". How can that be?


Here the journal:


0 11:00:00.076 '901619': order sell market 6.00 NZDCAD.bo sl: 0.00000 tp: 0.00000

1 11:00:00.842 '901619': order sell 6.00 NZDCAD.bo opening at market sl: 0.00000 tp: 0.00000 failed [Trade context is busy]

0 11:00:00.842 '901619': order sell market 6.00 NZDCAD.bo sl: 0.00000 tp: 0.00000

1 11:00:01.623 '901619': order sell 6.00 NZDCAD.bo opening at market sl: 0.00000 tp: 0.00000 failed [Trade context is busy]

0 11:00:01.623 '901619': order sell market 6.00 NZDCAD.bo sl: 0.00000 tp: 0.00000

0 11:00:02.404 '901619': order was opened : #34705702 sell 6.00 NZDCAD.bo at 0.92206 sl: 0.00000 tp: 0.00000



how can i fix that?

 
Show your code.
 
/*
there are different arrays where all details for the trades are stored in:

int         trade_status[50];       // 0=empty ; 1=ready for ordersend ; 2=order is in the market ; 3=order has closed
string      trade_symbol[50];       // symbol
int         trade_tf[50];           // timeframe
DIRECTION   trade_dir[50];          // UP, DOWN
double      trade_price[50];
double      trade_amount[50];
datetime    trade_signaltime[50];   // when the trade entry was calculated
datetime    trade_starttime[50];    // entry time
datetime    trade_expiretime[50];   // expire time
int         trade_ticket[50];
int         trade_magicnr[50]; 
int         trade_ordertry[50];     // counter for the tries, how often ordersend() was called unsuccessfully

*/



void ORDER_orders_from_tradearray ()
{
   int ticket = 0;
   int order_type = 0;
   
   // go through all trades
   for (int i=0 ; i<trade_array_size ; i++)
   {
      bool time_over = TimeCurrent()>trade_starttime[i] + timeout_in_sec ;
      
      ticket = 0;
      
      // if trade is ready for ordersend and starttime has come
      if (trade_status[i]==1 && trade_starttime[i] <= TimeCurrent() ) 
      {
         // check before ordersend
         if ( !time_over && !IsTesting() && (!IsExpertEnabled() || !IsConnected() || IsTradeContextBusy() || !IsTradeAllowed()) ) 
         {
            FileWrite(filehandle,LOG_time_format(),"|",trade_symbol[i],"M"+string(trade_tf[i]),i,""," ORDER WAIT ... because of '!IsExpertEnabled() OR !IsConnected() OR IsTradeContextBusy() OR !IsTradeAllowed()' " );
            continue;
         }
         
         if (trade_dir[i]==UP  ) order_type= OP_BUY;
         if (trade_dir[i]==DOWN) order_type= OP_SELL;
         
         double amount = int ( trade_amount[i] ); 
         
         FileWrite(filehandle,LOG_time_format(),"|",trade_symbol[i],"M"+string(trade_tf[i]),i,"","        timeover check - log the differences: (tradetime:"+string(trade_starttime[i])+", signaltime:"+string(trade_signaltime[i])+")");

         
         if ( !time_over )
         {
            ticket = OrderSend(trade_symbol[i],order_type,amount,iClose(trade_symbol[i],trade_tf[i],0),0,0,0,"BO exp:"+IntegerToString(trade_ablauf_in_sec[i]),trade_magicnr[i],0,clrWhite);
         }
            
         // analyse ordersend() return
         if (ticket>0) 
         {
            if ( OrderSelect(ticket,SELECT_BY_TICKET) ) 
            {
               // store order details in the trade..[] arrays
               trade_ticket[i]      = ticket;
               trade_starttime[i]   = OrderOpenTime();
               trade_expiretime[i]  = trade_starttime[i] + expire_time;
               trade_price[i]       = OrderOpenPrice(); 
            }
            
            FileWrite(filehandle,LOG_time_format(),"|",trade_symbol[i],"M"+string(trade_tf[i]),i,"&"," ORDER NEW from trade","amount:", DoubleToStr(amount,2), "ticket:",ticket,"trade_dir:",trade_dir[i],"signal_time:",trade_signaltime[i],"orderCloseTime:",trade_expiretime[i], "orderOpenPrice:",trade_price[i], "broker-order-open-time:", TimeToStr(trade_starttime[i],TIME_DATE|TIME_SECONDS) );
                
            trade_status[i]=2; // 2 means trade is in the market, success
         }
         else
         {
            // check GetLastError()
            ERROR_check4error_output_to_logfile();
            
            trade_ordertry[i]++;
            
            // max 10 tries or timeover
            if (trade_ordertry[i] >= 10 || time_over )
            {
               FileWrite(filehandle,LOG_time_format(),"|",trade_symbol[i],"M"+string(trade_tf[i]),i,""," TimeOver or ToManyTries" );
                  
               trade_status[i]=0; // 0 means the array-item with index "i" is empty
            }
            else
            {
               FileWrite(filehandle,LOG_time_format(),"|",trade_symbol[i],"M"+string(trade_tf[i]),i,"."," ORDER - TRY #",trade_ordertry[i], "amount:",DoubleToStr(trade_amount[i],2),"trade_dir:",trade_dir[i]);
            }
         }
      }
   }
}
hello Alain, here's the code. this function is called by OnTick() or OnTimer() and its the only function that calls OrderSend()
Reason: