My EA does a double entry - page 9

 

Hi

This approach only works if the current position size is zero and the new trade is executed successfully with the desired position size.

See code:

#include <Trade\Trade.mqh>

//--- global variables
CTrade trade;
bool position_opened=false;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   bool conditions_to_open;
   ENUM_ORDER_TYPE order_type;
   double lot;
   double price,sl,tp;

//---...set variables

//--- inside OnTick()
   if(conditions_to_open && !position_opened) //-- Or position_opened==false
     {
      if(trade.PositionOpen(_Symbol,order_type,lot,price,sl,tp,"comment")
         && 
         (trade.ResultRetcode()==10009 || trade.ResultRetcode()==10008)) //-- Or others condition according to your needs
        {
         //--- set the global variable to true to avoid duplicate orders
         position_opened=true;
         Print("Success!");
        }
      else
        {
         Print("Error = ",GetLastError(), "trade error = ", trade.ResultRetcode());
         //--- Sets the global variable to false
         // position_opened=false;                                         //-- Not needed as position_opened is already false
         return;
        }

     }
//--- 
  }


But what if your EA is turning the position around: from a LONG to a SHORT position or vice versa and this action is performed in 2 different trades?

After the first trade the "position_opened" boolean is set to "TRUE" and the EA can't open the 2e trade because the condition is checking "if(conditions_to_open && !position_opened)", so the second trade will be blocked.

And what if the position size is only partially opened? You need a second trade to get your position size to the correct size.


In the end, to make "a long story short", you always need the PositionSelect(Symbol()) function to calculate the CURRENT position size and this is where the problem all started (and this forum thread).

At the critical moment, when the EA is calling the PositionSelect(Symbol()), the terminal as not yet updated the CURRENT position size and the EA is working with the wrong position size and sends in a new double order to the trade server.  


I think the approach from (figurelli) is better because you are forced to wait until the EA has updated the PositionSelect(Symbol()) function to the right position size.  


bool fatalError=false; // atention: declare this as global

....

if(fatalError == false) 
      {
      if(m_Trade.PositionOpen(Symbol(), ORDER_TYPE_BUY, LotSize, Price, 0, 0)) 
            {
            Print("Position opened in ", Symbol());
            int maxTimeout=0;
            while(!PositionSelect(Symbol())) 
                     {
                     Sleep(100);
                     maxTimeout++;
                     if(maxTimeout > 100) 
                           {
                           Print("### PositionSelect fatal error!");
                           fatalError = true;
                           break;
                           }
                     }
            Print("--> PositionSelect delay=",maxTimeout * 100);
            break;
            }
      }
 
snelle_moda:

Hi

This approach only works if the current position size is zero and the new trade is executed successfully with the desired position size.

See code:


But what if your EA is turning the position around: from a LONG to a SHORT position or vice versa and this action is performed in 2 different trades?

After the first trade the "position_opened" boolean is set to "TRUE" and the EA can't open the 2e trade because the condition is checking "if(conditions_to_open && !position_opened)", so the second trade will be blocked.

And what if the position size is only partially opened? You need a second trade to get your position size to the correct size.


In the end, to make "a long story short", you always need the PositionSelect(Symbol()) function to calculate the CURRENT position size and this is where the problem all started (and this forum thread).

At the critical moment, when the EA is calling the PositionSelect(Symbol()), the terminal as not yet updated the CURRENT position size and the EA is working with the wrong position size and sends in a new double order to the trade server.  


I think the approach from (figurelli) is better because you are forced to wait until the EA has updated the PositionSelect(Symbol()) function  


You are right, both approach have limitations.

The origin of this issue is now identified clearly. Each one have to adapt his code depending of his strategy. I don't think there is a general method that can apply on all cases.

 
Do we need an OrderSendReliable() Library for the Great & Powerful mql5?
 
Ubzen:
Do we need an OrderSendReliable() Library for the Great & Powerful mql5?
I don't think sarcasm can be of any help. Though it's clearly a bad design which leads to such complexity.
 
angevoyageur:

You are right, both approach have limitations.

The origin of this issue is now identified clearly. Each one have to adapt his code depending of his strategy. I don't think there is a general method that can apply on all cases.


Or we have to wait for an "official statement" from the Metaquotes guys themselves.

Is there already a response from the service desk?

 
angevoyageur: I don't think sarcasm can be of any help. Though it's clearly a bad design which leads to such complexity.

If I agreed with you would you consider it un-helpful? .

I just taught that the solution of Sleeping until Terminating Trading reminded me of OrderSendReliable(). I taught these would have been addressed more elegantly.

It also reminds me of the old_documents handling ... where it goes { and all trading should be stopped }.

Anyways, apologies didn't mean to pick on mql5. 

 
snelle_moda:


Or we have to wait for an "official statement" from the Metaquotes guys themselves.

Is there already a response from the service desk?

No. I was just thinking about that...It would be probably useful if all concerned people write a ticket to ServiceDesk about this issue. However I am very sceptical if MQ is willing to change this design. But we can try.

People can write to ServiceDesk and report the ticket# here. Mine is 

Errors, MetaTrader 5 MQL, Open, Start: 2013.12.23 19:08, #916435
Get in touch with developers using Service Desk!
Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
 
Ubzen:

If I agreed with you would you consider if un-helpful? .

I just taught that the solution of Sleeping until Terminating Trading reminded me of OrderSendReliable(). I taught these would have been addressed more elegantly.

It also reminds me of the old_documents handling ... where it goes { and all trading should be stopped }.

Anyways, apologies didn't mean to pick on mql5. 

I wonder if a similar issue can't exist also in mql4, I read at least 2 topics in the last months on mql4 forum from users who complaints about double trades. Not sure it's related, but it can be worth to check these topics. Do you read these ones also ?
 
angevoyageur: I wonder if a similar issue can't exist also in mql4, I read at least 2 topics in the last months on mql4 forum from users who complaints about double trades. Not sure it's related, but it can be worth to check these topics. Do you read these ones also ?

Yes, but its hard to tell if its because of bad coding. Double trading was a problem even before the multiple trading thread. The way its traditionally handled is the way snelle_moda handles it.

 
Ubzen:

Yes, but its hard to tell if its because of bad coding. Double trading was a problem even before the multiple trading thread. The way its traditionally handled is the way snelle_moda handles it.

Not sure I understand what you mean. This issue we are talking in this topic is not caused by a bad coding but by a bad design in mql5 (it's my opinion, or maybe it's simply a bug ?). What do you mean by "multiple trading thread" ?
Get in touch with developers using Service Desk!
Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.