Discussion of article "MQL5 Cookbook: Processing of the TradeTransaction Event" - page 2

 

Here are the log entries of a position closed by a triggered Take Profit. How can this detected in OnTradeTransaction( const MqlTradeTransaction& trans,  const MqlTradeRequest& request,  const MqlTradeResult& result)??

Neither the return code of const MqlTradeResult& result (result.retcode) is set (0 is no option, TRADE_RETCODE_DONE = 10009) nor one of the ids (Deal ticket, Order ticket, Request ID ) in order to find them without looping every time through all deal.

This is the Print at line #376 that print the relevant(?) flags:

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
...// line 376:
   Print(__LINE__," ",counter," prc:",_ToStr2Dig(trans.price)," tckt: ",trans.position //," pTrigg:",_ToStr2Dig(trans.price_trigger)
            ," Tr.Type: ",EnumToString(trans.type)//," DL.Type: ",EnumToString(trans.deal_type)
            ," Tr.OrdType: ",EnumToString(trans.order_type)," Tr.OrdFill: ",EnumToString(trans.order_state)
            ," Tr.tckt:",trans.order," ",trans.position," ",trans.position_by
            ," retCode:",result.retcode," ",result.deal," ",result.order," ",result.deal," ",result.request_id         );
...
  }

 And this is what I can read in the journal:

10:04:40   take profit triggered #2 sell 0.01 EURUSD 1.00319 sl: 1.00816 tp: 1.00171 [#3 buy 0.01 EURUSD at 1.00171]
10:04:40   deal #3 buy 0.01 EURUSD at 1.00171 done (based on order #3)
10:04:40   deal performed [#3 buy 0.01 EURUSD at 1.00171]
10:04:40   order performed buy 0.01 at 1.00171 [#3 buy 0.01 EURUSD at 1.00171]
10:04:40   373  New trade operation dTime1970.01.01 09:03:41
10:04:40   376 0 prc:trans.price=1.00171   tckt: 2 Tr.Type: TRADE_TRANSACTION_DEAL_ADD Tr.OrdType: ORDER_TYPE_BUY Tr.OrdFill: ORDER_STATE_STARTED Tr.tckt:3 2 0 retCode:0 0 0 0 0
10:04:40   489 MqlTradeTransaction: TRADE_TRANSACTION_DEAL_ADD deal #3 DEAL_TYPE_BUY EURUSD 0.01 lot   
10:04:40   376 1 prc:trans.price=1.00171   tckt: 2 Tr.Type: TRADE_TRANSACTION_ORDER_DELETE Tr.OrdType: ORDER_TYPE_BUY Tr.OrdFill: ORDER_STATE_FILLED Tr.tckt:3 2 0 retCode:0 0 0 0 0
10:04:40   383 1 prc:trans.price=1.00171   tckt: 2  res.Ord:0 Tr.Type: TRADE_TRANSACTION_ORDER_DELETE Tr.OrdFill: ORDER_STATE_FILLED Tr.OrdType: ORDER_TYPE_BUY Tr.tckt:3 2 0 Res::0 0 0 0 0
10:04:40   408 PosExists #3 2 mag:0 ORDER_STATE_FILLED ORDER_TYPE_BUY
10:04:40   376 2 prc:trans.price=1.00171   tckt: 2 Tr.Type: TRADE_TRANSACTION_HISTORY_ADD Tr.OrdType: ORDER_TYPE_BUY Tr.OrdFill: ORDER_STATE_FILLED Tr.tckt:3 2 0 retCode:0 0 0 0 0
10:04:40   408 PosExists #3 2 mag:0 ORDER_STATE_FILLED ORDER_TYPE_BUY
2022.09.26 14:08:33.755    disconnected

The terminal writes so beautifully "take profit triggered #2 sell 0.01 EURUSD 1.00319 sl: 1.00816 tp: 1.00171 [#3 buy 0.01 EURUSD at 1.00171]"

So why the hell isn't there a flag like TRADE_TRANSACTION_TRIGGERED_PT and TRADE_TRANSACTION_TRIGGERED_SL??

And is the return code of the request always not set?

 
Part of the confusion is due to the fact that ENUM_ORDER_TYPE 0 = ORDER_TYPE_BUY. So an EA can not distinguish is the value of the field set with ORDER_TYPE_BUY or nullified therefore not set - great!!!
 

Denis, thank you for the article! I read it with interest, but I have not yet fully realised what I have read. I also downloaded your TradeProcessor Expert Advisor and ran it on my terminal, where I currently have an open position. I looked at the printouts and immediately some questions appeared, to which I want to find answers on my own. Well, if I can't do it, I will have to bother you. ))

Regards, Vladimir.

 
MrBrooklin open position. I looked at the printouts and immediately some questions appeared, to which I want to find answers on my own. Well, if I can't do it, I will have to bother you. ))

Regards, Vladimir.

Thank you for your opinion. You are welcome, MrBrooklin! ))

 
Denis Kirichenko #:

Thank you for your opinion. You are welcome, MrBrooklin! ))

Denis, sorry, but without your help my mind "explodes". Here is the result of one of the passes of your EA (by the way, a very cool EA!!!):

2023.10.27 17:11:02.514 TradeProcessor (EURUSDrfd,D1)   Проход : #100
2023.10.27 17:11:02.514 TradeProcessor (EURUSDrfd,D1)   Поступил запрос: изменить параметры отложенного ордера
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   ---===Транзакция===---
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Тикет сделки: 0
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Тип сделки: DEAL_TYPE_BUY
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Тикет ордера: 1030195768
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Состояние ордера: ORDER_STATE_PLACED
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Тип ордера: ORDER_TYPE_SELL_STOP
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Цена: 1.05853
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Уровень Stop Loss: 1.(скрыл значения стоп-лосса)
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Уровень Take Profit: 1.05803
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Цена срабатывания стоп-лимитного ордера: 0.00000
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Торговый инструмент: EURUSDrfd
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Срок истечения отложенного ордера: 2023.10.27 00:00
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Тип ордера по времени действия: ORDER_TIME_DAY
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Тип торговой транзакции: TRADE_TRANSACTION_ORDER_UPDATE
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Тикет позиции: 0
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Объём в лотах: 0.04
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   
2023.10.27 17:11:02.733 TradeProcessor (EURUSDrfd,D1)   Проход : #101

What I don't understand:

  1. my EA placed a pending order SELL_STOP;
  2. Your EA writes - trade type DEAL_TYPE_BY (highlighted in yellow). Just in case, I look in the MQL5 Reference Guide. It says that DEAL_TYPE_BY is:

ENUM_DEAL_TYPE

Identifier

Description

DEAL_TYPE_BUY

Buy


Question - how when modifying a pending order SELL_STOP the transaction type is determined to BUY????? ))

Regards, Vladimir.

 

And there is no transaction. Placing a pending order and its processing does not entail any transaction. Trade ticket = 0, type = 0, where 0 is equivalent to DEAL_TYPE_BUY for the enumeration ENUM_DEAL_TYPE. That is, in the MqlTradeTransaction trans structure , some fields are populated and some are not. The unfilled fields are usually nulled.

More details: in the Documentation about pending orders.

The deal field will be populated for a transaction of this type:

TRADE_TRANSACTION_DEAL_*

The following fields are populated in the MqlTradeTransaction structure for trade transactions related to transaction processing (TRADE_TRANSACTION_DEAL_ADD, TRADE_TRANSACTION_DEAL_UPDATE and TRADE_TRANSACTION_DEAL_DELETE):

  • deal - the ticket of the trade;
  • order - ticket of the order, on the basis of which the deal was made;
  • symbol - name of the financial instrument in the deal;
  • type - type of trade transaction;
  • deal_type - type of the deal;
  • price - price at which the deal was made;
  • price_sl - Stop Loss price (it is filled in if it is specified in the order on the basis of which the deal was made);
  • price_tp - Take Profit price (it is filled in, if it is specified in the order, on the basis of which the deal was made);
  • volume - volume of the deal in lots.
  • position - a ticket of a position opened, changed or closed as a result of a deal execution.
  • position_by - a ticket of a counter position. It is filled in only for trades for closing a position by counter (out by).


Only 3 types of transactions belong to "deal" types: TRADE_TRANSACTION_DEAL_ADD, TRADE_TRANSACTION_DEAL_UPDATE, TRADE_TRANSACTION_DEAL_DELETE.

Документация по MQL5: Константы, перечисления и структуры / Структуры данных / Структура торговой транзакции
Документация по MQL5: Константы, перечисления и структуры / Структуры данных / Структура торговой транзакции
  • www.mql5.com
Структура торговой транзакции - Структуры данных - Константы, перечисления и структуры - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Denis Kirichenko #:

And there is no transaction. Placing a pending order does not entail any transaction. Trade ticket = 0, type = 0, where 0 is equivalent to DEAL_TYPE_BUY for the enumeration ENUM_DEAL_TYPE. That is, in the MqlTradeTransaction trans structure , some fields are populated and some are not. The unfilled fields are usually zeroed.

More details: in the Documentation about pending orders.

Ahhhh, that's it!!! Man, it broke my head! ))

Thank you!!!

Regards, Vladimir.