PositionsTotal() return 0 after a call of function trade.PositionOpen()

 

Hi,

After running the following code block:

   if(!trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,size,openPrice,0,0,RobotName+" "+timeframeToString(_Period)) || trade.ResultRetcode()!=TRADE_RETCODE_DONE)
   {

      lastErrorMessage="Position open failed. Return code="+IntegerToString(trade.ResultRetcode());
      lastDateErrorMessage=TimeCurrent();        
      Print("code retour du trade:",trade.ResultRetcode());
      return -1;
      
   }
   else
   {
      Print("PositionsTotal:",PositionsTotal());
   }

PositionsTotal() return 0. 

Actually I have different result. For some broker accounts it returns 1 and other broker accounts it return 0.

What is the root cause of this behavior and how to get right number of open orders.

Note I am using only hedging account.

Regards,

Dorian

 
Dorian Baranes:

PositionsTotal() return 0. 

Actually I have different result. For some broker accounts it returns 1 and other broker accounts it return 0.

After sending the order you receive a market order (OrdersTotal() > 0) with ORDER_STATE_STARTED and OrderGetInteger(ORDER_POSITION_ID) == 0.


Details are described here in Russian.


Look at the description for this EA.

** When a trading order is sent to a server, an occasional delay in execution may lead to incorrectly countinging the number of market positions. If such an "undefined" state is detected, the Expert Advisor waits for the specified number of seconds and then reads the environment again.
*** The number of such waiting periods within one tick is set in the EA parameters. Having taken all the allowed attempts to get the accurate information on the environment, the Expert Advisor exits processing and waits for the next tick. The EA will repeat these attempts on this new tick if the trading environment has failed to update by this time.

//+------------------------------------------------------------------+
//| Возвращает "неопределённое" состояние торгового окружения        |
//+------------------------------------------------------------------+
bool IsUncertainStateEnv(const string symbol_name,const ulong magic_number)
  {
   if(MQLInfoInteger(MQL_TESTER)) return false;
   int total=OrdersTotal();
   for(int i=total-1; i>WRONG_VALUE; i--)
     {
      if(OrderGetTicket(i)==0) continue;
      if(OrderGetInteger(ORDER_TYPE)>ORDER_TYPE_SELL) continue;
      if(OrderGetInteger(ORDER_MAGIC)!=magic_number) continue;
      if(!OrderGetInteger(ORDER_POSITION_ID) && OrderGetString(ORDER_SYMBOL)==symbol_name)
         return true;
     }
   return false;
  }


I myself write like this (works correctly)

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

   if(OrderSend(_Symbol,OP_BUY,size,openPrice,slippage,0,0,RobotName+" "+timeframeToString(_Period)) == -1)
   {

      lastErrorMessage="Position open failed. Return code="+IntegerToString(MT4ORDERS::LastTradeResult.retcode);
      lastDateErrorMessage=TimeCurrent();        
      Print("code retour du trade:",MT4ORDERS::LastTradeResult.retcode);
      return -1;
      
   }
   else
   {
      Print("PositionsTotal:",PositionsTotal());
   }
 

Hi,

** When a trading order is sent to a server, an occasional delay in execution may lead to incorrectly countinging the number of market positions. If such an "undefined" state is detected, the Expert Advisor waits for the specified number of seconds and then reads the environment again.

Which function are you using to check if the order is executed ? 

Should I use the function ResultDeal ?

If ResultDeal is 0 then does it means the order is not yet executed ?


Regards,

Dorian

 
Dorian Baranes:

Which function are you using to check if the order is executed ? 

I'm using a complex solution. You can see it in the source code of MT4Orders.

If ResultDeal is 0 then does it means the order is not yet executed ?

Yes, that's right.

 
fxsaber:

I'm using a complex solution. You can see it in the source code of MT4Orders.

Yes, that's right.

Ok thanks for your help. As you said your solution seems to be complex for what I need.

I have also checked the code from the EA: https://www.mql5.com/en/code/20497

I do not think this piece of code will work :

//+------------------------------------------------------------------+
//| Возвращает "неопределённое" состояние торгового окружения        |
//+------------------------------------------------------------------+
bool IsUncertainStateEnv(const string symbol_name,const ulong magic_number)
  {
   if(MQLInfoInteger(MQL_TESTER)) return false;
   int total=OrdersTotal();
   for(int i=total-1; i>WRONG_VALUE; i--)
     {
      if(OrderGetTicket(i)==0) continue;
      if(OrderGetInteger(ORDER_TYPE)>ORDER_TYPE_SELL) continue;
      if(OrderGetInteger(ORDER_MAGIC)!=magic_number) continue;
      if(!OrderGetInteger(ORDER_POSITION_ID) && OrderGetString(ORDER_SYMBOL)==symbol_name)
         return true;
     }
   return false;
  }

since after I call PositionOpen with ORDER_TYPE_BUY the function OrderTotal returns 0 since it has not opened a pending order.

 
Dorian Baranes:

after I call PositionOpen with ORDER_TYPE_BUY the function OrderTotal returns 0 since it has not opened a pending order.

ServerName?

 
fxsaber:

ServerName?

ICMarkets-MT5

 

If ResultDeal is 0 then does it means the order is not yet executed ?

Yes, that's right.

After making some test on ResultDeal I notice that it always return 0 after the execution order.

So my question is which function should I use to test if the order has been executed.

Regards,

Dorian

 
Dorian Baranes:

When a trading order is sent to a server, an occasional delay in execution may lead to incorrectly countinging the number of market positions. 

Which function should I use to check if an order (not pending) is excuted on the server side ?

isn't a return code of 10009 a sufficient indication of a successful operation on server side ?
 
Dorian Baranes:

After making some test on ResultDeal I notice that it always return 0 after the execution order.

So my question is which function should I use to test if the order has been executed.

You must wait until Result.deal != 0

  static bool HistoryDealSelect( MqlTradeResult &Result )
  {
    // Заменить HistorySelectByPosition на HistorySelect(PosTime, PosTime)
    if (!Result.deal && Result.order && ::HistorySelectByPosition(::HistoryOrderGetInteger(Result.order, ORDER_POSITION_ID)))
      for (int i = ::HistoryDealsTotal() - 1; i >= 0; i--)
      {
        const ulong DealTicket = ::HistoryDealGetTicket(i);

        if (Result.order == ::HistoryDealGetInteger(DealTicket, DEAL_ORDER))
        {
          Result.deal = DealTicket;

          break;
        }
      }

    return(::HistoryDealSelect(Result.deal));
  }


There are many pitfalls in the platform. This one is the simplest. So I do not use a standard library and I use my own (MQL4-style).

 
Dorian Baranes:

ICMarkets-MT5

It's Live. Try this Demo for Result.deal == 0 - ForexTimeFXTM-Demo01 (or FXOpen-MT5).