Hi Folks,
Try codebase for some examples.
like here https://www.mql5.com/en/search#!keyword=history%20profit&module=mql5_module_codebase
Working example
Profit labels for closed trades (deals)
Conor Mcnamara, 2024.12.21 01:32
Creating profit labels on deals (closed trades) which also show in the strategy tester
Post edited by moderator. Instead of links to CodeBase and Articles, consider using the "pocket" feature instead.
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
It will benefit you to read the following ...
Orders, Positions and Deals in MetaTrader 5
MetaQuotes, 2011.02.01 16:13
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
MY_OrderTicket "1111" (what you get, If you send an Order via OrderSend()) -> (executed) dealticket_01 "2222" -> (Order sits in the market) -> (SL triggert) newOrderticket "3333" -> (executed) dealticket_02 "4444"
I don't find the link between 2222 and 3333
//OrderSend() gave us the inital intitTicket //This function will give us the cache HistoryOrderSelect(intitTicket) //To get the dealTicket, we can use HistoryDealGetTicket(intitTicket) //then I am stuck... there is no connection to the new orderTicket, that the position gets, if Stop Loss is triggert... //you cann get the history cache HistorySelect(timeOfLastTrade, TimeCurrent()); //Loop through the cache find the deals, but how to connect? for (int i = 0; i < HistoryDealsTotal(); i++) //SL was triggert and we get a new orderTicket, that is found here ulong deal_order = HistoryDealGetInteger(dealTicket, DEAL_ORDER); //here we get the dealTicket of the execution ulong deal_ticket = HistoryDealGetInteger(deal_order, DEAL_TICKET); //and now we can look up the profit double dealProfit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
Is MQL5 really build, that you lost track of you position, as soon, as they will be executet?
Thanks in advance for new input
A few days more, a bit wiser, but no solution to this time. What I understand:
MY_OrderTicket "1111" (what you get, If you send an Order via OrderSend()) -> (executed) dealticket_01 "2222" -> (Order sits in the market) -> (SL triggert) newOrderticket "3333" -> (executed) dealticket_02 "4444"
I don't find the link between 2222 and 3333
Is MQL5 really build, that you lost track of you position, as soon, as they will be executet?
Thanks in advance for new input
It seems you did not read, or properly research, the article that I advised you to read ... Orders, Positions and Deals in MetaTrader 5
Had you read it, you would have know that there is ticket that connects them all, called a "position identifier".
Retrieves the history of deals and orders having the specified position identifier.bool HistorySelectByPosition( long position_id // position identifier - POSITION_IDENTIFIER );
ENUM_POSITION_PROPERTY_INTEGER
POSITION_IDENTIFIER
Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.
Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.
When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.
long
ENUM_ORDER_PROPERTY_INTEGER
ORDER_POSITION_ID
Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of exactly this position is set to the executed order at this moment.
long
ENUM_DEAL_PROPERTY_INTEGER
DEAL_POSITION_ID
Identifier of a position, in the opening, modification or closing of which this deal took part. Each position has a unique identifier that is assigned to all deals executed for the symbol during the entire lifetime of the position.
long

- www.mql5.com
Please can somebody help?

- 2023.02.10
- Fotikhbek Khusanov
- www.mql5.com
Forum on trading, automated trading systems and testing trading strategies
What is the Order Close Price fucntion in MQL5?
fxsaber, 2023.11.23 14:08
#property script_show_inputs #include <HistoryPosition.mqh> // https://www.mql5.com/ru/blogs/post/755007 input long inTicket = 0; // Position Ticket void OnStart() { if (HistoryPositionSelect(inTicket)) Alert(HistoryPositionGetDouble(POSITION_HISTORY_PROFIT)); }
Myself had to understand, that the first Orderticket you get is also the PositionID! Damned that was hard...
MY_OrderTicket "1111" (what you get, If you send an Order via OrderSend()) -> (executed) dealticket_01 "2222" -> (Order sits in the market) -> (SL triggert) newOrderticket "3333" -> (executed) dealticket_02 "4444"
Her for all the poor souls, that have the same struggle my solution in code:
void examineProfit() { //This is the first Orderticket, you got from OrderSend()/ Ctrade trade -> trade.buy() -> trade.ResultOrder() //This will be also the PositionID, that is not changing through all the processing until the order is closed. //This PositionID-fact is superimportant to keep in mind ulong initialTicket = 1111; //Get the HistoryCache from the periode, you want to check, if the order was executed (for example closed by SL) //the last two days will be fine... datetime timeOfLastTrade = TimeCurrent()- (PeriodSeconds(PERIOD_D1) * 2); //trades will give us the number trades, that was delivered to the cache int trades = HistorySelect(timeOfLastTrade, TimeCurrent()); //Loop through the history data (HistoryDealsTotal() houses all the trades, we got from HistorySelect!) for (int i = 0; i < HistoryDealsTotal(); i++) { //get the dealTicket for each deal, you will get the part 2222 ulong dealTicket = HistoryDealGetTicket(i); //With dealTicket 2222 you lookup every deal, you find in the HistoryCache and take the positionID each lookup provides ulong positionID = HistoryDealGetInteger(dealTicket, DEAL_POSITION_ID); //If the positionID of the deal matches your intitTicket, you are almost there if (positionID == initialTicket) { //Now we have a match. This match will occour for every deal, that was made for your intitTicket(PositionID) 1111 //deal 2222 will also be found, but... //we are only looking for the DEAL_ENTRY: DEAL_ENTRY_OUT, that means, the Order was closed by SL or something //The only dealTicket that matches the DEAL_ENTRY_OUT, is 4444 - this is the dealticket we are looking for if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT) { //finaly we have a match!!! 4444 was found. Now give us the f***in information we need!!! double dealProfit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT); Print("dealProfit for '"+initialTicket+"': "+dealProfit); } } } }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Folks,
6 hours later and I am done... to get the profit from an opened Position you can do this:
Works like a charme...
BUT, if I want to get the profit from an closed Position, nothing works... I can see the ticketnumber in the MT5 History.
This works fine in another function, but this:
No Way, the ticketnumber will never be found. I dont get it, why is reading from open position a two-liner, but from closed, a +6h task, that failed?
Please can somebody help?
Thanks in advance...