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

 
secret:
The order has been placed beforehand. The price touches it, but it is executed later, at a worse price.

No, if the limit has performed worse, then screw the broker or deal with it.

And if the stop, that's fine, as it were. 20-30 ms is quite acceptable, if the order is output somewhere else and not just checked by a plugin.

And how many ticks were there - it does not matter at all.

 
Andrei Trukhanovich:

how does it help if the commission is counted in dollars from the dollar volume?

Strangely enough, the currency of the account is affected. Apparently, it's not the current exchange rate, but a fixed rate. Plus rounding, if the lot is small.

 

I had to figure something out for myself and a very easy to reproduce example was born, which shows how easy it is to work with orders.


So, let us imagine that we run an EA that runs in the classic OnTick mode. Since OnTick may occur at any time during the state of the terminal's environment, it will be interesting to see what the EA may see. Therefore, the states that can be encountered by the Expert Advisor onTick are picked out as follows.

#define  TOSTRING(A) #A + " = " + (string)(A) + " "

void OnTradeTransaction( const MqlTradeTransaction&, const MqlTradeRequest&, const MqlTradeResult& )
{
  static int i = 0;
  
  Print(i++);
  
  if (HistorySelect(0, INT_MAX))
    Print(TOSTRING(PositionsTotal()) + TOSTRING(OrdersTotal()) + TOSTRING(HistoryDealsTotal()) + TOSTRING(HistoryOrdersTotal()));
        
  Print("----------------");
}

This code simply prints out how many current positions/orders and how many closed deals/orders there are.


We run it and just manually open a position and close it. There is nothing else. I have the following printout (it may change from run to run).

        // Открываем позицию руками.

        0
        PositionsTotal() = 0 OrdersTotal() = 1 HistoryDealsTotal() = 25 HistoryOrdersTotal() = 25 
        ----------------
        1
        PositionsTotal() = 0 OrdersTotal() = 1 HistoryDealsTotal() = 25 HistoryOrdersTotal() = 25 
        ----------------
        2
        PositionsTotal() = 0 OrdersTotal() = 1 HistoryDealsTotal() = 25 HistoryOrdersTotal() = 25 
        ----------------
        3
        PositionsTotal() = 0 OrdersTotal() = 1 HistoryDealsTotal() = 25 HistoryOrdersTotal() = 25 
        ----------------
        4
        PositionsTotal() = 0 OrdersTotal() = 1 HistoryDealsTotal() = 25 HistoryOrdersTotal() = 25 
        ----------------
        5 // Открывающий позицию ордер исчез полностью - нет среди живых и мертвых.
        PositionsTotal() = 0 OrdersTotal() = 0 HistoryDealsTotal() = 25 HistoryOrdersTotal() = 25 
        ----------------
        6 // Открылась позиция, но нет соответствующей ей сделки.
        PositionsTotal() = 1 OrdersTotal() = 0 HistoryDealsTotal() = 25 HistoryOrdersTotal() = 26 
        ----------------
        7
        PositionsTotal() = 1 OrdersTotal() = 0 HistoryDealsTotal() = 26 HistoryOrdersTotal() = 26 
        ----------------

        // Закрываем позицию руками.
        8 // Позиция и закрывающий ее ордер.
        PositionsTotal() = 1 OrdersTotal() = 1 HistoryDealsTotal() = 26 HistoryOrdersTotal() = 26 
        ----------------
        9
        PositionsTotal() = 1 OrdersTotal() = 1 HistoryDealsTotal() = 26 HistoryOrdersTotal() = 26 
        ----------------
        10
        PositionsTotal() = 1 OrdersTotal() = 1 HistoryDealsTotal() = 26 HistoryOrdersTotal() = 26 
        ----------------
        11
        PositionsTotal() = 1 OrdersTotal() = 1 HistoryDealsTotal() = 26 HistoryOrdersTotal() = 26 
        ----------------
        12 // Позиция закрыта, но закрывающий ее ордер продолжает висеть.
        PositionsTotal() = 0 OrdersTotal() = 1 HistoryDealsTotal() = 27 HistoryOrdersTotal() = 26 
        ----------------
        13 // Закрывающий позицию ордер полностью исчез - нет среди мертвых/живых.
        PositionsTotal() = 0 OrdersTotal() = 0 HistoryDealsTotal() = 27 HistoryOrdersTotal() = 26 
        ----------------
        14
        PositionsTotal() = 0 OrdersTotal() = 0 HistoryDealsTotal() = 27 HistoryOrdersTotal() = 26 
        ----------------
        15
        PositionsTotal() = 0 OrdersTotal() = 0 HistoryDealsTotal() = 27 HistoryOrdersTotal() = 27 
        ----------------

So, the OnTick Expert Advisor can encounter any of these 16 situations, some interesting ones I commented above.

I didn't pick up an example on purpose. Just ran it and posted the result. Launches can give other interesting variants.


I was able to comment on the results because I knew exactly what I was doing (I opened one single position and closed it by hand).

How does it feel for an Expert Advisor that does not know that? Let's just imagine that there are several Expert Advisors working in parallel on the account. The ease of analysis of the resulting mess from the symbiosis of situations above is, I think, obvious.


A concise code is attached, anyone can reproduce it.

 
fxsaber:

I had to figure something out for myself and a very easy to reproduce example was born, which shows how easy it is to work with orders.


So, let us assume that we run an EA that runs in the classic OnTick mode. Since OnTick may occur at any time during the state of the terminal's environment, it will be interesting to see what the EA may see. Therefore, the states that can be encountered by the Expert Advisor onTick, I picked out in the following way.

This code simply prints out how many current positions/orders and how many closed deals/orders there are.


We run it and just manually open a position and close it. There is nothing else. I have the following printout (it may change from run to run).

So, the OnTick Expert Advisor can encounter any of these 16 situations, some interesting ones I commented above.

I didn't pick up an example on purpose. Just ran it and posted the result. Launches can give other interesting variants as well.


I was able to comment on the results because I knew exactly what I was doing (I opened one single position and closed it by hand).

How does it feel for an Expert Advisor that does not know that? Let's just imagine that there are several Expert Advisors working in parallel on the account. The ease of analysis of the resulting mess from the symbiosis of situations above is, I think, obvious.


A concise code is attached, everyone can reproduce it.

You have to understand how many times OnTradeTransaction is called and what you can get at what moment.

TRADE_TRANSACTION_ORDER_ADD

Addition of a new open order.

TRADE_TRANSACTION_ORDER_UPDATE

Modification of an open order. These changes include not only explicit changes on the client terminal or trade server side but also changes of the order placing state (e.g. from ORDER_STATE_STARTED to ORDER_STATE_PLACED or from ORDER_STATE_PLACED to ORDER_STATE_PARTIAL etc.).

TRADE_TRANSACTION_ORDER_DELETE

Deletes the order from the list of open orders. An order can be deleted from the list of open orders as a result of a trade request or as a result of execution (fill) and transferring to history.

TRADE_TRANSACTION_DEAL_ADD

Adding a trade to the history. This is performed as a result of the order execution or as a result of the account balance operations.

TRADE_TRANSACTION_DEAL_UPDATE

Changing of a trade in the history. It is possible situations when a previously executed deal is changed on the server. For example, the transaction was changed in the external trading system (exchange), where it was removed by the broker.

TRADE_TRANSACTION_DEAL_DELETE

Deletion of the transaction from the history. There may be situations when a previously executed trade is deleted on the server. For example, the trade was deleted in the external trading system (exchange) where it was removed by the broker.

TRADE_TRANSACTION_HISTORY_ADD

Adding an order to history as a result of execution or cancellation.

TRADE_TRANSACTION_HISTORY_UPDATE

Modification of an order placed in the order history. This type is intended for increasing the functionality on the trade server's side.

TRADE_TRANSACTION_HISTORY_DELETE

Deletion of an order from the order history. This type is intended for functionality expansion on the trade server's side.

TRADE_TRANSACTION_POSITION

Changing of a position not related to trade execution. This transaction type indicates that the position has been changed on the trade server's side. The position's volume, opening price, and Stop Loss and Take Profit levels may be changed. Information about changes is passed to MqlTradeTransaction structure through OnTradeTransaction handler. A position change (addition, modification or elimination) as a result of a trade does not cause occurrence of the TRADE_TRANSACTION_POSITION transaction.

TRADE_TRANSACTION_REQUEST

A notification that a trade request has been processed by the server and its result has been received. For transactions of this type it is necessary to analyze only one field - type (transaction type) in MqlTradeTransaction structure. The second and third parameters of OnTradeTransaction function (request and result) must be analyzed to get additional information.

 
Alexey Viktorov:

You have to figure out how many times OnTradeTransaction is called and what you can get at what point.

I think I've made it as clear as possible.

Forum on trading, automated trading systems and trading strategy testing

The great and terrible MT4 forever (or how to build a migration strategy)

fxsaber, 2021.05.03 12:48

So, OnTick EA may run into any of these 16 situations, interesting ones I commented above.

 
fxsaber:
I think I've made it as clear as possible.

What does OnTick() have to do with it if you handle it all in OnTradeTransaction

Get the reason for calling OnTradeTransaction and process what is available in this call accordingly.
 
Alexey Viktorov:

What does OnTick() have to do with it if you process it all in OnTradeTransaction

Get the reason for calling OnTradeTransaction and process what is available at this call accordingly.

Perhaps one of the forum members will explain. It doesn't work for me.

 
Alexey Viktorov:

What does OnTick() have to do with it if you process it all in OnTradeTransaction

Get the reason for calling OnTradeTransaction and handle what's available at that call accordingly.

InOnTick, absolutely all these oddities are in place.It's justdifficult to make an easily reproducible example with OnTick.OnTradeTransaction was chosen as an examplebut it doesn't change the essence.

 
traveller00:

OnTick has absolutely all of these oddities in place.It's justdifficult to make an easily reproducible example withOnTick.OnTradeTransaction was chosen as an example, but it doesn't change the point.

Keep on imitating Don Quixote...

 
fxsaber:

нет среди живых и мертвых

It would be great to get around this point at MT4Orders level.

Nowadays, order reversals happen, unfortunately. Most likely because of that.

Reason: