why my EA occasionally place 2 orders at the exact same time - page 2

 
fhjtiger:

Hi, I think the problem is caused by the long processing time of trade server.

if trade condition is satisfied, buy or sell order was sent to the trade server. When your EA gets true from the server, it does not mean  the success creation of position. It only means your order was accepted and waiting for further process. So for these codes to modify the position:

       // set stoploss and takeprofit
           double sl=0,tp=0;
            if(PositionsCounter()==1)

they are immediately executed just after the sending of order. At this time, the position may be not created by the server. the result of executing " if(PositionsCounter()==1) " may be false. At this situation, the position modification should be executed during the coming of next tick if position was created by the server then.

The worst situation is that when the next tick comes,  the server does not create the position. Just here your EA send one more order you don't wish.

Thank you. I also have the same thought.
 
phi.nuts:
If we are using OrderSendAsync then there is no way of telling whether position is opened or not. However since user greenteen and his code above was sending order using CTrade Class which using OrderSend, then we could easily tell whether the position is opened or not by simply checking OrderSend result and return code.  

Hello, I am afraid that the return value of OrderSend does not indicate a sign of successful execution of a trade operation.

 

https://www.mql5.com/en/docs/trading/ordersend

Return Value

In case of a successful basic check of structures (index checking) returns true. However, this is not a sign of successful execution of a trade operation. For a more detailed description of the function execution result, analyze the fields of result structure.

 

Documentation on MQL5: Trade Functions / OrderSend
Documentation on MQL5: Trade Functions / OrderSend
  • www.mql5.com
Trade Functions / OrderSend - Documentation on MQL5
 

I also have a new question,  I found the magic of a deal is not same as  EA's magic (3344), although this deal is opened by EA.

 I use the below code to check the magic of a deal in History tab, but it show  0 instead of 3344

      

  long tkt = 17795213;   // a history deal's ticket    //           
  Comment(tkt,"  --- ",HistoryDealGetInteger(tkt,DEAL_MAGIC));   //  0  
 

Hello, I am afraid that the return value of OrderSend does not indicate a sign of successful execution of a trade operation.

 ...

 That is true, however this is what I said (see the yellow highlighted word :)

phi.nuts:
...then we could easily tell whether the position is opened or not by simply checking OrderSend result and return code.  


I also have a new question,  I found the magic of a deal is not same as  EA's magic (3344), although this deal is opened by EA.

 I use the below code to check the magic of a deal in History tab, but it show  0 instead of 3344

Did you check in the mql5 documentation what DEAL_MAGIC is ?, I haven't check but I think that's for pending orders.

 

 
phi.nuts:

 That is true, however this is what I said (see the yellow highlighted word :)


Did you check in the mql5 documentation what DEAL_MAGIC is ?, I haven't check but I think that's for pending orders.

 

Hello Phi,

DEAL_MAGIC is for deals not for pending orders 

Reason: