My EA does a double entry - page 4

 
doshur:

can I ask if PositionSelect() checks client side or sever side?

I have a strong feeling that the problem is caused by the delay where server (broker side) is processing the request and not updated the client side that's why PositionSelect() runs again

I do strongly feel that there is no difference when we use cTrade vs MqlTradeRequest way and Sleep function should help delay everything to get our client side gets "updated" before PositionSelect() runs again causing a double entry. Checking from my journal tab, > 2013.12.20 08:35:00 Trades '800****': exchange buy 0.01 EURUSD at market placed for execution in 313 ms <

putting sleep more than 400 should be safe??? 

 

What do you think? 


"I have a strong feeling that the problem is caused by the delay where server (broker side) is processing the request and not updated the client side that's why PositionSelect() runs again"

I also think that this is the cause for the double entry. In my code it's theoretically impossible to send a new order if the current position size is equal or greater than the max allowed position size, so when the PositionSelect() doesn't receive in time the status of the current position, my EA will send in a new order again. 


"putting sleep more than 400 should be safe??? "

The bigger the time interval the better but there is a problem. If you turn your position around, in two steps  (LONG to SHORT or SHORT to LONG), this extra time delay can be the cause for a bad execution price, especially during macro economic event.

 
snelle_moda:


"I have a strong feeling that the problem is caused by the delay where server (broker side) is processing the request and not updated the client side that's why PositionSelect() runs again"

I also think that this is the cause for the double entry. In my code it's theoretically impossible to send a new order if the current position size is equal or greater than the max allowed position size, so when the PositionSelect() doesn't receive in time the status of the current position, my EA will send in a new order again. 


"putting sleep more than 400 should be safe??? "

The bigger the time interval the better but there is a problem. If you turn your position around, in two steps  (LONG to SHORT or SHORT to LONG), this extra time delay can be the cause for a bad execution price, especially during macro economic event.

I think shouldn't be a problem. My EA don't reverse immediately when I just sent a buy/sell request. I'm putting my sleep to 800ms so that my EA has ample time to wait for the broker updates. Hopefully Sleep can resolve this problem here.
 
doshur:
I don't know if broker plays apart here but it seems our broker is the same. Alpari.

Pls remove broker name if needed. 
yes broker is the same.
 
snelle_moda:


I have had 1 more double entry since 03-10-2013. I use both methods for sending my order. See my previous post.

ah huh... just as I expected...
 

this is what I just implemented. hopefully can slove the problem

if(m_Trade.PositionOpen(Symbol(), ORDER_TYPE_BUY, LotSize, Price, 0, 0))
            {
               Sleep(800);

               if(m_Trade.ResultRetcode() == 10009)
               {
                  Print("Position opened in ", Symbol());

                  return;
               }
               else
               {
                  Print("Error opening position in ", Symbol(), " - ", m_Trade.ResultComment(), "\n", "Return Code Desc - ", m_Trade.ResultRetcodeDescription());
               }
            }
            else
            {
               Print("Error with PositionOpen in ", Symbol(), " - ", m_Trade.ResultComment(), "\n", "Return Code Desc - ", m_Trade.ResultRetcodeDescription());
            }
 
doshur:

this is what I just implemented. hopefully can slove the problem

As far as I know a result code = 10008 is also indicating that a trade is well placed.
 

I think it's very important to found the reason behind this issue, of course it's also important to have a workaround (Sleep ?) until we can understand fully what's happening. So I try to resume the situation :

  • When using the method PositionOpen from CTrade class, at least 3 users got at some time, 2 deals in the same direction instead of 1, resulting to a position with a doubled volume relative to what is expected.
  • The code posted initially by doshur, can explain why he can see in his log "Position opened in..." while no trade has been opened. This is because, even if PositionOpen() return true it doesn't mean a trade is placed. See documentation. But it can't explain why a "double" trade was placed.
  • I can only see 2 explanation for this "double" trade :
  1. PositionSelect() doesn't always return the true situation of the position. A position is opened but PositionSelect return false. Bug in PositionSelect then.
  2. A trade is placed but, but when PositionSelect() is called on the next tick, the position doesn't exist yet. To understand if it's possible we have to know the flow of operation when a trade is placed.
  • This problem seems occur on the same broker, with a symbol where Depth of Market is activated (can the concerned people confirm this please).
  • This problem occurs with synchronous order, asynchronous order wasn't used (please confirm).
  • The problem occurs randomly.
  • Klammeraffe reports no longer have the problem, but I can't see how the code he posted can explain that. Is this code executed on each tick ? Is this code executed after the use of PositionSelect() ? So maybe he removed the cause of the error or it's just random.
  • After checking the code, I can't see any difference between using CTrade class or MqlTradeRequest with OrderSend directly.

I agree with snella_moda that the best explanation is :

I think the problem is the (to slow) execution of the PositionSelect(Symbol()) function. Maybe, the new ticks come in so fast, the EA sends in a new order before it receives a response of the PositionSelect(Symbol()). So the current position size is not calculated properly. In my code, its theoretically impossible to send in a new/double order if the current position size is equal or greater than the max allowed position size, see code. 

But it's difficult to check.

I think the best think to do is to ask advice from Metaquotes. I will try that.

 
angevoyageur:
  • Klammeraffe reports no longer have the problem, but I can't see how the code he posted can explain that. Is this code executed on each tick ? Is this code executed after the use of PositionSelect() ? So maybe he removed the cause of the error or it's just random.

The line regarding "each tick" might be why it doesn't happen anymore.

The function is only executed, when a new bar appears. So, most likely, only the first tick of a bar can execute a trade. After the first bar, the code get's a 'return' until new bar appears. Maybe this solved it for me.

I think this piece of code is from the articles:

//-------------------------------------------------- Check for new bar     
         static datetime OldTime;
         datetime NewTime[1];
         bool newBar=false;
         
         int copied=CopyTime(Symbol(),Period(),0,1,NewTime);
         if (copied>0)
           {
             if (OldTime != NewTime[0])
               {  
                 newBar=true;
                 OldTime=NewTime[0];
               }
           }
         else
           {
            Print("Error in copying historical times data, error =",GetLastError());
            ResetLastError();
            return;
           }  
         if(newBar==false) return;      
//-------------------------------------------------- Check for new bar
 
Klammeraffe:

The line regarding "each tick" might be why it doesn't happen anymore.

The function is only executed, when a new bar appears. So, most likely, only the first tick of a bar can execute a trade. After the first bar, the code get's a 'return' until new bar appears. Maybe this solved it for me.

I think this piece of code is from the articles:

Yes, I think so. Thank you.
 
  • The code posted initially by doshur, can explain why he can see in his log "Position opened in..." while no trade has been opened. This is because, even if PositionOpen() return true it doesn't mean a trade is placed. See documentation. But it can't explain why a "double" trade was placed.

correction. There is a double "Position opened in..." and 2 trade has been opened.

 

  • This problem seems occur on the same broker, with a symbol where Depth of Market is activated (can the concerned people confirm this please).
Not sure about others but mine do have DOM

 

  • This problem occurs with synchronous order, asynchronous order wasn't used (please confirm).
I'm using the default cTrade settings.

 

  • The problem occurs randomly.
yes, randomly

 

Reason: