MT4 fails to sync executed pending Orders during fast moving market and recognizes it a pending order that cant be deleted

[Deleted]  

MT4 fails to sync executed pending Orders during fast moving market and recognizes it a pending order that cant be deleted. It occurs occasionally during fast moving market.

Sometimes I notice an open pending order that could't be deleted by EA and the market have passed that price. If i try deleting  it manually is says ''invalid '',but when i restart MT4 it appears to be an open trade. Can any help me or i'm experiencing the same issue ?


This code creates new pending Orders

if( rateChange > VelocityTrigger * Point && avgSpread <= maxSpread && totalBuyStop < TradeDeviation ) {
         if( AccountFreeMarginCheck( Symbol(), OP_BUY, lotSize() ) <= 0 || GetLastError() == 134 ) {
            Print( "Notice: Not enough free margin to open trade. Expert is removed." );
            ExpertRemove();
            return ( 0 ); 
         }
         ticket = OrderSend( Symbol(), OP_BUYSTOP, lotSize(), Ask + ( totalBuyStop + 1.0 ) * ( Point * TradeDelta ), Slippage, 0, 0, TradeComment, MagicNumber, 0 );
      } 
      if( rateChange < - VelocityTrigger * Point && avgSpread <= maxSpread && totalSellStop < TradeDeviation ) {
         if( AccountFreeMarginCheck( Symbol(), OP_SELL, lotSize() ) <= 0 || GetLastError() == 134 ) {
            Print( "Notice: Not enough free margin to open trade. Expert is removed." );
            ExpertRemove();
            return ( 0 );
         }
         ticket = OrderSend(Symbol(), OP_SELLSTOP, lotSize(), Bid - ( totalSellStop + 1.0 ) * ( Point * TradeDelta ), Slippage, 0, 0, TradeComment, MagicNumber, 0 );

      }   




This code check for order status

case OP_BUYSTOP:
               if( avgSpread > maxSpread ||  MathAbs( rateChange ) < VelocityTrigger * Point  ) 
                  r = OrderDelete( OrderTicket() );
               totalBuyStop++;
            break;
            case OP_SELLSTOP:
               if( avgSpread > maxSpread ||  MathAbs( rateChange ) < VelocityTrigger * Point  ) 
                  r = OrderDelete( OrderTicket() );
               totalSellStop++;
            break;
 
 = OrderDelete( OrderTicket() );
You can not use any Trade Functions until you select an order.
 

It should look something like this...

   for(int i=OrdersTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==Symbol()&&OrderMagicNumber()==MAGIC)
         if(avgSpread > maxSpread|| MathAbs(rateChange) < VelocityTrigger * _Point)
            for(int j=0;j<2 && !OrderDelete(OrderTicket());j++) // extra attempt if first one fails
               Print(__FUNCTION__," <!!!> ",ErrorDescription(GetLastError()));
[Deleted]  
whroeder1:
You can not use any Trade Functions until you select an order.

Thanks i will check it out

[Deleted]  
nicholishen:

It should look something like this...


I will try to apply that and it resolves it

[Deleted]  
nicholishen:

It should look something like this...


I tried it but same issue occurred with two trades.

the below image shows the pending trades


 
Chike Assuzu:

I tried it but still same issue occured.

this image shows the pending trades

Your using the wrong orders. Limit orders go under the price and stop orders go above it.
[Deleted]  
nicholishen:
Your using the wrong orders. Limit orders go under the price and stop orders go above it.
 EA already executed several trades before it occured again on the last 2 trades you can see the error
 
Chike Assuzu:
 EA already executed several trades before it occured again on the last 2 trades you can see the error
Yeah because you're sending the wrong orders. Use limit orders instead.
[Deleted]  

after i closed the terminal, and restarted it. It showed the open trades and closed it immediately. because the TP have already passed.