I've read this post. But I am still not clear.
Here is my log when stop loss is executed in strategy tester.
Now I have a position.
It looks like OnTradeTransaction() is triggered 3 times.
The MqlTradeTransaction is: TRADE_TRANSACTION_HISTORY_ADD
The MqlTradeTransaction is: TRADE_TRANSACTION_ORDER_DELETE
The MqlTradeTransaction is: TRADE_TRANSACTION_DEAL_ADD
position is closed.
There is no transaction type called "stop_loss_executed". The right way to detect stop loss transaction is to do what?
My idea is if a position is closed, then it can be result from a stop loss on a position, or user manually send an order. There is no way to tell the difference. Is this correct?
If I am wrong, please tell me how to detect a stop loss event.
Here is my log for that minute when stop loss event is triggered.
Thanks!
You know your posittion (symbol, buy or sell), your volume, your sl price : EURUSD BUY 1.0 sl:1.27526
So if you detect a opposite deal, with the same volume and your sl price, it means stoploss has been triggered.
LO 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 The MqlTradeTransaction is: TRADE_TRANSACTION_DEAL_ADD PM 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Symbol: EURUSD GG 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Deal ticket: 5 IQ 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Deal type: DEAL_TYPE_SELL LD 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Order ticket: 5 IS 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Order type: ORDER_TYPE_BUY NL 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Order state: ORDER_STATE_STARTED HI 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Order time type: ORDER_TIME_GTC FQ 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Order expiration: 1970.01.01 00:00 CR 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Price: 1.27526 HJ 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Price trigger: 0 KL 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Stop Loss: 0 HE 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Take Profit: 0 QL 0 22:11:32.125 A1 (EURUSD,M1) 2014.09.26 03:00:39 Volume: 1
You know your posittion (symbol, buy or sell), your volume, your sl price : EURUSD BUY 1.0 sl:1.27526
So if you detect a opposite deal, with the same volume and your sl price, it means stoploss has been triggered.
enum postype {BUY,SELL,NONE}; static int prevposition=NONE; void OnTradeTransaction( const MqlTradeTransaction& trans, // estrutura das transações de negócios const MqlTradeRequest& request, // estrutura solicitada const MqlTradeResult& results // resultado da estrutura ) { HistorySelect(TimeCurrent()-3600,TimeCurrent()); if(trans.type==TRADE_TRANSACTION_DEAL_ADD && trans.deal_type==DEAL_TYPE_SELL && PositionsTotal()==0 && prevposition==BUY) { if(HistoryDealGetInteger(trans.deal,DEAL_REASON)==DEAL_REASON_TP){ Print("BUY + DEAL_REASON_TP"); prevposition=NONE;} if(HistoryDealGetInteger(trans.deal,DEAL_REASON)==DEAL_REASON_SL){ Print("BUY + DEAL_REASON_SL") prevposition=NONE;} } if(trans.type==TRADE_TRANSACTION_DEAL_ADD && trans.deal_type==DEAL_TYPE_BUY && PositionsTotal()==0 && prevposition==SELL) { if(HistoryDealGetInteger(trans.deal,DEAL_REASON)==DEAL_REASON_TP){ Print("SELL + DEAL_REASON_TP"); prevposition=NONE;} if(HistoryDealGetInteger(trans.deal,DEAL_REASON)==DEAL_REASON_SL){ Print("SELL + DEAL_REASON_SL"); prevposition=NONE;} } }This code works for me!!! Note I assign BUY/SELL to prevposition when I send the order and get confirmation.
You know your posittion (symbol, buy or sell), your volume, your sl price : EURUSD BUY 1.0 sl:1.27526
So if you detect a opposite deal, with the same volume and your sl price, it means stoploss has been triggered.
Isn't that a bit risky: "You know your position (symbol, buy or sell), your volume, your sl price : EURUSD BUY 1.0 sl:1.27526" ??
Why don't you use the Order-Ticket to identify the opposite deal? Is it changed or different compared to the original order?
For centuries we have learned to use the ticket number to identify the positions, open and closed?
Here My log file in backtest Mql5. I using the function OnTradeTransaction for check StopLoss events.
sometimes two or more stops occur at the same price and same time , but only one report is given in the function OnTradeTransaction. Can anyone help me?
sorry My code is very big and i can only report Log file.
Thanks!
at 14:48:32 two stoploss occoured(226 and 228) but in OnTradeTransaction Only reported one Stop(226) no any report from stoploss after that
void OnTradeTransaction( const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& results ) { HistorySelect(T1,TimeCurrent()); if(trans.symbol==Symbol()) { Print("\n--------- START OnTradeTransaction ---------"); Print("ONTRADE TYPE= ",EnumToString(trans.type)); if(trans.type==TRADE_TRANSACTION_DEAL_ADD) { if(HistoryDealGetInteger(trans.deal,DEAL_ENTRY)==DEAL_ENTRY_OUT) { if(HistoryDealGetInteger(trans.deal,DEAL_REASON)==DEAL_REASON_SL) { Print("OnTradeTransaction OUT-SL Position=",trans.position); Function_Stoploss(trans.position); } } } T1=TimeCurrent(); } } LH 0 19:14:13.071 Trade 2020.01.17 14:48:32 stop loss triggered #226 buy 0.34 EURUSD 1.11123 sl: 1.11062 [#234 sell 0.34 EURUSD at 1.11062] EP 0 19:14:13.071 Trades 2020.01.17 14:48:32 deal #58 sell 0.34 EURUSD at 1.11062 done (based on order #234) QF 0 19:14:13.071 Trade 2020.01.17 14:48:32 deal performed [#58 sell 0.34 EURUSD at 1.11062] HD 0 19:14:13.071 Trade 2020.01.17 14:48:32 order performed sell 0.34 at 1.11062 [#234 sell 0.34 EURUSD at 1.11062] DE 0 19:14:13.071 Trade 2020.01.17 14:48:32 stop loss triggered #228 buy 0.25 EURUSD 1.11146 sl: 1.11062 [#235 sell 0.25 EURUSD at 1.11062] IE 0 19:14:13.071 Trades 2020.01.17 14:48:32 deal #59 sell 0.25 EURUSD at 1.11062 done (based on order #235) NK 0 19:14:13.071 Trade 2020.01.17 14:48:32 deal performed [#59 sell 0.25 EURUSD at 1.11062] CQ 0 19:14:13.071 Trade 2020.01.17 14:48:32 order performed sell 0.25 at 1.11062 [#235 sell 0.25 EURUSD at 1.11062] NQ 0 19:14:13.071 Trade 2020.01.17 14:48:32 order [#232 sell stop 0.5 EURUSD at 1.11062] triggered FP 0 19:14:13.071 Trades 2020.01.17 14:48:32 deal #60 sell 0.5 EURUSD at 1.11062 done (based on order #232) NR 0 19:14:13.071 Trade 2020.01.17 14:48:32 deal performed [#60 sell 0.5 EURUSD at 1.11062] LG 0 19:14:13.071 Trade 2020.01.17 14:48:32 order performed sell 0.5 at 1.11062 [#232 sell stop 0.5 EURUSD at 1.11062] 2020.01.17 14:48:32 2020.01.17 14:48:32 --------- START OnTradeTransaction --------- 2020.01.17 14:48:32 ONTRADE TYPE= TRADE_TRANSACTION_DEAL_ADD 2020.01.17 14:48:32 DEAL_ADD DEAL_REASON= 4 2020.01.17 14:48:32 OnTradeTransaction OUT-SL Position=226
- www.mql5.com
at 14:48:32 two stoploss occoured(226 and 228) but in OnTradeTransaction Only reported one Stop(226) no any report from stoploss after that
Here is a technical forum and no one believes just words. You should show the OnTradeTransaction code - you probably made a mistake in handling events inside OnTradeTransaction.
What
? Where did it come from?
Why are you doing it? All you need is to find ONE deal in the trading history. You don't need to copy the ENTIRE trading history! Moreover, you are copying a trading history with an error.
T1 is a global Parameter in top of expert when expert run it set to starttime then it update frome OnTradeTransaction in every Add Deal event
datetime starttime=TimeCurrent(); datetime T1=starttime;
Why I copying a trading history with an error? That function is recommended by Metaquotes
T1 is a global Parameter in top of expert when expert run it set to starttime then it update frome OnTradeTransaction in every Add Deal event
Why I copying a trading history with an error? That function is recommended by Metaquotes
You are performing this function with an error. You are requesting trade history incorrectly. And most importantly - you do not need an action. You need to request ONE deal.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I've read this post. But I am still not clear.
Here is my log when stop loss is executed in strategy tester.
Now I have a position.
It looks like OnTradeTransaction() is triggered 3 times.
The MqlTradeTransaction is: TRADE_TRANSACTION_HISTORY_ADD
The MqlTradeTransaction is: TRADE_TRANSACTION_ORDER_DELETE
The MqlTradeTransaction is: TRADE_TRANSACTION_DEAL_ADD
position is closed.
There is no transaction type called "stop_loss_executed". The right way to detect stop loss transaction is to do what?
My idea is if a position is closed, then it can be result from a stop loss on a position, or user manually send an order. There is no way to tell the difference. Is this correct?
If I am wrong, please tell me how to detect a stop loss event.
Here is my log for that minute when stop loss event is triggered.
Thanks!