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

To add comments, please log in or register
fxsaber
11123
fxsaber  
Alain Verleyen:
It may happens there is never a deal.

It's not about reject.

Alain Verleyen
37324
Alain Verleyen  
fxsaber:

It's not about reject.

It may happens there is never a deal, even when the order was accepted.
fxsaber
11123
fxsaber  
Alain Verleyen:
It may happens there is never a deal, even when the order was accepted.

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.

Alain Verleyen
37324
Alain Verleyen  
fxsaber:

I know that since 2014.

Forum on trading, automated trading systems and testing trading strategies

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

fxsaber, 2018.08.09 19:44

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

I am saying, third time, that even with a previously accepted order, it may happen that there is never a deal.
Code2219 or probably 2319
441
Code2219 or probably 2319  
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 ?
fxsaber
11123
fxsaber  

Forum on trading, automated trading systems and testing trading strategies

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

fxsaber, 2018.08.09 19:44

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

Anthony Garot
2143
Anthony Garot  
Dorian Baranes:

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

I monitor in the OnTradeTransaction() event. This is similar to @fxsaber 's solution.

void OnTradeTransaction(
    const MqlTradeTransaction&       trans,            // trade transaction structure
    const MqlTradeRequest&           request,          // request structure
    const MqlTradeResult&            result            // result structure
    )
{

    if ( trans.deal > 0 )
    {
        HistoryDealSelect(trans.deal);

        // IN
        // This tells when an order is placed.
        if ( (ENUM_DEAL_ENTRY)HistoryDealGetInteger(trans.deal,DEAL_ENTRY) == DEAL_ENTRY_IN )
        {
            const ENUM_DEAL_PROPERTY_INTEGER dealTicket=(ENUM_DEAL_PROPERTY_INTEGER) HistoryDealGetInteger(trans.deal, DEAL_TICKET);
            const ENUM_DEAL_PROPERTY_INTEGER dealOrder=(ENUM_DEAL_PROPERTY_INTEGER) HistoryDealGetInteger(trans.deal, DEAL_ORDER);
            const ENUM_DEAL_TYPE dealType=(ENUM_DEAL_TYPE) HistoryDealGetInteger(trans.deal, DEAL_TYPE);
            Print(StringFormat("IN - Trade opened. Deal [%d] Order [%d] Type [%s]", dealTicket, dealOrder, EnumToString(dealType)));
        }
    }
}
fxsaber
11123
fxsaber  
Anthony Garot:

I monitor in the OnTradeTransaction() event.

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.

Alain Verleyen
37324
Alain Verleyen  
Code2219 or probably 2319:
isn't a return code of 10009 a sufficient indication of a successful operation on server side ?
The problem is the synchronization between Server side and Client (Terminal) side.
fxsaber
11123
fxsaber  
Alain Verleyen:
The problem is the synchronization between Server side and Client (Terminal) side.

It's not true!

#include <Trade/Trade.mqh>

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

void OnStart()
{
  const int PrevTotal = PositionsTotal();
  
  CTrade Trade;  
  
  while (PositionsTotal() == PrevTotal)
  {
    PRINT(OrdersTotal());
    PRINT(PositionsTotal());
    
    for (int i = OrdersTotal() - 1; i >= 0; i--)
      if (OrderGetTicket(i))
        PRINT(EnumToString((ENUM_ORDER_STATE)OrderGetInteger(ORDER_STATE)));
    
    Trade.Buy(1);
  }
}


Result

2018.08.09 22:16:27.075 OrdersTotal() = 0
2018.08.09 22:16:27.075 PositionsTotal() = 0
2018.08.09 22:16:27.111 OrdersTotal() = 1
2018.08.09 22:16:27.111 PositionsTotal() = 0
2018.08.09 22:16:27.111 EnumToString((ENUM_ORDER_STATE)OrderGetInteger(ORDER_STATE)) = ORDER_STATE_PLACED
To add comments, please log in or register