- Questions from a "dummy"
- Difficulty selecting the right history
- How to write EA with hidden takeprofit and stoploss?
If you don't already know the Position ID, then you can scan through the Orders history to get a list of Position ID's.
Then use each position ID with HistorySelectByPosition() to get access to all the deals for that position.
You can then scan through the resulting set with HistoryDealGetTicket(), of the the total number returned by HistoryDealsTotal().
No, on MT5, the position ID does not change after a parcial close.
If you don't already know the Position ID, then you can scan through the Orders history to get a list of Position ID's.
Then use each position ID with HistorySelectByPosition() to get access to all the deals for that position.
You can then scan through the resulting set with HistoryDealGetTicket(), of the the total number returned by HistoryDealsTotal().
I've already answered ... "If you don't already know the Position ID, then you can scan through the Orders history to get a list of Position ID's."
For a better understanding, please 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.
I've already answered ... "If you don't already know the Position ID, then you can scan through the Orders history to get a list of Position ID's."
For a better understanding, please read the following ...
Oh, my bad, for some reason I didn't noticed the "don't". So, to summarize:
-Scan the closed orders with HistoryOrdersTotal()
-Iterate through the orders and retrieve the associated ticket with HistoryOrderGetTicket() given that the order was a market order and not a pending order
-Select all related deals with HistorySelectByPosition(<ticket>)
-Iterate on them using HistoryDealsTotal().
Would that be the workflow?
Oh, my bad, for some reason I didn't noticed the "don't". So, to summarize:
-Scan the closed orders with HistoryOrdersTotal()
-Iterate through the orders and retrieve the associated ticket with HistoryOrderGetTicket() given that the order was a market order and not a pending order
-Select all related deals with HistorySelectByPosition(<ticket>)
-Iterate on them using HistoryDealsTotal().
Would that be the workflow?
Almost but not quite ...
First you have to select the total orders to scan:
Retrieves the history of transactions and orders for the specified period of the server time
Then you can retrieve the total Order count and iterate over them using:
Returns the number of deals in the history
Returns a ticket of a corresponding deal in the history
And for each iteration, retrieve the Position ID with:
Returns the requested property of the order (datetime or int)
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.
You have to collect ALL the Positions IDs first before proceeding. You cannot do the next step on each iteration or else that will mess up the cache.
So, only once you have collected all the position IDs, can you then use:
Requests the history of deals with a specified position identifier.
And proceed with scanning the deals with:
Returns the number of deals in the history
Returns a ticket of a corresponding deal in the history
Almost but not quite ...
First you have to select the total orders to scan:
Retrieves the history of transactions and orders for the specified period of the server time
Then you can retrieve the total Order count and iterate over them using:
Returns the number of deals in the history
Returns a ticket of a corresponding deal in the history
And for each iteration, retrieve the Position ID with:
Returns the requested property of the order (datetime or int)
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.
You have to collect ALL the Positions IDs first before proceeding. You cannot do the next step on each iteration or else that will mess up the cache.
So, only once you have collected all the position IDs, can you then use:
Requests the history of deals with a specified position identifier.
And proceed with scanning the deals with:
Returns the number of deals in the history
Returns a ticket of a corresponding deal in the history
Almost but not quite ...
First you have to select the total orders to scan:
Retrieves the history of transactions and orders for the specified period of the server time
Then you can retrieve the total Order count and iterate over them using:
Returns the number of deals in the history
Returns a ticket of a corresponding deal in the history
And for each iteration, retrieve the Position ID with:
Returns the requested property of the order (datetime or int)
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.
You have to collect ALL the Positions IDs first before proceeding. You cannot do the next step on each iteration or else that will mess up the cache.
So, only once you have collected all the position IDs, can you then use:
Requests the history of deals with a specified position identifier.
And proceed with scanning the deals with:
Returns the number of deals in the history
Returns a ticket of a corresponding deal in the history
Thank you very much, the workflow is very clear. I put it exactly like that in Chat GPT and it gave me the following:
// Definition of a structure to store position information struct PositionInfo { ulong ticket; double profit; datetime close_time; }; // Function to get all closed positions in the last 30 days void GetLastClosedPositions(PositionInfo &positions[]) { datetime fromTime = TimeCurrent() - 30 * 86400; // 30 days ago datetime toTime = TimeCurrent(); // Current time // Select the order and transaction history for the last 30 days if (!HistorySelect(fromTime, toTime)) { Print("Failed to select the history."); return; } // Get the total number of deals in the history int totalDeals = HistoryDealsTotal(); // Initialize the array to store position IDs ArrayResize(positions, 0); // Iterate over the deals to get the position IDs for (int i = 0; i < totalDeals; i++) { ulong dealTicket = HistoryDealGetTicket(i); // Get the position ID associated with the order ulong positionID = HistoryDealGetInteger(dealTicket, DEAL_POSITION_ID); // Check if the position is already in the list bool positionExists = false; for (int j = 0; j < ArraySize(positions); j++) { if (positions[j].ticket == positionID) { positionExists = true; break; } } // If the position is not in the list, add it if (!positionExists) { PositionInfo newPos; newPos.ticket = positionID; newPos.profit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT); newPos.close_time = HistoryDealGetInteger(dealTicket, DEAL_TIME); int newSize = ArraySize(positions) + 1; ArrayResize(positions, newSize); positions[newSize - 1] = newPos; } } // Sort the positions by close time in descending order ArraySort(positions, WHOLE_ARRAY, 0, MODE_DESCEND, 2); } // Usage example void OnTick() { // Define an array to store the closed positions in the last 30 days PositionInfo positions[]; // Call the function to get the closed positions in the last 30 days GetLastClosedPositions(positions); // Print the transaction information for (int i = 0; i < ArraySize(positions); i++) { Print("Transaction ", positions[i].ticket, ": Profit = ", positions[i].profit, ", Close time = ", TimeToString(positions[i].close_time, TIME_DATE | TIME_MINUTES)); } }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use