How to figure out the close price when position ran into stop loss?

 

Hi there,

I am wondering how to figure out the close price and time, when a position was closed by its corresponding stop loss.

The picture shows a part of the history. The original position id was 1253268, it contains no entries for profit or loss. The corresponding stop loss is below, 1253269, this additional order, created by MT5, contains the data, but I don´t see any connection between both. So how is it possible to figure out that the original was closed by stop loss and with which results?

Thanks in advance


History sample

 

Hi Doerk,

check the order history for comments the syntax SL 

StringFind( OrderComment(), "[sl]" ) 

Every order which is closed by SL is tagged with this "sl" 

Hope this helps

Daniel

 
Doerk Hilger:

Hi there,

I am wondering how to figure out the close price and time, when a position was closed by its corresponding stop loss.

The picture shows a part of the history. The original position id was 1253268, it contains no entries for profit or loss. The corresponding stop loss is below, 1253269, this additional order, created by MT5, contains the data, but I don´t see any connection between both. So how is it possible to figure out that the original was closed by stop loss and with which results?

Thanks in advance



https://www.mql5.com/en/docs/trading/historydealselect

Documentation on MQL5: Trade Functions / HistoryDealSelect
Documentation on MQL5: Trade Functions / HistoryDealSelect
  • www.mql5.com
Selects a deal in the history for further calling it through appropriate functions. It returns true if the function has been successfully completed. Returns false if the function has failed. For more details on error call GetLastError(). Do not confuse orders, deals and positions. Each deal is the result of the execution of an order, each...
 
Thanks guys, yes, but this does not lead to the corresponding deal. I need a chain from the position (entry #1) to the deal (entry #2), in the shown example, from deal 1223902 to deal ID 1223903. How is it possible to obtain this deal ID by the position with the SL? Because I need all the details/results of the SL execution. 
 
Doerk Hilger:
Thanks guys, yes, but this does not lead to the corresponding deal. I need a chain from the position (entry #1) to the deal (entry #2), in the shown example, from deal 1223902 to deal ID 1223903. How is it possible to obtain this deal ID by the position with the SL? Because I need all the details/results of the SL execution. 

Calculate the difference between the in & out price.

https://www.mql5.com/en/docs/constants/tradingconstants/dealproperties#enum_deal_property_double

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
A deal is the reflection of the fact of a trade operation execution based on an order that contains a trade request. Each trade is described by properties that allow to obtain information about it. In order to read values of properties, functions of the Identifier of a position, in the opening, modification or closing of which this deal...
 
Icham Aidibe:

Calculate the difference between the in & out price.

https://www.mql5.com/en/docs/constants/tradingconstants/dealproperties#enum_deal_property_double

This sounds like a workaround. I need a reliable connection between the original position/deal and the deal with the SL execution. What if there were 10 positions at different prices and 10 stop losses at the same price? There must be a correct solution to build a chain between them all.

 
The problem is, when you execute HistorySelectByPosition(1253268), there are no results, all fields are empty when SL was executed, no close price, close time etc. 
 
Doerk Hilger:
The problem is, when you execute HistorySelectByPosition(1253268), there are no results, all fields are empty when SL was executed, no close price, close time etc. 

Damn I'm lazy to write you an example!

 
  HistorySelect(0,TimeCurrent());
  for (int i=0;i<HistoryDealsTotal();i++)
  {
  long entry = 0; double price = 0;
  if(HistoryDealGetInteger(HistoryDealGetTicket(i),DEAL_ENTRY,entry) && entry == DEAL_TYPE_BUY && HistoryDealGetDouble(HistoryDealGetTicket(i),DEAL_PRICE,price)) { printf("*** price in : %g",price); }
  }
 
Icham Aidibe:

Damn I'm lazy to write you an example!

I don´t ask for an example, I just ask for the solution. Which is the function and property that tells me the corresponding deal ID with the SL execution? None of the properties I know returns such. If you know more than the documentation does, me and the rest of the MQL world would be glad to know. If I am on the wrong way, I´d be glad to meet the right one.

 
Doerk Hilger:

I don´t ask for an example, I just ask for the solution. Which is the function and property that tells me the corresponding deal ID with the SL execution? None of the properties I know returns such. If you know more than the documentation does, me and the rest of the MQL world would be glad to know. If I am on the wrong way, I´d be glad to meet the right one.

Yes you'll got an example on how to use the function, I don't know how your EA is made. You get the in price, the out price & you make the difference.

Reason: