double order

 

While testing my EA on a demo account, I noticed that I got an order executed twice in the same second. Although I have a check before using OrderSend to make sure that I do not currently have an order open. What would you suggest I do to prevent this?

   total=OrdersTotal();
   
   if(total<1) 
   {  ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss,Ask+TakeProfit,"",0,0,Green); }
DoubleOrder
 

This was previously reported, I didn't believe it then.

It's possible that there is now a delay in transferring open orders and a new tick comes in first.

Adding a 5 second delay after opening, before returning, should be sufficient to insure the update occurs before the next tick.

 
   if(total<1) 
   {  ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss,Ask+TakeProfit,"",0,0,Green);
            Sleep (3000); }

I am still having the same issue after the change

 
jshumaker:

I am still having the same issue after the change

I don't know if this is a good idea or not as it involves a possible endless loop

total=OrdersTotal();
   
   if(total<1) 
   {  ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss,Ask+TakeProfit,"",0,0,Green); }

   if(ticket>0)
      while ( total <1 )
         {
         total=OrdersTotal();
         }
 

Keep your records of initiated orders in memory.

When initiate a new order, check whether another live order triggered by the same logic already exists, if yes, this is a duplicated execution.

 
jshumaker:

I am still having the same issue after the change

Where do you update the variable total ? can yo please show all the relevant code . . .
 
total=OrdersTotal();
   
   if(total<1) 
   {  if(hB - lB <= 0.0035 && (tH >= 17 || tH <= 3))
      {  
         if (Ask <= lB + 0.0002)
         {  ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss,Ask+TakeProfit,"",0,0,Green);
            Sleep (10000); }
         else if (Bid >= hB - 0.0002)
         {  ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss,Bid-TakeProfit,"",0,0,Red);
            Sleep (10000);  }  }  }
 
never mind i think i see the issue
 
jshumaker:
never mind i think i see the issue
Please share what you have fond as this is potentially a very serious issue . . .
 

I believe the total variable was getting reset. I believe that this fixes the issue. Although I did not have my EA running in demo mode since I made the change. So I cannot say for certain if that fixes it, but I believe I got the double orders after a code change. Therefore I really felt that it had to be something with my code

//total=OrdersTotal(); 
//   if(total<1) 
// replaced with this
if(OrdersTotal()==0)
Reason: