Libraries: MT4Orders - page 10

 
fxsaber #:
Provided all the links with details. Everything works correctly.
I understand the question, but I want to clarify: is it possible to just get a list similar to position history in MT5 without going into details?
 
hini #:
I understand the question, but I want to clarify: is it possible to just get a list similar to position history in MT5, without going into details?
 
fxsaber #:
I have tried this and it is not the same as the historical positions list in MT5. Even though the two closeby's merged, the volume is still short by 0.01 lots.
 
hini #:
volume is still not enough for 0.01 lot.
Provide evidence of the error. I recommend to open a demo account on MT4 and try to make the same trading operations there.
 
fxsaber # :
Provide evidence of the error. I recommend to open a demo account on MT4 and try to make the same trading operations there.
Got it, it is indeed identical with MT4.
 
hini #:
is it possible to just get a list similar to the position history in MT5?
Through snapshots.
#define  MT4ORDERS_BYPASS_MAXTIME 1000000 // Maximum time (in µs) to wait for synchronisation of the trading environment
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

#define  VIRTUAL_SNAPSHOT_REFRESHTIME 1000 // Snapshot lifetime for updating. In MT5 requires connected MT4Orders.mqh
#include <fxsaber\Virtual\Virtual.mqh> // https://www.mql5.com/en/code/22577

#define  Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)
#define  Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK)

void PrintHistory()
{
  for (uint i = OrdersHistoryTotal(); (bool)i--;)
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
      OrderPrint();
}

void OnStart()
{
  if (AccountInfoInteger(ACCOUNT_TRADE_MODE) == ACCOUNT_TRADE_MODE_DEMO)
  {
    const TICKET_TYPE Ticket1 = OrderSend(_Symbol, OP_BUY, 1, Ask, 0, 0, 0);
    const TICKET_TYPE Ticket2 = OrderSend(_Symbol, OP_SELL, 1, Bid, 0, 0, 0);
      
    if (OrderCloseBy(Ticket1, Ticket2)) // OrderCloseBy(Ticket2, Ticket1) is another sequence.
    {
      Print("\nWith CloseBy.");
      PrintHistory(); // The result depends on the sequence of input parameters OrderCloseBy.
      
      Print("\nWithout CloseBy.");
      VIRTUAL::Snapshot(VIRTUAL_SNAPSHOT_REFRESHTIME, -1, false, "", false);    
      PrintHistory(); // Result NOT зависит от последовательности входных параметров OrderCloseBy.
    }
  }  
}


Result.

With CloseBy.
#151231378604 2025.12.16 14:59:36.869 sell 0.00 GBPJPY 207.722 0.000 0.000 2025.12.16 14:59:36.904 207.722 0.00 0.00 0.00 #151304734151  by #151304734153 0
#151231378603 2025.12.16 14:59:36.834 buy 1.00 GBPJPY 207.734 0.000 0.000 2025.12.16 14:59:36.904 207.722 0.00 0.00 -6.59 #151304734151  by #151304734153 0
#151231377009 2025.12.16 14:59:15.351 balance 0.00 0.000 0.000 0.000 2025.12.16 14:59:15.351 0.000 0.00 0.00 100000.00 0

Without CloseBy.
#151231378603 2025.12.16 14:59:36.834 buy 1.00 GBPJPY 207.734 0.000 0.000 2025.12.16 14:59:36.869 207.722 0.00 0.00 -6.59 #151304734151  by #151304734153 0: -12 (-12) - 00:00:00
#151231377009 2025.12.16 14:59:15.351 balance 0.00 0 0 0 2025.12.16 14:59:15.351 0 0.00 0.00 100000.00 0 - 00:00:00
 
fxsaber # :
Through snapshots.
Okay, thank you very much!
 

This seems wrong; the direction is incorrect and the order number doesn't seem to match either.

With CloseBy.
#721743851 2025.12.16 06:12:03.182 buy 0.02 XAUUSDm 4285.524 0.000 0.000 2025.12.16 06:50:55.122 4289.859 0.00 0.00 8.67 0
#721743849 2025.12.16 06:10:25.029 buy 0.03 XAUUSDm 4286.454 0.000 0.000 2025.12.16 06:50:54.942 4289.859 0.00 0.00 10.22 0
#721743835 2025.12.16 06:14:10.761 sell 0.03 BTCUSDm 86202.58 0.00 0.00 2025.12.16 06:50:54.142 86461.98 0.00 0.00 -7.78 0
#721743834 2025.12.16 06:10:41.259 sell 0.03 BTCUSDm 86199.15 0.00 0.00 2025.12.16 06:50:54.123 86461.98 0.00 0.00 -7.89 0
#721743827 2025.12.16 06:11:07.913 buy 0.02 GBPUSDm 1.33627 0.00000 0.00000 2025.12.16 06:50:54.069 1.33626 0.00 0.00 -0.02 0
#721743824 2025.12.16 06:10:55.744 buy 0.04 EURUSDm 1.17503 0.00000 0.00000 2025.12.16 06:50:54.032 1.17511 0.00 0.00 0.32 0
#721743822 2025.12.16 06:13:38.019 sell 0.00 XAUUSDm 4286.352 0.000 0.000 2025.12.16 06:50:53.875 4286.352 0.00 0.00 0.00 #999167005  by #999174176 0
#721743821 2025.12.16 06:10:25.029 buy 0.01 XAUUSDm 4286.454 0.000 0.000 2025.12.16 06:50:53.875 4286.352 0.00 0.00 -0.10 #999167005  by #999174176 0

Without CloseBy.
#721743851 2025.12.16 06:12:03.182 buy 0.02 XAUUSDm 4285.524 0.000 0.000 2025.12.16 06:50:55.122 4289.859 0.00 0.00 8.67 0: +4335 (+4335) - 00:38:52
#721743849 2025.12.16 06:10:25.029 buy 0.03 XAUUSDm 4286.454 0.000 0.000 2025.12.16 06:50:54.942 4289.859 0.00 0.00 10.22 0: +3405 (+3405) - 00:40:29
#721743835 2025.12.16 06:14:10.761 sell 0.03 BTCUSDm 86202.58 0.00 0.00 2025.12.16 06:50:54.142 86461.98 0.00 0.00 -7.78 0: -25940 (-25940) - 00:36:44
#721743834 2025.12.16 06:10:41.259 sell 0.03 BTCUSDm 86199.15 0.00 0.00 2025.12.16 06:50:54.123 86461.98 0.00 0.00 -7.89 0: -26283 (-26283) - 00:40:13
#721743827 2025.12.16 06:11:07.913 buy 0.02 GBPUSDm 1.33627 0.00000 0.00000 2025.12.16 06:50:54.069 1.33626 0.00 0.00 -0.02 0: -1 (-1) - 00:39:47
#721743824 2025.12.16 06:10:55.744 buy 0.04 EURUSDm 1.17503 0.00000 0.00000 2025.12.16 06:50:54.032 1.17511 0.00 0.00 0.32 0: +8 (+8) - 00:39:59
#721743821 2025.12.16 06:10:25.029 buy 0.01 XAUUSDm 4286.454 0.000 0.000 2025.12.16 06:13:38.019 4286.352 0.00 0.00 -0.10 #999167005  by #999174176 0: -102 (-102) - 00:03:13


 

Forum on trading, automated trading systems and testing trading strategies

Libraries: MT4Orders

hini, 2025.12.17 02:14 pm.

That seems incorrect; the direction is incorrect, and the order number doesn't seem to match either.

With CloseBy.
#721743849 2025.12.16 06:10:25.029 buy 0.03 XAUUSDm 4286.454 0.000 0.000 2025.12.16 06:50:54.942 4289.859 0.00 0.00 10.22 0
#721743822 2025.12.16 06:13:38.019 sell 0.00 XAUUSDm 4286.352 0.000 0.000 2025.12.16 06:50:53.875 4286.352 0.00 0.00 0.00 #999167005  by #999174176 0
#721743821 2025.12.16 06:10:25.029 buy 0.01 XAUUSDm 4286.454 0.000 0.000 2025.12.16 06:50:53.875 4286.352 0.00 0.00 -0.10 #999167005  by #999174176 0

Without CloseBy.
#721743849 2025.12.16 06:10:25.029 buy 0.03 XAUUSDm 4286.454 0.000 0.000 2025.12.16 06:50:54.942 4289.859 0.00 0.00 10.22 0: +3405 (+3405) - 00:40:29
#721743821 2025.12.16 06:10:25.029 buy 0.01 XAUUSDm 4286.454 0.000 0.000 2025.12.16 06:13:38.019 4286.352 0.00 0.00 -0.10 #999167005  by #999174176 0: -102 (-102) - 00:03:13

In MT5, there are only two entities in the trade history: trades and orders. There are no positions in the trade history and for many years MT5 did not display closed positions.

MQ created this mode of displaying history by generating position history from trade/order history. That's generation. And unfortunately, in some situations it is wrong. In particular, in the above case.


Let's consider the chronology of trading operations in this situation.

  1. The trade 2025.12.16 06:10:25.029 721657655 BUY 0.04 was executed - the position 999167005 BUY 0.04 was opened.
  2. Deals executed 2025.12.16 06:13:38.019 721662376 SELL 0.01 - opens position 999174176 SELL 0.01.
  3. Deals executed 2025.12.16 06:50:54.942 721743849 SELL 0.03 - position 999167005 BUY 0.03 is closed.

In total, exactly three trades were made. And it fully corresponds to what the script shows in the quote above.


In the trade history, OrderTicket is a ticket of the transaction closing the position. Each position has its own OrderTicket. MT4Orders generates the history of positions unambiguously.

The same POSITION_ID (OrderTicketID) value can be used for different positions that did not even overlap in the time of their existence. MT5 generates position history freely.


Take a look at these lines of MT5 position history generation (red is not mine).

There's a lot there that raises questions. In particular, was there a trade turnover (full circle) of 0.05 lots?


Many different trading situations are dealt with in detail on the blog.

 

I repeated the experiment and it seems that the historical positions generated by MT5 are correct:

Here I manually opened positions: buy-0.04, sell-0.01, buy-0.04, and then closed all with one button.

And the historical positions generated via CloseBy are different from the one I created manually.

With CloseBy.
#726199620 2025.12.17 08:31:37.613 buy 0.04 XAUUSDm 4315.506 0.000 0.000 2025.12.17 08:31:54.047 4315.881 0.00 0.00 1.50 0
#726199619 2025.12.17 08:31:00.023 buy 0.03 XAUUSDm 4315.885 0.000 0.000 2025.12.17 08:31:54.038 4315.881 0.00 0.00 -0.02 0
#726199617 2025.12.17 08:31:04.356 sell 0.00 XAUUSDm 4315.630 0.000 0.000 2025.12.17 08:31:53.880 4315.630 0.00 0.00 0.00 #1004816081  by #1004816270 0
#726199616 2025.12.17 08:31:00.023 buy 0.01 XAUUSDm 4315.885 0.000 0.000 2025.12.17 08:31:53.880 4315.630 0.00 0.00 -0.26 #1004816081  by #1004816270 0

Without CloseBy.
#726199620 2025.12.17 08:31:37.613 buy 0.04 XAUUSDm 4315.506 0.000 0.000 2025.12.17 08:31:54.047 4315.881 0.00 0.00 1.50 0: +375 (+375) - 00:00:17
#726199619 2025.12.17 08:31:00.023 buy 0.03 XAUUSDm 4315.885 0.000 0.000 2025.12.17 08:31:54.038 4315.881 0.00 0.00 -0.02 0: -4 (-4) - 00:00:54
#726199616 2025.12.17 08:31:00.023 buy 0.01 XAUUSDm 4315.885 0.000 0.000 2025.12.17 08:31:04.356 4315.630 0.00 0.00 -0.26 #1004816081  by #1004816270 0: -255 (-255) - 00:00:04

Here are the MT5: historical positions