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

 
Doerk Hilger:

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

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

void OnStart()
{
  const int Total = OrdersHistoryTotal();
  
  for (int i = 0; i < Total; i++)
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderCloseReason() == DEAL_REASON_SL))
      OrderPrint();  
}


MT5-Result

#4393663 2018.07.09 18:46:41 buy 0.01 USDZAR 13.39350 13.48019 0.00000 2018.07.10 12:40:21 13.47910 -0.08 -0.13 6.35 [sl 13.48019] 0
 
Like the above but instead of loop on all history only on HistorySelectByPosition
 
Amir Yacoby:
Like the above but instead of loop on all history only on HistorySelectByPosition

SELECT_BY_TICKET.

 
fxsaber:

SELECT_BY_TICKET.

but loop on all history

 
Amir Yacoby:

but loop on all history

Because this example is for SELECT_BY_POS.

 
fxsaber:

Because this example is for SELECT_BY_POS.

The OP wanted an sl from position.
He needs to select the position and get the position id by PositionGetInteger(POSITION_IDENTIFIER) and the select its deals using HistorySelectByPosition.

Doerk, maybe you used position ticket and not identifier?
 
Amir Yacoby:
The OP wanted an sl from position.
He needs to select the position and get the position id by PositionGetInteger(POSITION_IDENTIFIER) and the select its deals using HistorySelectByPosition.

Script for this situation (screenshot)

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

void OnStart()
{
  if (OrderSelect(1223903, SELECT_BY_TICKET))
    OrderPrint();  
}
 

Thanks guys for your input. The "ran into SL" was only a sample, I needed the profit of a position no matter how it was closed. The solution is now like this and kind of universal for either positions or deals, written for my own needs. One can use it and modify it. 

The function returns any numeric information of a closed trade, such as the time, price, profit, swap and commission, no matter how many deals were involved. One may either pass the position ticket or deal ticket,


#define TICKET ulong
#define TICKET_INVALID 0

      //+------------------------------------------------------------------+
      //|  Get closing information                                         |
      //+------------------------------------------------------------------+

         bool  GetCloseData(TICKET position, TICKET deal, double &price, datetime &time, double &profit, double &swap, double &commission, bool report=true)
            {
               //--- Try to find the closing information 
               price=PRICE_INVALID;
               profit=0;
               time=0;
               swap=0;
               commission=0;
               
               bool result=false;
               //--- Use position ticket
               if (position!=TICKET_INVALID)
                  result=HistorySelectByPosition(position);
               //--- Use deal ticket
               else if (deal!=TICKET_INVALID)
                  result=HistoryDealSelect(deal);
               if (result)
                  {
                  int cnt=HistoryDealsTotal();
                  for (int i=0;i<cnt;i++)
                     {
                     TICKET tempticket=HistoryDealGetTicket(i);
                     if (report)
                        {
                        string text=HistoryDealGetString(tempticket,DEAL_COMMENT);
                        Print("#",i,": Ticket ",tempticket, " / Profit ",HistoryDealGetDouble(tempticket,DEAL_PROFIT), " / Price ",HistoryDealGetDouble(tempticket,DEAL_PRICE), " / Text ", text);
                        }
                     profit+=HistoryDealGetDouble(tempticket,DEAL_PROFIT);
                     commission+=HistoryDealGetDouble(tempticket,DEAL_COMMISSION);
                     swap+=HistoryDealGetDouble(tempticket,DEAL_SWAP);
                     double closeprice=HistoryDealGetDouble(tempticket,DEAL_PRICE);
                     if (closeprice!=0 && price<=0) price=closeprice;
                     long closetime=HistoryDealGetInteger(tempticket,DEAL_TIME);
                     if (closetime!=0 && time==0) time=closetime;
                     }
                  if (report)
                     Print("Closed profit by history data, ticket #",position!=TICKET_INVALID ? position : deal, ", total:",profit+swap+commission, " (Net/Swap/Comm: ",profit,"/",swap,"/",commission,")");
                  return true;
                  }
               return false;
            }
 
Daniel Stein: check the order history for comments the syntax SL  Every order which is closed by SL is tagged with this "sl"
  1. Broker dependent. Some brokers do not modify comments. Not a good idea to use comments, brokers can change comments, including complete replacement.
  2. Your answer is for MT4 (Orders.) This is the MT5 forum (Positions.)