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?
He needs to select the position and get the position id by PositionGetInteger(POSITION_IDENTIFIER) and the select its deals using HistorySelectByPosition.
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 ticketif (position!=TICKET_INVALID)
result=HistorySelectByPosition(position);
//--- Use deal ticketelseif (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,")");
returntrue;
}
returnfalse;
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I am wondering how to figure out the close price and time, when a position was closed by its corresponding stop loss.
MT5-Result
Like the above but instead of loop on all history only on HistorySelectByPosition
SELECT_BY_TICKET.
SELECT_BY_TICKET.
but loop on all history
but loop on all history
Because this example is for SELECT_BY_POS.
Because this example is for SELECT_BY_POS.
The OP wanted an sl from position.
Forum on trading, automated trading systems and testing trading strategies
How to figure out the close price when position ran into stop loss?
Doerk Hilger, 2018.07.10 08:50
Script for this situation (screenshot)
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,