"Floating PositionSelect() error - page 4

 
prostotrader:

Once again, for "candidates", "doctors", "professors" and "academics".

Any event coming to OnTradeTransaction() may be lost

That's why I think it is a mistake that after receiving meaningful eventTRADE_TRANSACTION_HISTORY_ADD

the data in the terminal will not be updated!

In Help(https://www.mql5.com/ru/docs/basis/function/events#ontradetransaction):

One trade request sent manually from the terminal or viaOrderSend()/OrderSendAsync()functions can generate several consecutive trade transactions on the trade server. The order of these transactions arrival into the terminal is not guaranteed, so we cannot build our trading algorithm on waiting for the arrival of some trade transactions after others. Besides, transactions may be lost when being delivered from the server to the terminal.

Therefore, it is not clear why this function is needed at all. You just should not use it, that's all. You have to analyse the history of orders and deals yourself.

 
Dmitry Fedoseev:

In Help(https://www.mql5.com/ru/docs/basis/function/events#ontradetransaction):

It is therefore not clear why this function is needed at all. You just shouldn't use it, that's all. You have to analyse the history of orders and trades yourself.

Thanks. Also, you don't have to use all the other functions either :) (no offense)
 

As for orders, positions and transactions.

As written in the help - the sequence of transactions is not guaranteed - a transaction of order transfer to history can be received earlier than a transaction of a deal.
Changing a position in the terminal is strictly a result of receiving a trade transaction, so its receipt is a guarantee that the position has changed.
Obtaining a transaction to transfer an order to the history ensures only that the order has ceased to be active (open) and has been moved into the order history.


Кроме того, транзакции могут потеряться при доставке от сервера к терминалу.

As for this phrase.

It seems to have remained in the documentation of one of the initial versions of the terminal with asynchronous trading. It will be removed in the near future. The culprits will be executed.

 
MQ Alexander:

As for orders, positions and transactions.

As written in the help - the sequence of transactions is not guaranteed - a transaction of order transfer to history can be received earlier than a transaction of a deal.
Changing a position in the terminal is strictly a result of receiving a trade transaction, so its receipt is a guarantee that the position has changed.
Obtaining a transaction to transfer an order to the history ensures only that the order has ceased to be active (open) and has been moved into the order history.


As for this phrase.

It seems to have remained in the documentation of one of the initial versions of the terminal with asynchronous trading. It will be removed in the near future. The guilty ones will be executed.

Thank you very much!

And me a machine gun (or at least a rifle), to participate in the shooting? :)

 

MQ Alexander!

I would like a more extended explanation of the trades and positions.

Because YOU wrote:

Изменение позиции в терминале происходит строго в результате получения сделочной транзакции, соответственно её получение является гарантией того что позиция поменялась.

And in fact it turns out the following:

Test Expert Advisor build 1375 demo opening (was run 2 times)

//+------------------------------------------------------------------+
//|                                              Test_deff_order.mq5 |
//|                                                   Copyright 2016 |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016"
#property link      "https://www.mql5.com"
#property version   "1.00"
//
uint  order_req_id;
ulong order_ticket;
ulong magic=987142563;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   OrderPlace();
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
    switch( trans.type )
    {
      case TRADE_TRANSACTION_REQUEST:
        if((order_req_id > 0) && (order_req_id==result.request_id))
        {
          if(result.retcode==TRADE_RETCODE_PLACED) order_ticket=result.order;
        }
      break;
      case TRADE_TRANSACTION_DEAL_ADD:
        if((order_ticket!=0) && (trans.order==order_ticket))
        {
         Print("Deal done. Ticket: ",trans.order);
        }
      break;
      case TRADE_TRANSACTION_HISTORY_ADD:
        if((order_ticket!=0) && (trans.order==order_ticket))
        {
         Print("History done. Ticket: ",order_ticket);
         if(HistoryOrderSelect(order_ticket))
         {
           double vol_cur = HistoryOrderGetDouble(order_ticket,ORDER_VOLUME_CURRENT);
           double vol_init = HistoryOrderGetDouble(order_ticket,ORDER_VOLUME_INITIAL);
           datetime time_setup=datetime(HistoryOrderGetInteger(order_ticket,ORDER_TIME_SETUP));
           double deals_vol = GetDealsVolume(Symbol(),order_ticket,time_setup);
           Print("Volume initial: ",vol_init," Volume current: ",vol_cur," Deals done: ",deals_vol);
           if(PositionSelect(Symbol()))
           {
            Print("Position exists.");
           }
           else
           {
            Print("Position NOT exists.");
           }
         }
        }
      break;
    }
  }
///----
double GetDealsVolume(const string a_symbol,const ulong a_ticket,const datetime start)
  {
   double volume=0;
   if(HistorySelect(start-180,TimeTradeServer()+180))
     {
      int deals=HistoryDealsTotal();
      if(deals>0)
        {
         for(int i=deals-1; i>=0; i--)
           {
            ulong deal_ticket=HistoryDealGetTicket(i);
            ulong ticket=ulong(HistoryDealGetInteger(deal_ticket,DEAL_ORDER));
            if(( ticket>0) && (ticket==a_ticket))
              {
               volume+=HistoryDealGetDouble(deal_ticket,DEAL_VOLUME);
              }
           }
        }
     }
   return( volume );
  } 
//---
//+------------------------------------------------------------------+
//| Place order                                                      |
//+------------------------------------------------------------------+
void OrderPlace()
{
  ResetLastError();
  MqlTradeRequest request={0};
  MqlTradeResult  result={0};
  order_ticket=0;
  order_req_id=0;
    
//--- Fill structure
  request.action=TRADE_ACTION_DEAL;//PENDING;
  request.magic=magic;
  request.symbol=Symbol();
  request.volume=1;
  double step=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE); 
  request.price=0;//SymbolInfoDouble(Symbol(), SYMBOL_ASK);//-step;
  request.type = ORDER_TYPE_BUY;//BUY_LIMIT;
  request.comment = "";      
  request.type_filling = ORDER_FILLING_IOC; ///
  request.type_time = ORDER_TIME_DAY;
  if(OrderSendAsync(request, result))
  {
    if(result.retcode==TRADE_RETCODE_PLACED) 
    {
      order_req_id=result.request_id;
    }
    else
    {
      Print( __FUNCTION__, ": Error! Retcode: ",GetLastError());
    }
  }
  else
  {
    Print( __FUNCTION__, ": Order not sent!");
  }
}   
//+------------------------------------------------------------------+

And here, the results:

Both times TRADE_TRANSACTION_HISTORY_ADD came first, but

in the first case there was no position but in the second case it was!

How should I understand it?

I forgot to attach the terminal log:

2016.08.15 15:37:14.935 Experts expert Test_deff_order (GAZR-9.16,M1) loaded successfully
2016.08.15 15:37:17.694 Trades  '1007932': exchange buy 1.00 GAZR-9.16 at market
2016.08.15 15:37:17.702 Trades  '1007932': exchange buy 1.00 GAZR-9.16 at market placed for execution in 8 ms
2016.08.15 15:37:17.721 Trades  '1007932': deal #7487011 buy 1.00 GAZR-9.16 at 13897 done (based on order #50942179)
2016.08.15 15:37:46.456 Trades  '1007932': exchange sell 1.00 GAZR-9.16 at market, close #50942179 buy 1.00 GAZR-9.16 13897
2016.08.15 15:37:46.463 Trades  '1007932': exchange sell 1.00 GAZR-9.16 at market, close #50942179 buy 1.00 GAZR-9.16 13897 placed for execution in 7 ms
2016.08.15 15:37:46.497 Trades  '1007932': deal #7487015 sell 1.00 GAZR-9.16 at 13892 done (based on order #50942187)
2016.08.15 15:37:50.348 Experts expert Test_deff_order (GAZR-9.16,M1) removed
2016.08.15 15:37:51.845 Experts expert Test_deff_order (GAZR-9.16,M1) loaded successfully
2016.08.15 15:37:53.776 Trades  '1007932': exchange buy 1.00 GAZR-9.16 at market
2016.08.15 15:37:53.786 Trades  '1007932': exchange buy 1.00 GAZR-9.16 at market placed for execution in 9 ms
2016.08.15 15:37:53.807 Trades  '1007932': deal #7487017 buy 1.00 GAZR-9.16 at 13898 done (based on order #50942195)
2016.08.15 15:37:58.632 Trades  '1007932': exchange sell 1.00 GAZR-9.16 at market, close #50942195 buy 1.00 GAZR-9.16 13898
2016.08.15 15:37:58.639 Trades  '1007932': exchange sell 1.00 GAZR-9.16 at market, close #50942195 buy 1.00 GAZR-9.16 13898 placed for execution in 7 ms
2016.08.15 15:37:58.664 Trades  '1007932': deal #7487020 sell 1.00 GAZR-9.16 at 13892 done (based on order #50942197)
2016.08.15 15:41:52.483 Experts expert Test_deff_order (GAZR-9.16,M1) removed
 

prostotrader, let me ask you an intimate question. Why are you so fond of historical orders and not fond of trades? :-))

MQ Alexander said:

Что касается ордеров, позиций и сделок.

As written in the Help - the sequence of incoming transactions is not guaranteed - the transaction of order transfer to history can be received before the transaction.
Changing a position in the terminal is strictly a result of receiving a trade transaction, therefore its receipt is a guarantee that the position has changed.
Obtaining a transaction to transfer the order into the history only guarantees that the order has ceased to be active (open) and has been transferred into the order history.

And in your case

case TRADE_TRANSACTION_DEAL_ADD


almost empty...

And another lyrical remark. In the concept of MT5 the presence of a history order reflects the fact that there was an attempt to make a trade. And it's not the fact that it was executed. And in order to find out we need to refer to the trade. It already reflects the result of the trade operation. Then it is more logical to deal with transactions in your code...

 
Dennis Kirichenko:

prostotrader, let me ask you an intimate question. Why are you so fond of historical orders and not fond of trades? :-))

MQ Alexander said:

And in your case


is almost empty...

I am answering to your intimate question.

You probably work on the FOREX market and deal with a single order,

So you persistently "lie" in an issue you do not even understand.

Imagine there are two steering wheels in a car and two drivers who both look only at the

the road. How would the 1st driver know that the other driver is steering (let's say to the right)?

Also look carefully:

All your primitive logic is based on the execution of a market order, so,

I'll bring it to your attention that there are limit and pending orders

which may not be fully executed but may be executed in portions.

 
prostotrader:

Also, look carefully:

And what should I see there?!

Do you realise that you accidentally get position information in case TRADE_TRANSACTION_HISTORY_ADD?

And you have already been told that...
 

prostotrader:

All your primitive logic is based on the execution of a market order, so,

I would like to bring to your attention that there are limit and pending orders

which may not be executed in full but in parts.

You do not judge my primitive logic, child...
 
Dennis Kirichenko:
You're not the one to judge my primitive logic...

And who is to judge?

I judge your abilities by your statements!

The picture is clearly not pretty :(

Is that clearer, "Professor"?

And I don't recallMQ Alexander delegating you to answer for him.

Reason: