Issues with OnTradeTransaction - page 2

 
The TRADE_TRANSACTION_ORDER_ADD is generated when the order is sent to the trading server (it is not sent to trading server in tester, so probably is why this event is not fired). At this point the order state is usually ORDER_STATE_STARTED. It does not mean the order has been accepted and/or executed. You can know that an order has been placed by the event TRADE_TRANSACTION_ORDER_UPDATE, where if the order was placed will change the order status to ORDER_STATE_PLACED.

Regarding SL and TP, you track it by TRADE_TRANSACTION_DEAL_ADD and the DEAL_REASON, depending on the DEAL_REASON the deal price may be SL or TP, if the reason as SL or TP.
 
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.

Thanks for the response.

Fernando Carreiro #:
As an update, I only use Market orders, so I don't know how OnTradeTransaction() handles Pending orders in the Strategy Tester.

It is difficult to develop and debug an EA when you design the EA according to the MQL5 documentation and the Strategy Tester doesn't function that way.

It makes it very difficult.

 
Samuel Manoel De Souza #:
The TRADE_TRANSACTION_ORDER_ADD is generated when the order is sent to the trading server (it is not sent to trading server in tester, so probably is why this event is not fired). At this point the order state is usually ORDER_STATE_STARTED. It does not mean the order has been accepted and/or executed. You can know that an order has been placed by the event TRADE_TRANSACTION_ORDER_UPDATE, where if the order was placed will change the order status to ORDER_STATE_PLACED.

Regarding SL and TP, you track it by TRADE_TRANSACTION_DEAL_ADD and the DEAL_REASON, depending on the DEAL_REASON the deal price may be SL or TP, if the reason as SL or TP.

Thanks.

Samuel Manoel De Souza #:
ORDER_STATE_PLACED

Your suggestion didn't seem to work either, using the Strategy Tester.

What I see in the debugger:

  • I requested a Market Buy order.
  • The Order was accepted and placed.
  • The Deal was added and TRADE_TRANSACTION_DEAL_ADD was triggered.
  • The Order was later deleted and TRADE_TRANSACTION_ORDER_DELETE was triggered.
  • Neither TRADE_TRANSACTION_ORDER_ADD nor TRADE_TRANSACTION_ORDER_UPDATE were ever called in the process before the Order was deleted.
  • There doesn't seem to be an event that occurs (in the Strategy Tester) when the Order is added or when ORDER_STATE_STARTED happens.
In my EA, I am trying to determine when my Market Buy Order begins processing, which is the start of the entire trading process.
In that way, I can determine when the Buy Order has been accepted and begins working it way through the other stages of trading.

Also, my EA cannot request any additional orders, from the time the Order is first requested, until the time it is closed and all the associated Deals are completed.

A major goal of the Strategy Tester support should be to get the Strategy Tester to behave exactly like the MetaQuotes terminal does and the documentation say it should so that it can function as an effective debug platform for project development. Anything less, IMHO, is less than acceptable.

Any misbehavior in the Strategy Tester should be handled as a Bug.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be...
 
Don Baechtel:

The CDealInfo does not contain the SL and TP prices

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?

 
fxsaber #:

Thanks for that!

In my MetaTrader 5 Version 5.00 Build 5260 5 September 2025:

CDealInfo::TakeProfit in not defined and dealInfo.TakeProfit(); generates compile error undeclared identifier.

The same is true for CDealInfo::StopLoss.

How can I get an updated version of DealInfo.mqh and the other Includes for MT5 Build 5260?

 
Don Baechtel #:

Thanks for that.

In my MetaTrader 5 Version 5.00 Build 5260 5 September 2025:

CDealInfo::TakeProfit in not defined and dealInfo.TakeProfit(); generates compile error undeclared identifier.

The same is true for CDealInfo::StopLoss.

How can I get an updated version of DealInfo.mqh and the other Includes for MT5 Build 5260?

You did not read @fxsaber's post that he generously referenced, in which he describes that the class methods are missing, so he proposed the code to be added.

So, that is what you have to do—to copy his proposed code and add it yourself to the CDealInfo class implementations.

When people answer you and offer help, please be so kind as to properly follow up on it.

 
Fernando Carreiro # :

You did not read  @fxsaber 's post that he generously referenced, in which he describes that the class methods are missing, so he proposed the code to be added.

So, that is what you have to do—to copy his proposed code and add it yourself to the CDealInfo class implementations.

When people answer you and offer help, please be so kind as to properly follow up on it.

I did not see and proposal in  fxsaber 's post. I did not see any reference to a proposal in the post.

@ fxsaber  seemed to say, in the post, that the improvements shown were already made in  MetaTrader 5 Version 5.00 Build 5260 dated 2025.09.07 09:30.

I checked my MetaTrader 5 Version 5.00 Build 5260 dated 5 September 2025, and those improvements have not yet been made.
I was asking if I can get an update of the standard libraries  with those improvements from somewhere.

I did see the code that should be added to the CDealInfo class,
but it is dangerous for me to add those changes to my copies of the CDealInfo class
because how do I know if in some future MT5 update those changes will not be included and cause my application to fail? I cannot risk that.

The MQL5 development team should make those improvements to the MQL5 Libraries for all to benefit.

Fernandom, please tone down the nastiness and ramp up the help and assistance a little. I would appreciate it. Thanks.

 
Don Baechtel #I did see the code that should be added to the CDealInfo class, but it is dangerous for me to add those changes to my copies of the CDealInfo class because how do I know if in some future MT5 update those changes will not be included and cause my application to fail? I cannot risk that.

Using the Standard Library has always been "dangerous" since its conception for the very reason you describe. It has always been somewhat "buggy" and "incomplete", and has suffered updates on occasion causing havoc for existing programs dependant on it. However, that also happens to the MQL5 functions and the language itself too, but the impact is different.

So, here are some solutions that users have implemented to prevent the issue with the Standard Library (some good, some less):

  1. The most common solution is not to use Standard Library, nor even Object Oriented Programming (OOP) for the most part, and only use functional/procedural programming, using the MQL5 functions directly.
  2. Another solution, is to programme one's own OOP libraries over which one has full control, and that can be built over time based on one's needs.
  3. Yet another, is to create a clone/copy of the entire Standard Library in another folder, so as not to suffer any future changes, and to make one's own modifications and additions without worries. And when the original does undergo changes, one can just manually integrate the modifications if needed.
  4. And yet another, is to create derived classes based off the Standard Library when needed, and then making changes and additions on the derived classes. 

Personally, I consider option 4 more of a temporary stop-gap, but it is still a valid solution. For myself, I use a combination of options 1 and 2.

Which solution will you adopt?

EDIT: If you ever plan to code for others, or you simply want to make your programmes self-contained, then I recommend option 1 (with or without some local OOP code). This way, the programmes have no dependencies—single source code file for each program with no need for "#includes".


Don Baechtel #Fernando, please tone down the nastiness and ramp up the help and assistance a little. I would appreciate it. Thanks.

Yes, I have been somewhat arrogant with you (and others), but with some reason, mostly because I consider that you don't put in the necessary effort. Maybe it is because I am old school, during a time where there was no Internet and one had no recourse but to read the documentation, and programme by trial and error, over and over, until one got it right. No one would come to our aid to figure it out quickly, but this has the advantage of one really learning things well. Really well! I truely recommend you take this approach. It might seem slower (it's not), and it will truely make you a better programmer.

However, I totally disagree that I have not been helpful nor that I have not offered you proper assistance. Real "nastiness" would have been for me to totally ignore your posts, even when I see you struggling.

Again, I recommend the "old school" way!