Your topic has been moved to the section: Expert Advisors and Automated Trading — Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
I use this code.
#include <Trade\Trade.mqh> void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) { CDealInfo objDeal; long reason=-1; //--- get transaction type as enumeration value ENUM_TRADE_TRANSACTION_TYPE type=trans.type; //--- if transaction is result of addition of the transaction in history if(type==TRADE_TRANSACTION_DEAL_ADD) { if(HistoryDealSelect(trans.deal)) objDeal.Ticket(trans.deal); else { Print(__FILE__," ",__FUNCTION__,", ERROR: HistoryDealSelect(",trans.deal,")"); return; } //--- if(!objDeal.InfoInteger(DEAL_REASON,reason)) { Print(__FILE__," ",__FUNCTION__,", ERROR: InfoInteger(DEAL_REASON,reason)"); return; } if((ENUM_DEAL_REASON)reason==DEAL_REASON_SL) { //Handle when price hit SL } else if((ENUM_DEAL_REASON)reason==DEAL_REASON_TP) { //Handle when price hit TP } } }
HenZen:
Greetings,
I'd like to detect when the SL or TP level is hit and take appropriate action. Is the following snippet correct?
I'd appreciate any comments. This *appears* to work, but I'm not sure.
Thanks
You can test your code in strategy tester and see whether it works. For example,....check below code
void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) { if(HistoryDealSelect(trans.deal)) { long reason = HistoryDealGetInteger(trans.deal, DEAL_REASON); if ((reason == DEAL_REASON_SL && HistoryDealGetInteger(trans.deal, DEAL_MAGIC) == eaMagic) Alert("StopLoss has been taken out"); } if ((reason == DEAL_REASON_TP && HistoryDealGetInteger(trans.deal, DEAL_MAGIC) == eaMagic) Alert("TakeProfit has been hit"); } }
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Greetings,
I'd like to detect when the SL or TP level is hit and take appropriate action. Is the following snippet correct?
I'd appreciate any comments. This *appears* to work, but I'm not sure.
Thanks