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:
- 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
- Order Strategies. Multi-Purpose Expert Advisor
- Programming EA's Modes Using Object-Oriented Approach
- MQL5 Wizard: Placing Orders, Stop-Losses and Take Profits on Calculated Prices. Standard Library Extension
- 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?
It was meant to be a question but not clearly formulated as such. Let me catch up with the latter now:
- 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?
- 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.
- www.mql5.com
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 Programming, How 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.
- wiki2.org
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.
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.
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).
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
- Standard way to detect stop loss transaction executed on a position,
- OnTradeTransaction(), and
- Detect stop trigger in OnTradeTransaction backtest
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.
Thank you so much for your support!
So basically the most significant threads for detecting triggered stops are
- Standard way to detect stop loss transaction executed on a position,
- OnTradeTransaction(), and
- Detect stop trigger in OnTradeTransaction backtest
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.
void OnTradeTransaction(const MqlTradeTransaction & trans, const MqlTradeRequest & request, const MqlTradeResult & result) { if (HistoryDealSelect(trans.deal)) { ENUM_DEAL_ENTRY deal_entry = (ENUM_DEAL_ENTRY) HistoryDealGetInteger(trans.deal, DEAL_ENTRY); ENUM_DEAL_REASON deal_reason = (ENUM_DEAL_REASON) HistoryDealGetInteger(trans.deal, DEAL_REASON); if(EnumToString(deal_entry) == "DEAL_ENTRY_IN") { if(EnumToString(deal_reason) == "DEAL_REASON_EXPERT" && EnumToString(trans.deal_type) == "DEAL_TYPE_BUY") { Alert("Buy"); } else if(EnumToString(deal_reason) == "DEAL_REASON_EXPERT" && EnumToString(trans.deal_type) == "DEAL_TYPE_SELL") { Alert("Sell"); } } else if(EnumToString(deal_entry) == "DEAL_ENTRY_OUT") { if(EnumToString(deal_reason) == "DEAL_REASON_SL" && EnumToString(trans.deal_type) == "DEAL_TYPE_BUY") { Alert("Sell SL"); } else if(EnumToString(deal_reason) == "DEAL_REASON_SL" && EnumToString(trans.deal_type) == "DEAL_TYPE_SELL") { Alert("Buy SL"); } else if(EnumToString(deal_reason) == "DEAL_REASON_TP" && EnumToString(trans.deal_type) == "DEAL_TYPE_BUY") { Alert("Sell TP"); } else if(EnumToString(deal_reason) == "DEAL_REASON_TP" && EnumToString(trans.deal_type) == "DEAL_TYPE_SELL") { Alert("Buy TP"); } } } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.
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.