How can i avoid ERR_TRADE_TIMEOUT on OrderSend ?

 

Hi...

 

Houston, we have a problem! lol

How can i avoid problems with error 128 on OrderSend ?

On this code... i am opening a first order, and next order is opened as a Pending SELLSTOP/BUYSTOP order ... But, only if there is at least one ACTIVE ORDER...

But... if i sent a order and returns error # 128 ( ERR_TRADE_TIMEOUT )... many brokers, despite the given timeout, will accept the order... but, on next iteraction of OnTick() function tick my AssetActiveTrades() function will count active trades as 0 and my logic will fail!

I need to find a way to not open more trades when i receive the timeout till my metatrader order history update itself... to count it right...

int AssetActiveTrades()
{
   int i=0, c=0;
   for(i=0; i < OrdersTotal(); i++)
   {
      if( !OrderSelect(i, SELECT_BY_POS, MODE_TRADES) ) continue;
      if( OrderSymbol() != _Symbol ) continue;
      if( OrderMagicNumber() != MyMagicNumber ) continue;
      if( OrderType() == OP_BUY || OrderType() == OP_SELL )
         c++;
   }
  
   return c;
}


void OnTick()
{
   int ticket = -1;
   int cmd = -1;
   int activetrades=AssetActiveTrades();
   double price=0;
   bool buy=false, sell=false;
  
   buy=CheckForBuy();
   if( !buy ) sell=CheckForSell();

   // BUY trades start
   if( buy )
   {
      RefreshRates();
      price = Ask;
      if(IsTradeAllowed())
      {
         cmd = OP_BUY;
         if( activetrades > 0 ){
            cmd = OP_BUYSTOP;
            price += Pending_PIPs*myPoint;
         }
         ticket = OpenMyOrder(cmd, price, MyLot, SL_PIPs, TP_PIPs);
         if(ticket <= 0) return;
      }
   }
   // BUY trades end

   // SELL trades start   
   if( sell )
   {
      RefreshRates();
      price = Bid;
      if(IsTradeAllowed())
      {
         cmd = OP_SELL;
         if( activetrades > 0 ){
            cmd = OP_SELLSTOP;
            price -= Pending_PIPs*myPoint;
         }
         ticket = OpenMyOrder(cmd, price, MyLot, SL_PIPs, TP_PIPs);
         if(ticket <= 0) return;
      }
   }
   // SELL trades end

}
 
Wemerson Guimaraes: But... if i sent a order and returns error # 128 ( ERR_TRADE_TIMEOUT )... many brokers, despite the given timeout, will accept the order... but, on next iteraction of OnTick() function tick my AssetActiveTrades() function will count active trades as 0 and my logic will fail!

"How can i avoid" Pay your broker a $Million for a co-located server. Otherwise you can't with a internet connection.

If you get timeout, the order may or may not have been received by the server, may or may not have been opened, and if opened the returned ticket numer not received.

Wait for the next tick, if your "count active trades as 0" that doesn't mean your logic will fail, it means the server never received the request. Reevaluate conditions and retry.

 
Wemerson Guimaraes:

...

I need to find a way to not open more trades when i receive the timeout till my metatrader order history update itself... to count it right...

There is no easy way. You could set a flag when you receive error 128, then don't trade while the flag is set.

The problem arise when you want to reset the flag, if you receive an order it's ok, but you have no idea if you will receive it or if it's just lost. So you could set your own timeout with the flag... But can we be sure you will not receive your order after this timeout ?

 
whroeder1:

"How can i avoid" Pay your broker a $Million for a co-located server. Otherwise you can't with a internet connection.

If you get timeout, the order may or may not have been received by the server, may or may not have been opened, and if opened the returned ticket numer not received.

Wait for the next tick, if your "count active trades as 0" that doesn't mean your logic will fail, it means the server never received the request. Reevaluate conditions and retry.

Today testing on ICMarkets,  i've seen when timeout occurs, but the broker opens the order... and only a couple of ticks after the timeout event, my AssetActuveTrades() returns a count > 0... then i think this is taking a while to metatrader receives the update about the new order... 

Then i am thinking to check if get timeout error... and in this case, will call a Sleep() for a while... but, i don't know if this is the best approach.

Guys, sorry for my poor english... using google translator!!! 

 
and only a couple of ticks after the timeout event, my AssetActuveTrades() returns a count > 0

I've never seen this. The terminal recognizes a connection failure and errors the OrderSend. Once the connection is reestablished, the charts must be updated and so is the order lists. Only after that is a tick received.

But a lot of internals has changed since build 600. Newly attached indicators received a OnCalculate, even when there is no market ticks, for example.

It is also possible, that windows successfully resent the last packet (with the OrderSend.) while the terminal was trying to reconnect, resulting in a delayed opened order.

Since you say several ticks were received, before the lists are updated. I'd set a timeout flag. On the first new tick, sleep for a few seconds, clear the flag, and return. The list has to be updated by next tick.

 
whroeder1:

I've never seen this. The terminal recognizes a connection failure and errors the OrderSend. Once the connection is reestablished, the charts must be updated and so is the order lists. Only after that is a tick received.

Since you say several ticks were received, before the lists are updated. I'd set a timeout flag, on the first new tick, sleep for a few seconds, clear the flag, and return. The list have to be updated by next tick

Me too... never seen this.


I am waiting for next episode to post here....  

Reason: