My EA does a double entry - page 3

 
doshur:
Omg. So sleep doesn't help?

What can we do to avoid this?

It helped for me. I used snelle_modas tip plus the sleep. That worked out.

But since then I revamped the way trades are opened. Now I don't need neither of these solutions. Here is what I wrote to angevoyageur yesterday. I hope it helps:

Hi,

well last time I solved it, was using sleep function after trade. But with my new bot this is not needed anymore. Maybe that is because opening a trade is now handled differently. This first bot I had this problem with (maybe other EAs also had this way of opening a trade and that's why had the problem too (e.g. metaquant)) used this method:

void SetOrder(ENUM_ORDER_TYPE type, double lot)
{
   int ticket = -1;
   ResetLastError();
   double price = 0;
   double ask, bid;
   ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
   bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
   if (maxSpread != 0 && NormalizeDouble(ask - bid, _Digits) >= NormalizeDouble(maxSpread * point, _Digits)) return; 
   if (type == ORDER_TYPE_BUY) price = ask;
   if (type == ORDER_TYPE_SELL) price = bid;
   trade.PositionOpen(Symbol(), type, lot, price, 0, 0, "");
   Sleep(1000);
   int err = GetLastError();
   if (err > 0) Print(ErrorDescription(err));
}


Now I'm opening orders like I have learned from the documentation and don't have this problem anymore:

void open_sell(double xlot, int xTP)
{
         MqlTradeRequest mrequest;
         MqlTick latest_price;      
         MqlTradeResult mresult;   
            if(!SymbolInfoTick(Symbol(),latest_price))
              {
               Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
               return;
              }
         ZeroMemory(mrequest);
         mrequest.action=TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.bid,Digits());           // latest Bid price
         if (StopLoss!=0) mrequest.sl = NormalizeDouble(latest_price.bid + StopLoss*point,Digits()); // Stop Loss
         if (xTP!=0) mrequest.tp = NormalizeDouble(latest_price.bid - xTP*point,Digits()); // Take Profit
         mrequest.symbol = Symbol();                                          // currency pair
         mrequest.volume = xlot;                                              // number of lots to trade
         mrequest.magic = EA_Magic;                                          // Order Magic Number
         mrequest.type= ORDER_TYPE_SELL;                                     // Sell Order
         mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         mrequest.deviation=100;                                             // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
          // Print("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Print("The Sell order request could not be completed -error:",GetLastError());
            ResetLastError();
            return;
           }
}


Maybe this helps. I see that doshur uses a similar way top open trades like I did when it caused this behaviour.

Kind regards
 
So there's a problem with ctrade class?

Can anyone confirm this before I change my code?
 
doshur:
So there's a problem with ctrade class?

Can anyone confirm this before I change my code?

I can just say that I don't have this problem anymore after removing ctrade class.

You might want to create a second version of the EA that uses the "old fashioned" way of opening a trade and see if it helps.


On the other hand, sleep function did solve the problem for me too.

Documentation on MQL5: Common Functions / Sleep
Documentation on MQL5: Common Functions / Sleep
  • www.mql5.com
Common Functions / Sleep - Documentation on MQL5
 
doshur:
So there's a problem with ctrade class?

Can anyone confirm this before I change my code?
Please be patient, I will answer you as soon as possible. I will try to reproduce the problem when Market will be open.
 
I don't know if broker plays apart here but it seems our broker is the same. Alpari.

Pls remove broker name if needed. 
 
Klammeraffe:

I can just say that I don't have this problem anymore after removing ctrade class.

You might want to create a second version of the EA that uses the "old fashioned" way of opening a trade and see if it helps.


On the other hand, sleep function did solve the problem for me too.

This is an interesting point.

I use the ctrade class for adjusting the stoploss value.

      
My_Trade.PositionModify(Symbol(), 
                        NormalizeDouble(dPositionStoploss,   Digits()), 
                        NormalizeDouble(dPositionTakeProfit, Digits())
                        ); 


The opening of the position itself is done by using the "old fashioned" way.

mrequest.action         = TRADE_ACTION_DEAL;                            // immediate order execution stoploss en takeprofit worden aangepast mrequest.price          = NormalizeDouble(Latest_Price.ask, Digits());  // latest ask price mrequest.symbol         = Symbol();                                     // currency pair mrequest.volume         = dTradePosition_Size;                          // number of lots to trade mrequest.magic          = EA_Magic_Number;                              // Order Magic Number mrequest.type           = ORDER_TYPE_BUY;                               // Buy Order mrequest.type_filling   = ORDER_FILLING_RETURN;                         // Order execution type mrequest.deviation      = 1000;                                         // Max prijs afwijking                                                                                                        OrderSend(mrequest,mresult); //--- send order

Is it possible that the ctrade class is sending in a new double order when I'm adjusting the stoploss order? It seems strange. 

 
Klammeraffe:

I can just say that I don't have this problem anymore after removing ctrade class.

You might want to create a second version of the EA that uses the "old fashioned" way of opening a trade and see if it helps.


On the other hand, sleep function did solve the problem for me too.

If you look into ctrade class. Is there any difference using this class vs the mqltraderequest way?
 
snelle_moda:

That's a good point. Maybe I should only use the change in the BID price.

A BAR on the chart is also based on the BID price? 


For the trigger signal of my EA I'm only interested in the change of the price on which the 1 minute BAR is based.

Snelle_moda do you still get double entry using mqltraderequest to send order?
Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Standard Constants, Enumerations and Structures / Data Structures / Trade Request Structure - Documentation on MQL5
 

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? 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Standard Constants, Enumerations and Structures / Data Structures / Trade Request Structure - Documentation on MQL5
 
doshur:
Snelle_moda do you still get double entry using mqltraderequest to send order?


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

Reason: