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

To add comments, please log in or register
Dorian Baranes
30424
Dorian Baranes  

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

fxsaber
9834
fxsaber  
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());
   }
Dorian Baranes
30424
Dorian Baranes  

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

fxsaber
9834
fxsaber  
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.

Dorian Baranes
30424
Dorian Baranes  
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.

fxsaber
9834
fxsaber  
Dorian Baranes:

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

ServerName?

Dorian Baranes
30424
Dorian Baranes  
fxsaber:

ServerName?

ICMarkets-MT5

Dorian Baranes
30424
Dorian Baranes  

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

fxsaber
9834
fxsaber  
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).

fxsaber
9834
fxsaber  
Dorian Baranes:

ICMarkets-MT5

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

Alain Verleyen
35985
Alain Verleyen  
fxsaber:

You must wait until Result.deal != 0


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).

It may happens there is never a deal.
To add comments, please log in or register