MetaEditor build 1490 - page 2

 
Andrey Dik:

How do I know that SL/TP has been triggered in OnTradeTransaction()?

To understand the logic of messages in OnTrade*, I make a printout of input parameters at the moment when the event of interest occurred.

Do the same, everything becomes clear at once.

Remember, the TP in MT5 is always (in contrast to MT4) a market order, which hangs on the MT5 server, not on the exchange/ECN. Accordingly, when it is triggered there will be latency + market slippage of any sign. On some MT4 TP are limit orders which are placed directly on the ECN. So there is no latency (MT-ECN) and no negative slippage, but there is a redirect. So TP is one of the strong disadvantages of MT5 over MT4.

 
fxsaber:

To understand the logic of the messages in OnTrade*, I make a printout of the input parameters at the moment when the event of interest occurred.

Do the same, everything becomes clear at once.

Tried it. In this case OnTrade() is of no use and even no good, since we have to process the history for each trade event.

It makes sense to find out about triggering of SL/TP in OnTradeTransaction() because you can filter it by event type. In this case I defined the event type as "Add to history", then the trade type as "Exit" and then a deadlock.

 
Andrey Dik:

Tried it. OnTrade() in this case is of no use, even detrimental, as it will have to process the history at each trade event.

I was referring to OnTrade*.
 
fxsaber:
It was about OnTrade*.
Yeah, I got that. Tried it. I haven't been able to find out anything (from what I need).
 
#include <MT4Orders.mqh> // https://www.mql5.com/ru/code/16006

bool FirstRun = true;

void OnTick()
{  
  if (FirstRun)    
  {
    const double Price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    
    FirstRun = (OrderSend(_Symbol, OP_BUY, 1, Price, 0, 0, Price + 10 * _Point) <= 0);
  }
}

void OnTradeTransaction ( const MqlTradeTransaction &Trans, const MqlTradeRequest &Request, const MqlTradeResult &Result )
{
  if (!FirstRun)
    Print(ToString(Trans) + ToString(Request) + ToString(Result));
}

#define TOSTRING(A)  #A + " = " + (string)(A) + "\n"
#define TOSTRING2(A) #A + " = " + EnumToString(A) + "\n"

string ToString( const MqlTradeTransaction &Trans )
{
  return(TOSTRING(Trans.deal) + TOSTRING(Trans.order) + TOSTRING(Trans.symbol) +
         TOSTRING2(Trans.type) + TOSTRING2(Trans.order_type) + TOSTRING2(Trans.order_state) +
         TOSTRING2(Trans.deal_type) + TOSTRING2(Trans.time_type) +
         TOSTRING(Trans.time_expiration) + TOSTRING(Trans.price) + TOSTRING(Trans.price_trigger) +
         TOSTRING(Trans.price_sl) + TOSTRING(Trans.price_tp) + TOSTRING(Trans.volume) +
         TOSTRING(Trans.position) + TOSTRING(Trans.position_by));
}

string ToString( const MqlTradeRequest &Request )
{
  return(TOSTRING2(Request.action) + TOSTRING(Request.magic) + TOSTRING(Request.order) +
         TOSTRING(Request.symbol) + TOSTRING(Request.volume) + TOSTRING(Request.price) +
         TOSTRING(Request.stoplimit) + TOSTRING(Request.sl) +  TOSTRING(Request.tp) +
         TOSTRING(Request.deviation) + TOSTRING2(Request.type) + TOSTRING2(Request.type_filling) +
         TOSTRING2(Request.type_time) + TOSTRING(Request.expiration) + TOSTRING(Request.comment) +
         TOSTRING(Request.position) + TOSTRING(Request.position_by));
}

string ToString( const MqlTradeResult &Result )
{
  return(TOSTRING(Result.retcode) + TOSTRING(Result.deal) + TOSTRING(Result.order) +
         TOSTRING(Result.volume) + TOSTRING(Result.price) + TOSTRING(Result.bid) +  
         TOSTRING(Result.ask) + TOSTRING(Result.comment) + TOSTRING(Result.request_id) +  
         TOSTRING(Result.retcode_external));
}
Tester result
2016.11.23 23:59:57   take profit triggered #2 buy 1.00 EURUSD 1.06235 tp: 1.06245 [#3 sell 1.00 EURUSD at 1.06245]
2016.11.23 23:59:57   deal #3 sell 1.00 EURUSD at 1.06245 done (based on order #3)
2016.11.23 23:59:57   deal performed [#3 sell 1.00 EURUSD at 1.06245]
2016.11.23 23:59:57   order performed sell 1.00 at 1.06245 [#3 sell 1.00 EURUSD at 1.06245]
2016.11.23 23:59:57   Trans.deal = 3
2016.11.23 23:59:57   Trans.order = 3
2016.11.23 23:59:57   Trans.type = TRADE_TRANSACTION_DEAL_ADD
2016.11.23 23:59:57   Trans.order_type = ORDER_TYPE_BUY
2016.11.23 23:59:57   Trans.order_state = ORDER_STATE_STARTED
2016.11.23 23:59:57   Trans.deal_type = DEAL_TYPE_SELL
2016.11.23 23:59:57   Trans.time_type = ORDER_TIME_GTC
2016.11.23 23:59:57   Trans.time_expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Trans.price = 1.06245
2016.11.23 23:59:57   Trans.price_trigger = 0.0
2016.11.23 23:59:57   Trans.price_sl = 0.0
2016.11.23 23:59:57   Trans.volume = 1.0
2016.11.23 23:59:57   Trans.position = 2
2016.11.23 23:59:57   Trans.position_by = 0
2016.11.23 23:59:57   Request.action = ENUM_TRADE_REQUEST_ACTIONS::0
2016.11.23 23:59:57   Request.magic = 0
2016.11.23 23:59:57   Request.order = 0
2016.11.23 23:59:57   Request.symbol =
2016.11.23 23:59:57   Request.volume = 0.0
2016.11.23 23:59:57   Request.price = 0.0
2016.11.23 23:59:57   Request.sl = 0.0
2016.11.23 23:59:57   Request.tp = 0.0
2016.11.23 23:59:57   Request.deviation = 0
2016.11.23 23:59:57   Request.type = ORDER_TYPE_BUY
2016.11.23 23:59:57   Request.type_filling = ORDER_FILLING_FOK
2016.11.23 23:59:57   Request.type_time = ORDER_TIME_GTC
2016.11.23 23:59:57   Request.expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Request.comment =
2016.11.23 23:59:57   Request.position = 0
2016.11.23 23:59:57   Result.retcode = 0
2016.11.23 23:59:57   Result.deal = 0
2016.11.23 23:59:57   Result.order = 0
2016.11.23 23:59:57   Result.volume = 0.0
2016.11.23 23:59:57   Result.price = 0.0
2016.11.23 23:59:57   Result.bid = 0.0
2016.11.23 23:59:57   Result.ask = 0.0
2016.11.23 23:59:57   Result.comment =
2016.11.23 23:59:57   Result.request_id = 0
2016.11.23 23:59:57  
2016.11.23 23:59:57   Trans.deal = 0
2016.11.23 23:59:57   Trans.order = 3
2016.11.23 23:59:57   Trans.symbol = EURUSD
2016.11.23 23:59:57   Trans.type = TRADE_TRANSACTION_ORDER_DELETE
2016.11.23 23:59:57   Trans.order_type = ORDER_TYPE_SELL
2016.11.23 23:59:57   Trans.order_state = ORDER_STATE_FILLED
2016.11.23 23:59:57   Trans.deal_type = DEAL_TYPE_BUY
2016.11.23 23:59:57   Trans.time_type = ORDER_TIME_GTC
2016.11.23 23:59:57   Trans.price = 1.06245
2016.11.23 23:59:57   Trans.price_trigger = 0.0
2016.11.23 23:59:57   Trans.price_sl = 0.0
2016.11.23 23:59:57   Trans.price_tp = 0.0
2016.11.23 23:59:57   Trans.volume = 1.0
2016.11.23 23:59:57   Trans.position = 2
2016.11.23 23:59:57   Trans.position_by = 0
2016.11.23 23:59:57   Request.action = ENUM_TRADE_REQUEST_ACTIONS::0
2016.11.23 23:59:57   Request.magic = 0
2016.11.23 23:59:57   Request.symbol =
2016.11.23 23:59:57   Request.volume = 0.0
2016.11.23 23:59:57   Request.price = 0.0
2016.11.23 23:59:57   Request.stoplimit = 0.0
2016.11.23 23:59:57   Request.sl = 0.0
2016.11.23 23:59:57   Request.tp = 0.0
2016.11.23 23:59:57   Request.deviation = 0
2016.11.23 23:59:57   Request.type = ORDER_TYPE_BUY
2016.11.23 23:59:57   Request.type_filling = ORDER_FILLING_FOK
2016.11.23 23:59:57   Request.expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Request.comment =
2016.11.23 23:59:57   Request.position = 0
2016.11.23 23:59:57   Request.position_by = 0
2016.11.23 23:59:57   Result.retcode = 0
2016.11.23 23:59:57   Result.deal = 0
2016.11.23 23:59:57   Result.order = 0
2016.11.23 23:59:57   Result.volume = 0.0
2016.11.23 23:59:57   Result.price = 0.0
2016.11.23 23:59:57   Result.ask = 0.0
2016.11.23 23:59:57   Result.comment =
2016.11.23 23:59:57   Result.request_id = 0
2016.11.23 23:59:57   Result.retcode_external = 0
2016.11.23 23:59:57  
2016.11.23 23:59:57   Trans.deal = 0
2016.11.23 23:59:57   Trans.order = 3
2016.11.23 23:59:57   Trans.symbol = EURUSD
2016.11.23 23:59:57   Trans.type = TRADE_TRANSACTION_HISTORY_ADD
2016.11.23 23:59:57   Trans.order_state = ORDER_STATE_FILLED
2016.11.23 23:59:57   Trans.deal_type = DEAL_TYPE_BUY
2016.11.23 23:59:57   Trans.time_type = ORDER_TIME_GTC
2016.11.23 23:59:57   Trans.time_expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Trans.price = 1.06245
2016.11.23 23:59:57   Trans.price_trigger = 0.0
2016.11.23 23:59:57   Trans.price_sl = 0.0
2016.11.23 23:59:57   Trans.price_tp = 0.0
2016.11.23 23:59:57   Trans.volume = 0.0
2016.11.23 23:59:57   Trans.position_by = 0
2016.11.23 23:59:57   Request.action = ENUM_TRADE_REQUEST_ACTIONS::0
2016.11.23 23:59:57   Request.magic = 0
2016.11.23 23:59:57   Request.order = 0
2016.11.23 23:59:57   Request.symbol =
2016.11.23 23:59:57   Request.volume = 0.0
2016.11.23 23:59:57   Request.price = 0.0
2016.11.23 23:59:57   Request.stoplimit = 0.0
2016.11.23 23:59:57   Request.sl = 0.0
2016.11.23 23:59:57   Request.deviation = 0
2016.11.23 23:59:57   Request.type = ORDER_TYPE_BUY
2016.11.23 23:59:57   Request.type_filling = ORDER_FILLING_FOK
2016.11.23 23:59:57   Request.type_time = ORDER_TIME_GTC
2016.11.23 23:59:57   Request.expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Request.comment =
2016.11.23 23:59:57   Request.position = 0
2016.11.23 23:59:57   Request.position_by = 0
2016.11.23 23:59:57   Result.retcode = 0
2016.11.23 23:59:57   Result.order = 0
2016.11.23 23:59:57   Result.volume = 0.0
2016.11.23 23:59:57   Result.price = 0.0
2016.11.23 23:59:57   Result.bid = 0.0
2016.11.23 23:59:57   Result.ask = 0.0
2016.11.23 23:59:57   Result.comment =
2016.11.23 23:59:57   Result.request_id = 0
2016.11.23 23:59:57   Result.retcode_external = 0
 

fxsaber:

The tester's result

Yeah, but then what?

Where does it show that SL/TP is triggered? - That's the difficulty))

 

closing a position .

That is the problem. But MT5 receives this information somehow, judging by the log and drawing of closed positions.
 
fxsaber:
This is the problem. But MT5 somehow gets this information, judging by the log and drawing of closed positions.

Does it not work like this anymore?

Forum on trading, automated trading systems and strategy testing

How do I know if Take Profit or Stop Loss triggered?

Stanislav Korotky, 2014.10.16 12:06

I checked by SL/TP of the position (order) and that the closing price is better or equal to TP and worse or equal to SL.

 
Stanislav Korotky:

Does this not work anymore?

Can I have the code?

...yes yes, can I have Mashka by the thigh, sorry... show me the code, please.

 
Stanislav Korotky:

Does it not work like that anymore?

There is no way to define it. SL and TP are solely an MT server entity.
Reason: