The great and terrible MT4 forever (or how to strategise a transition) - page 15

 
fxsaber:

No, the last record of trading activity is in the second log.

And this is correct in terms of priority:

2021.05.05 10:32:35.896 Trades  '160122203': market buy 0.01 EURUSD placed for execution
2021.05.05 10:32:35.931 Trades  '160122203': order #2249869869 buy 0.01 / 0.01 EURUSD at market done in 81.962 ms
2021.05.05 10:32:35.931 Trades  '160122203': deal #2240398230 buy 0.01 EURUSD at 1.19876 done (based on order #2249869869)
2021.05.05 10:32:35.931 Trades  '160122203': market sell 0.01 EURUSD, close #2249869868 buy 0.01 EURUSD 1.19876
2021.05.05 10:32:35.978 Trades  '160122203': accepted market sell 0.01 EURUSD, close #2249869868 buy 0.01 EURUSD 1.19876
2021.05.05 10:32:35.978 Trades  '160122203': market sell 0.01 EURUSD, close #2249869868 buy 0.01 EURUSD 1.19876 placed for execution
2021.05.05 10:32:36.009 Trades  '160122203': order #2249869870 sell 0.01 / 0.01 EURUSD at market done in 77.026 ms
2021.05.05 10:32:36.009 Trades  '160122203': deal #2240398231 sell 0.01 EURUSD at 1.19876 done (based on order #2249869870)
2021.05.05 10:32:36.009 Scripts script Test (EURUSD,M1) removed

Here I have reverted to the option without prints and again there is one position left after execution. It turns out that you need to hold some kind of pause after the trades are executed. Man, what a crutch.

 
Ihor Herasko:

Well, who's going to update it? ))

but when there is an open position - the script closes it and does not do anything else

Snapshot2

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

also if i try to open a position the script closes it instantly

 
fxsaber:

managed to write IsSynchronized(). The code is a bit heavy. I haven't decided yet in what form to post it.

How does it work? It is sort of found but sort of absent? What is the gist or principle of the solution?

 
SanAlex:

But when there is an open position, the script closes it and does nothing else.

Not all servers will reproduce the opening of two positions. In your case, it is the third side (one side - two positions, the other - one position).

 
Ihor Herasko:

And that's about right in terms of sequencing:

Forum on trading, automated trading systems and trading strategy testing

The Great and Powerful MT4 Forever (or how to choose migration strategy)

Ihor Herasko, 2021.05.05 09:35

And it's all right here in terms of ordering:

2021.05.05 10:32:35.896 Trades  '160122203': market buy 0.01 EURUSD placed for execution
2021.05.05 10:32:35.931 Trades  '160122203': order #2249869869 buy 0.01 / 0.01 EURUSD at market done in 81.962 ms
2021.05.05 10:32:35.931 Trades  '160122203': deal #2240398230 buy 0.01 EURUSD at 1.19876 done (based on order #2249869869)
2021.05.05 10:32:35.931 Trades  '160122203': market sell 0.01 EURUSD, close #2249869868 buy 0.01 EURUSD 1.19876
2021.05.05 10:32:35.978 Trades  '160122203': accepted market sell 0.01 EURUSD, close #2249869868 buy 0.01 EURUSD 1.19876
2021.05.05 10:32:35.978 Trades  '160122203': market sell 0.01 EURUSD, close #2249869868 buy 0.01 EURUSD 1.19876 placed for execution
2021.05.05 10:32:36.009 Trades  '160122203': order #2249869870 sell 0.01 / 0.01 EURUSD at market done in 77.026 ms
2021.05.05 10:32:36.009 Trades  '160122203': deal #2240398231 sell 0.01 EURUSD at 1.19876 done (based on order #2249869870)
2021.05.05 10:32:36.009 Scripts script Test (EURUSD,M1) removed

The highlighted lines confirm the sequence.

PositionsTotal = 1 - sends an order to close.

  • Then PositionsTotal = 2 and the order from step 1 is closed.
  • The closing takes place after the script execution is completed. Print-cache is a bit distorted.

     
    Dmitry Fedoseev:

    How so? It's sort of found but sort of not there?

    I haven't gotten around to posting it yet. I will post it within 24 hours.

    What is the essence or principle of the solution?

    It analyzes the correspondence of the trading history to the current trading environment. In this case, there are no slows.

    It also checks Result.order.

    As a result, the use is as follows: IsSync() returns false - the trading environment in the Terminal is crooked, otherwise we can proceed as usual.

     
    The MT developers are traditionally silent. The Sheriff is not interested in the problems of the Indians.
     
    Ihor Herasko:

    Added prints. Now two positions remain, as expected:

    Result:

    Add Trade.ResultRetcodeDescription()

    
    #include <Trade\Trade.mqh>
    
    void OnStart()
    {
      CTrade Trade;
      
      while (!IsStopped() && (PositionsTotal() <= 1)) // Закончим, когда появится более одной позиции.
        if (PositionsTotal() == 1)
        {
          Trade.PositionClose(PositionGetTicket(0)); // Если есть позиция - закрываем.
          Print("Закрытие, т. к. PositionsTotal() равно 1");
        }
        else if (!OrdersTotal())
        {
          printf("Перед открытием. PositionsTotal: %d, OrdersTotal: %d", PositionsTotal(), OrdersTotal());
          Trade.Buy(0.01); // Если нет позиции и ордера - открываем позицию.
          Print(Trade.ResultRetcodeDescription());
          printf("После открытия. PositionsTotal: %d, OrdersTotal: %d", PositionsTotal(), OrdersTotal());
        }
        
      Print("Выход. PostionsTotal: ", PositionsTotal());
    }

    Also ResultDeal / ResultOrder / ResultComment and Order.State

    Surely there will be an answer there

    State
    Документация по MQL5: Стандартная библиотека / Торговые классы / CTrade / ResultOrder
    Документация по MQL5: Стандартная библиотека / Торговые классы / CTrade / ResultOrder
    • www.mql5.com
    ResultOrder - CTrade - Торговые классы - Стандартная библиотека - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
     
    Rashid Umarov:

    Add Trade.ResultRetcodeDescription()

    And also ResultDeal / ResultOrder / ResultComment and Order.State

    Surely there will be an answer there

    State

    Done:

    #define  PRINT(A) Print(#A + " = " + (string)(A))
    
    #include <Trade\Trade.mqh>
    
    void OnStart()
    {
      CTrade Trade;
      
      while (!IsStopped() && (PositionsTotal() <= 1)) // Закончим, когда появится более одной позиции.
        if (PositionsTotal() == 1)
        {
          Trade.PositionClose(PositionGetTicket(0)); // Если есть позиция - закрываем.
          Print("Закрытие, т. к. PositionsTotal() равно 1");
        }
        else if (!OrdersTotal())
        {
          printf("Перед открытием. PositionsTotal: %d, OrdersTotal: %d", PositionsTotal(), OrdersTotal());
          Trade.Buy(0.01); // Если нет позиции и ордера - открываем позицию.
          PRINT(Trade.ResultRetcodeDescription());
          PRINT(Trade.ResultDeal());
          PRINT(Trade.ResultOrder());
          PRINT(Trade.ResultComment());
          printf("После открытия. PositionsTotal: %d, OrdersTotal: %d", PositionsTotal(), OrdersTotal());
        }
        
      Print("Выход. PostionsTotal: ", PositionsTotal());
    }

    Result:

    2021.05.05 11:36:14.566 Test (EURUSD,M1)        Перед открытием. PositionsTotal: 0, OrdersTotal: 0
    2021.05.05 11:36:14.646 Test (EURUSD,M1)        Trade.ResultRetcodeDescription() = done at 0.00000
    2021.05.05 11:36:14.646 Test (EURUSD,M1)        Trade.ResultDeal() = 0
    2021.05.05 11:36:14.646 Test (EURUSD,M1)        Trade.ResultOrder() = 2249874645
    2021.05.05 11:36:14.646 Test (EURUSD,M1)        Trade.ResultComment() = Request executed
    2021.05.05 11:36:14.646 Test (EURUSD,M1)        После открытия. PositionsTotal: 2, OrdersTotal: 1
    2021.05.05 11:36:14.646 Test (EURUSD,M1)        Выход. PostionsTotal: 2
    
     
    Rashid Umarov:

    Add Trade.ResultRetcodeDescription()

    And also ResultDeal / ResultOrder / ResultComment and Order.State

    Surely there will be an answer there.

    State

    This is definitely not the case as the condition for sending a market order to open a position is PositionsTotal == OrdersTotal == 0.

    Reason: