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

 
fxsaber:

It's not true!


Result

So what ? Why are you saying "it's not true" ?
 
Alain Verleyen:
So what ? Why are you saying "it's not true" ?

Because the terminal and the trading server are synchronized.

 
fxsaber:

Because the terminal and the trading server are synchronized.

Of course not. You can have a position on the server while the terminal is not yet aware about it.

Forum on trading, automated trading systems and testing trading strategies

How to check that an order is executed after calling PositionOpen

fxsaber, 2018.08.09 20:35

Try this script there ForexTimeFXTM-Demo01 (or FXOpen-MT5)

#include <Trade/Trade.mqh>

void OnStart()
{
  const int PrevTotal = PositionsTotal();
  
  CTrade Trade;  
  
  while (PositionsTotal() == PrevTotal)
    Trade.Buy(1);    
}

Sometimes two new positions will open, not one.

Why ?
 
Alain Verleyen:

Of course not. You can have a position on the server while the terminal is not yet aware about it.

Why ?

You are wrong. The function exposes the order to the trading server. Further third-party programs this order from MT5 are sent outside. The result is sent back to the trading server MT5. Then - to the terminal.


I will not argue. This is the answer of the developers. You can ask them about it yourself. Unfortunately, the history of the servicedesk is no longer available ...

 
fxsaber:

You are wrong. The function exposes the order to the trading server. Further third-party programs this order from MT5 are sent outside. The result is sent back to the trading server MT5. Then - to the terminal.


I will not argue. This is the answer of the developers. You can ask them about it yourself. Unfortunately, the history of the servicedesk is no longer available ...

Please explain why the above code open 2 positions ?
 
Alain Verleyen:
Please explain why the above code open 2 positions ?

The OrderSend exposes the order to the trading server. The trading server tells the terminal that the order is set and returns true.

Orders became one more, but not positions.

 
fxsaber:

The OrderSend exposes the order to the trading server. The trading server tells the terminal that the order is set and returns true.

Orders became one more, but not positions.

Yes because the Terminal is not aware about the Trading server position yet. Not synchronized. It's obvious.

 
Alain Verleyen:

Yes because the Terminal is not aware about the Trading server position yet. Not synchronized. It's obvious.

Forum on trading, automated trading systems and testing trading strategies

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

fxsaber, 2018.08.09 21:27

I will not argue. This is the answer of the developers. You can ask them about it yourself. Unfortunately, the history of the servicedesk is no longer available ...

 
fxsaber:

It's you who are coming here to affirm things which are obviously wrong, without serious argumentation, referring to unavailable reference.

PositionTotal() returns 0, while there is a position in the trading server. So it opens a second position. You reported it yourself.

This is a synchronisation issue, it's how MT5 works and people have to deal with it.

 
#include <Trade/Trade.mqh>

#define PRINT(A) Print(#A + " = " + (string)(A))

string TimeToString( const long Value )
{
  return((string)(datetime)(Value / 1000) + "." + (string)IntegerToString(Value % 1000, 3, '0'));
}

class MyCTrade : public CTrade
{
public:  
  const MqlTradeResult GetResult( void ) const
  {
    return(this.m_result);
  }
};

void OnStart()
{
  const int PrevTotal = PositionsTotal();
  
  MyCTrade Trade;
  
  PRINT(AccountInfoString(ACCOUNT_SERVER));

  Trade.Buy(1);
  PRINT(Trade.GetResult().deal);
  
  Sleep(3000);
    
  if (HistorySelectByPosition(PositionGetTicket(PositionsTotal() - 1)) && HistoryDealsTotal())
  {
    const ulong DealTicket = HistoryDealGetTicket(HistoryDealsTotal() - 1);
    const ulong OrderTicket = HistoryDealGetInteger(DealTicket, DEAL_ORDER);
    
    PRINT(TimeToString(HistoryDealGetInteger(DealTicket, DEAL_TIME_MSC)));
    PRINT(TimeToString(HistoryOrderGetInteger(OrderTicket, ORDER_TIME_DONE_MSC)));
    PRINT(TimeToString(HistoryOrderGetInteger(OrderTicket, ORDER_TIME_SETUP_MSC)));
  }
}


Results

AccountInfoString(ACCOUNT_SERVER) = MetaQuotes-Demo
Trade.GetResult().deal = 268407287
TimeToString(HistoryDealGetInteger(DealTicket,DEAL_TIME_MSC)) = 2018.08.09 23:44:41.109
TimeToString(HistoryOrderGetInteger(OrderTicket,ORDER_TIME_DONE_MSC)) = 2018.08.09 23:44:41.109
TimeToString(HistoryOrderGetInteger(OrderTicket,ORDER_TIME_SETUP_MSC)) = 2018.08.09 23:44:41.109
AccountInfoString(ACCOUNT_SERVER) = FXOpen-MT5
Trade.GetResult().deal = 0
TimeToString(HistoryDealGetInteger(DealTicket,DEAL_TIME_MSC)) = 2018.08.09 23:46:53.873
TimeToString(HistoryOrderGetInteger(OrderTicket,ORDER_TIME_DONE_MSC)) = 2018.08.09 23:46:53.873
TimeToString(HistoryOrderGetInteger(OrderTicket,ORDER_TIME_SETUP_MSC)) = 2018.08.09 23:46:53.857
AccountInfoString(ACCOUNT_SERVER) = ICMarkets-Demo
Trade.GetResult().deal = 4832346
TimeToString(HistoryDealGetInteger(DealTicket,DEAL_TIME_MSC)) = 2018.08.10 01:24:50.411
TimeToString(HistoryOrderGetInteger(OrderTicket,ORDER_TIME_DONE_MSC)) = 2018.08.10 01:24:50.411
TimeToString(HistoryOrderGetInteger(OrderTicket,ORDER_TIME_SETUP_MSC)) = 2018.08.10 01:24:50.153
Reason: