Download MetaTrader 5

OnTakeProfit & OnStopLoss instead of OnTradeTransaction()

To add comments, please log in or register
Sign up for MQL5.community to post your Forum topic
Thomas Schwabhäuser
77
Thomas Schwabhäuser 2016.02.18 16:02 

In the thread Standard way to detect stop loss transaction executed on a position it was suggested to catch SL and TP events by waiting for TradeTransaction a event for the "opposite deal, with the same volume and your sl price" as this "means stoploss has been triggered"

The thread OnTradeTransaction() had provided more detailed information a few months earlier.

 Alain Verleyen:

There are mainly 2 ways to detect that a position was closed by SL :

    • If you had an open position and it is now closed, check the history of deals to find the closing price of your position. This is the standard approach.
    • Use OnTradeTransaction to detect changes in your position "on the fly". But it's an advanced technique.

However, it had already been requested in MQL4 community thread OnTakeProfit & OnStopLoss to integrate the standard task of detecting whether SL or TP were triggered into the event-driven programming model of MQL5. I do not claim that my research on this topic has been exhaustive but I have definitely spent more time on it than we usually want to spend on standard problems which are considered to have been solved. In addition to online documentation and community articles I consulted books like EA Programing for MT5 by Andrew Young. Unfortunately the latter does not cover OnTradeTransaction() but praises the Event-driven OOP model as presented in various MQL5 Cookbook articles, as well.

Noticing the problem that SL is not necessarily triggered in the TradeTransaction event immediately following the event confirming the deal for which the SL has been triggered I cannot help keeping wondering why the suggestion for a better coding style has not been considered yet although every aspect of it is superior to the "standard approach" as well as the so-called "advanced technique". In particular as this suggestion was made in 2007!

Moreover, I wonder if there are really as few code examples for catching SL and TP as I have found, namely zero.

OnTakeProfit & OnStopLoss - MQL4 forum
OnTakeProfit & OnStopLoss - MQL4 forum
  • www.mql5.com
OnTakeProfit & OnStopLoss - MQL4 forum - MQL4 forum
Alain Verleyen
Moderator
30752
Alain Verleyen 2016.02.18 16:14  
Is this a question ?
Thomas Schwabhäuser
77
Thomas Schwabhäuser 2016.02.18 16:24  
Alain Verleyen:
Is this a question ?

It was meant to be a question but not clearly formulated as such. Let me catch up with the latter now:

  1. Could anybody point me to standard code examples for catching SL and TP that fit into standard EA prototypes as promoted by online documentation, community articles, MQL5 Cookbook articles, and the book EA Programing for MT5? In particular, I am looking for how to add SL/TP catching to the class CTrade or the examples in
  2. How can I extend the event-driven programming model by new SL and TP events which will be caught by custom event handlers potentially named OnTakeProfit & OnStopLoss?
Alain Verleyen
Moderator
30752
Alain Verleyen 2016.02.18 17:07  
Thomas Schwabhäuser:

It was meant to be a question but not clearly formulated as such. Let me catch up with the latter now:

  1. Could anybody point me to standard code examples for catching SL and TP that fit into standard EA prototypes as promoted by online documentation, community articles, MQL5 Cookbook articles, and the books EA Programing for MT5?
  2. How can I extend the event-driven programming model by new SL and TP events which will be caught by custom event handlers potentially named OnTakeProfit & OnStopLoss?

There is nothing "standard" as far as I know, you can see this topic for some code examples.

You can use EventChartCustom() to create your own events.

Detect stop trigger in OnTradeTransaction backtest
Detect stop trigger in OnTradeTransaction backtest
  • www.mql5.com
Unfortunately, i get only DEAL_ENTRY_IN provided by MqlTradeTransaction passed to OnTradeTransaction. - - Category: technical indicators
Thomas Schwabhäuser
77
Thomas Schwabhäuser 2016.02.18 19:29  
Alain Verleyen:

There is nothing "standard" as far as I know, you can see this topic for some code examples.

You can use EventChartCustom() to create your own events.

That is about what I already expected after having read as much as possible before asking. Nonetheless I am extremely grateful for your advice!

When I had been reading the article Detect stop trigger in OnTradeTransaction backtest I noticed that it specifically addresses the question how to detect stops when backtesting. This immediately raises the question if I need different methods for detecting stops in backtesting and in real trading. And if so, how would I test the stop detection that is to be used for real trading? However, it does not seem to me as if this method for detecting stops in backtesting could miss in real trading, could it?

As Perl is among many languages I used to code with I am familiar with the concept TIMTOWTDI. However, since MT4 and 5 provide a Wizard it might be reasonable to consider its framework sort of quasi-standard following the descriptions MQL5 Wizard: Creating Expert Advisors without ProgrammingHow to Create a Module of Trading Signals, of Trailing of Open Positions, and a Risk and Money Management Module. If I want to implement a new trading strategy why should not I follow such a standard which allows for better integration into my environment and facilitates sharing my ideas with other traders? What is the reason for this community if sharing trading systems is not desired? Standards help us sharing ideas. Moreover, it is a generally accepted best practice not to reinvent the wheel.

Pointed to EventChartCustom() I concluded you recommend to call it in OnTradeTransaction() after I catch SL and TP there. So did I get you right that it is possible to create an actual trade event this way although such an event is considered a chart event according to Events of the client terminal?

I am just asking because the docs mention different types of events so it should not be possible that one type is taken for another one. However I assume the answer is just that different types of events are dealt by different handlers since a custom chart event triggers OnChartEvent() whose responsibility it is to identify it as a custom trade event and act accordingly. I hope I understood the essentials but if others are equally far from certainty it should be helpful to confirm what SEEMS right to me now.

There's more than one way to do it - Wikipedia Republished // WIKI 2
There's more than one way to do it - Wikipedia Republished // WIKI 2
  • en.wiki2.org
There's more than one way to do it (TMTOWTDI or TIMTOWTDI, pronounced Tim Toady) is a Perl programming motto. The language was designed with this idea in mind, in that it “doesn't try to tell the programmer how to program.” As proponents of this motto argue, this philosophy makes it easy to write concise statements like or...
Alain Verleyen
Moderator
30752
Alain Verleyen 2016.02.18 21:05  
However, it does not seem to me as if this method for detecting stops in backtesting could miss in real trading, could it?

Theoretically this method could not work in real trading as trading event (OnTradeTransaction) are not guaranteed. However in practice I never got an issue with EA I coded and used on real account. So it's up to you to check and decide.

Alain Verleyen
Moderator
30752
Alain Verleyen 2016.02.18 21:12  

If I want to implement a new trading strategy why should not I follow such a standard which allows for better integration into my environment and facilitates sharing my ideas with other traders? What is the reason for this community if sharing trading systems is not desired? Standards help us sharing ideas. Moreover, it is a generally accepted best practice not to reinvent the wheel.

Metaquotes called some libraries they made "Standard Library" but I am afraid it's far from standard, I doubt there is more than 10 coders who are understanding these libraries, also because they are very poorly documented.

I understand but it's not so simple. For example, I will not share all my code publicly, my knowledge and coding skills is what allow me to earn my life, I will not share it for free. Most people come here asking to share ideas, while they don't provide any useful idea and share nothing, they just want to use other ideas.

Alain Verleyen
Moderator
30752
Alain Verleyen 2016.02.18 21:16  

Pointed to EventChartCustom() I concluded you recommend to call it in OnTradeTransaction() after I catch SL and TP there. So did I get you right that it is possible to create an actual trade event this way although such an event is considered a chart event according to Events of the client terminal?

I don't recommend anything. Whatever way you will use to detect the SL/TP trigger, you can then send a custom event. And yes all these events are processed through chart events (OnChartEvent handler).

Thomas Schwabhäuser
77
Thomas Schwabhäuser 2016.02.18 23:32  
Alain Verleyen:

I don't recommend anything. Whatever way you will use to detect the SL/TP trigger, you can then send a custom event. And yes all these events are processed through chart events (OnChartEvent handler).

Thank you so much for your support!

So basically the most significant threads for detecting triggered stops are

the latter of which seems most helpful to me. I think I will find my way through it tomorrow and solve the special case I have in mind quite soon. The general stop trigger event will require solving more issues but I dare to predict that I will post it at a later point of time.

Alain Verleyen
Moderator
30752
Alain Verleyen 2016.02.19 19:01  
Thomas Schwabhäuser:

Thank you so much for your support!

So basically the most significant threads for detecting triggered stops are

the latter of which seems most helpful to me. I think I will find my way through it tomorrow and solve the special case I have in mind quite soon. The general stop trigger event will require solving more issues but I dare to predict that I will post it at a later point of time.

Can't wait to see your solution
To add comments, please log in or register