Issues with OnTradeTransaction

 

CHistoryOrderInfo StopLoss and TakeProfit values are 0.0 at OnTradeTransaction case TRADE_TRANSACTION_HISTORY_ADD when CHistoryOrderInfo TypeDescription = ORDER_TYPE_SELL

I am writing a MT5 Expert Adviser (EA) that Buys long positions and maintains both a trailing SL and a leading TP.

The EA will make adjustments to the SL and TP prices, at various times, while the Position is open.

I wish to record the final SL and TP prices that are in effect after a Position is closed.

The CDealInfo does not contain the SL and TP prices, but the CHistoryOrderInfo  does.

The Position detail is Deleted once the Position is closed.

I find that,  OnTradeTransaction case TRADE_TRANSACTION_HISTORY_ADD when CHistoryOrderInfo TypeDescription = ORDER_TYPE_BUY && StateDescription==ORDER_STATE_FILLED
is a good time to record the Order and Deal information that opened the Position. At that time, the CHistoryOrderInfo has the SL and TP prices.

Likewise, I find that,  OnTradeTransaction case TRADE_TRANSACTION_HISTORY_ADD when CHistoryOrderInfo TypeDescription = ORDER_TYPE_SELL && StateDescription==ORDER_STATE_FILLED
is a good time to record the Order and Deal information when the Position is closed. But at that time, I find that the CHistoryOrderInfo  SL and TP prices are both 0.0, and not the SL and TP prices when the Position was closed.

Is it possible, that OnTradeTransaction case TRADE_TRANSACTION_HISTORY_ADD when CHistoryOrderInfo TypeDescription = ORDER_TYPE_SELL && StateDescription==ORDER_STATE_FILLED
for the CHistoryOrderInfo  to contain the SL and TP prices that were in effect when the Position was closed, instead of 0.0? Is that reasonable for MQL5 to do?

What is the best way, in MQL5, for an EA to capture both the SL and TP prices that were in effect when the Position was closed?


MQL5 never calls OnTradeTransaction with trans.type = TRADE_TRANSACTION_ORDER_ADD even when a new Order is added

In debugging my EA, I noticed that OnTradeTransaction with trans.type = TRADE_TRANSACTION_ORDER_ADD is never called, even when a new Order is added.

I do see OnTradeTransaction with trans.type = TRADE_TRANSACTION_ORDER_DELETE when an Order is deleted, but never TRADE_TRANSACTION_ORDER_ADD .
Why is 
OnTradeTransaction with trans.type = TRADE_TRANSACTION_ORDER_ADD never called?

How can I keep track of open Orders if I do not receive an event when the are added?

What conditions would cause the event OnTradeTransaction with trans.type = TRADE_TRANSACTION_ORDER_ADD?

How can I detect the event TRADE_TRANSACTION_ORDER_ADD when a new Order is added?

 
Don BaechtelIn debugging my EA, I noticed that OnTradeTransaction with trans.type = TRADE_TRANSACTION_ORDER_ADD is never called, even when a new Order is added.

Yes, you will find that in debugging or during back-tests, that "TRADE_TRANSACTION_ORDER_ADD" (and a few others) is often not tracked. However, when in "Live" execution, it is called.

This is probably a bug in the Strategy Tester, but I can't confirm this, and I suspect that MetaQuotes will probably not address it or place it at very low priority.

 
Fernando Carreiro #:

Yes, you will find that in debugging or during back-tests, that "TRADE_TRANSACTION_ORDER_ADD" (and a few others) is often not tracked. However, when in "Live" execution, it is called.

This is probably a bug in the Strategy Tester, but I can't confirm this, and I suspect that MetaQuotes will probably not address it or place it at very low priority.

Thanks.

It is difficult debugging an EA using the Strategy Tester if there is a significant difference in how they perform.
This should be fixed. It is important that MetaQuotes and the Strategy Tester should show the same behavior so that the testing results are reliable and can be trusted.

Can you answer the following questions in reference to debugging using the Strategy Tester:

What conditions would cause the event OnTradeTransaction with trans.type = TRADE_TRANSACTION_ORDER_ADD?

How can I detect the event TRADE_TRANSACTION_ORDER_ADD when a new Order is added?

 
Don Baechtel #:

Thanks.

It is difficult debugging an EA using the Strategy Tester if there is a significant difference in how they perform.
This should be fixed.

Can you answer the following questions in reference to debugging using the Strategy Tester:

What conditions would cause the event OnTradeTransaction with trans.type = TRADE_TRANSACTION_ORDER_ADD?

How can I detect the event TRADE_TRANSACTION_ORDER_ADD when a new Order is added?

I've never had a problem with the standard OrdersTotal() function in a loop in the Tester.

I figure... If OOP fails, try going standard.

 
Ryan L Johnson #:

I've had a problem with the standard OrdersTotal() function in a loop in the Tester.

I figure... If OOP fails, try going standard.

I do not understand your references to OOP and "going standard", can you elaborate on this?

 
Don Baechtel #Can you answer the following questions in reference to debugging using the Strategy Tester:
What conditions would cause the event OnTradeTransaction with trans.type = TRADE_TRANSACTION_ORDER_ADD?
How can I detect the event TRADE_TRANSACTION_ORDER_ADD when a new Order is added?

Unknown! I found that it failed 100% of the times I have tested it in the Strategy Tester, but there may be conditions unknown to me where it will work correctly. Instead, when I need to test such functionality, I do so "live" on a demo account.

However, I seldom use or rely on only the OnTradeTransaction() for tracking trades. I use it only as a secondary measure, and instead track the History, Orders and Positions directly, whenever a OnTrade() event occurs, or when a tick value change indicates possible changes in trade state.
 
Don Baechtel #I do not understand your references to OOP and "going standard", can you elaborate on this?
He meant using the MQL5 trade functions directly, but the user misunderstood your query.
Documentation on MQL5: Trade Functions
Documentation on MQL5: Trade Functions
  • www.mql5.com
This is the group of functions intended for managing trading activities. Before you proceed to study the trade functions of the platform, you must...
 
@Ryan L Johnson #I've never had a problem with the standard OrdersTotal() function in a loop in the Tester. I figure... If OOP fails, try going standard.
You are misunderstanding the query. The user is referring to transaction event handling, not necessarily OOP.
 
Ryan L Johnson #:

I've never had a problem with the standard OrdersTotal() function in a loop in the Tester.

I figure... If OOP fails, try going standard.

In my EA, I am not executing the standard OrdersTotal() function in a loop in the Tester.
I do call OrdersTotal() once, rarely based on some MQL5 event.

How does your statement apply to my issue?

 
Fernando Carreiro #:

Unknown! I found that it failed 100% of the times I have tested it in the Strategy Tester, but there may be conditions unknown to me where it will work correctly. Instead, when I need to test such functionality, I do so "live" on a demo account.

However, I seldom use or rely on only the OnTradeTransaction() for tracking trades. I use it only as a secondary measure, and instead track the History, Orders and Positions directly, whenever a OnTrade() event occurs, or when a tick value change indicates possible changes in trade state.
As an update, I only use Market orders, so I don't know how OnTradeTransaction() handles Pending orders in the Strategy Tester.
 
Don Baechtel #:

In my EA, I am not executing the standard OrdersTotal() function in a loop in the Tester.
I do call OrdersTotal() once, rarely based on some MQL5 event.

How does your statement apply to my issue?

Disregard my post. I was implying that I would replace the CHistoryOrderInfo code block with OrdersTotal() in a loop.

Fernando Carreiro has deemed that is outside of the scope of this thread, so my posts should be deleted.

In any event, good luck!