Live-Trading Re-quots Handling?

 

Below is my favorite order-send with time-stamp code. This code works well in back-tests and demo-testing which doesn't have allot of broker re-quoting. Would this code survive re-quoting situations? I'm concern because when I OrderSend, I place a Time-Stamp on the variable assuming that the order went through. If the order does Not go through, the Time-Stamp will negate it from re-submitting the order again. Maybe adding if(OrderSend)>0 then {TimeStamp} might fix.

//+------------------------------------------------+
//+------------------------------------------------+
//----------Placing Orders
if(Cntr_Active==false)
if(Cntr_TimeStamp!=Time[0])
{
   //----------Buy Order
   if(Cntr_Direction==1)
   {
      if(Last_Order<=0)
      {
         OrderSend(Symbol(),OP_BUY,Lots,
         Price_Buy,4,
         Price_Buy-(Cntr_StopLoss*Point),
         Price_Buy+(Cntr_TakeProfit*Point),
         "Cntr Long",Cntr_Magic,0,Green);
         Cntr_TimeStamp=Time[0];
      }
   }
   //----------Sell Order
   if(Cntr_Direction==-1)
   {
      if(Last_Order>=0)
      {
         OrderSend(Symbol(),OP_SELL,Lots,
         Price_Sell,4,
         Price_Sell+(Cntr_StopLoss*Point),
         Price_Sell-(Cntr_TakeProfit*Point),
         "Cntr Short",Cntr_Magic,0,Red);
         Cntr_TimeStamp=Time[0];
      }
   }
}
//+------------------------------------------------+
//+------------------------------------------------+
 

Try this.

//+------------------------------------------------+
//+------------------------------------------------+
//----------Placing Orders
if(Cntr_Active==false)
if(Cntr_TimeStamp!=Time[0])
{
   //----------Buy Order
   if(Cntr_Direction==1)
   {
      if(Last_Order<=0)
      {
         while(!IsTradeContextBusy() || !IsTradeAllowed()) Sleep(5000);
         if(OrderSend(Symbol(),OP_BUY,Lots,
         Price_Buy,4,
         Price_Buy-(Cntr_StopLoss*Point),
         Price_Buy+(Cntr_TakeProfit*Point),
         "Cntr Long",Cntr_Magic,0,Green) )
         { Cntr_TimeStamp=Time[0]; 
         Print("Buy OrderSend Successful"); }
         else { Print("Buy OrderSend Error ",GetLastError()); if(GetlastError()==138 || GetLastError()==146) return(-1); }
      }
   }
   //----------Sell Order
   if(Cntr_Direction==-1)
   {
      if(Last_Order>=0)
      {
         while(!IsTradeContextBusy() || !IsTradeAllowed()) Sleep(5000);
         if(OrderSend(Symbol(),OP_SELL,Lots,
         Price_Sell,4,
         Price_Sell+(Cntr_StopLoss*Point),
         Price_Sell-(Cntr_TakeProfit*Point),
         "Cntr Short",Cntr_Magic,0,Red) )
         { Cntr_TimeStamp=Time[0]; 
         Print("Sell OrderSend Successful"); }
         else { Print("Sell OrderSend Error ",GetLastError()); if(GetlastError()==138 || GetLastError()==146) return(-1); }
      }
   }
}
//+------------------------------------------------+
//+------------------------------------------------+
 
Ohhh! Thank You so Very Much. Have a Great Trading Day :)
 
  1. while(!IsTradeContextBusy() || !IsTradeAllowed()) Sleep(5000);
    

    If you sleep, you must RefreshRates() and you want don't want the bangs:

    while(IsTradeContextBusy()||IsTradeAllowed()) {Sleep(5000);RefreshRates()}

  2. Print("Buy OrderSend Error ",GetLastError()); if(GetlastError()==138 || GetLastError()==146
    This won't work. (Won't compile either.) Calling GetLastError clears the error so you'll never see the 138 or the 146.
    int GLE=GetLastError();
    Print("Buy OrderSend Error ",GLE);
    if(GLE==ERR_REQUOTE || GLE==ERR_TRADE_CONTEXT_BUSY
 
Ah... Even better. I've got a little more understanding of the above codes. I'll be sure to study both versions before entering into my EA. Thank you WHRoeder and Wackena, you guys are at the up-most helpful on this forum.
 
U may want to have a look at LibOrderReliable (Google it...). It's a library of drop-in alternatives to the Trading functions (OrderSend(), OrderClose(), etc.). It's a bit outdated, but it will give u a good example of how to treat most errors.
 
Cool, just what I needed now that I entering the polishing of my EA phase. Initially Error handling processes seemed complex and redundant. But It's not so intimidating anymore due to my-getting used to the basics.  As always thanks again Gordon.
Reason: