MQL5 Tutorial: How to get the Order History Profit



This tutorial is centered around the development of an Expert Advisor in MQL5 that calculates the profit for positions that have already been closed in the history. The primary objective is to determine the profit from the last closed position and display it. Here’s an in-depth breakdown:

  1. Objective and Overview: The tutorial’s main goal is to calculate the profit for positions that have been historically closed. It will display a message indicating the last profit value derived from the first order in the history.
  2. The OnTick Function: At the heart of the code lies the OnTick function. This function is triggered with every new price update, serving as the foundation for all subsequent calculations and actions of the Expert Advisor.
  3. Signal Variable Creation: Within the OnTick function, a string variable named “Signal” is meticulously defined. This variable’s purpose is to later determine if a buy or sell signal is present, guiding the subsequent logic.
  4. Order History Calculation:
    • History Selection: The tutorial employs the historyselect function to fetch the history up to the current time using timecurrent. This provides a comprehensive list of all historical trades.
    • Looping Through Deals: The code then loops through all the deals using an unsigned integer as a counter. For each deal, various parameters are fetched and calculated.
    • Ticket Number: The ticket number for each deal is fetched using the historydealgetticket function. This ticket number serves as a unique identifier for each deal.
    • Order Profit: The profit for each order is calculated using the historydealgetdouble function combined with the dealprofit parameter.
    • Order Type & Currency Pair: The order type (buy/sell) and the currency pair for each deal are determined using the historydealgetinteger and historydealgetstring functions, respectively.
    • Deal Entry Type: The deal entry type, which indicates whether the order was closed or not, is fetched using historydealgetinteger combined with the dealentry parameter.
  5. Determining the Last Profit:
    • Criteria Matching: The logic checks if the currency pair of the order in history matches the current currency pair on the chart. Additionally, it checks the order type and whether the order was closed.
    • Profit Calculation: Based on the matched criteria, the last profit is determined. If the criteria are met, the result string is populated with the order profit, ticket number, and position direction.
  6. Displaying the Result: The populated result string, which encapsulates the order profit, ticket number, and position direction, is returned to the main function (OnTick). This result is then displayed to provide the trader with the information about the last closed position’s profit.

In conclusion, this tutorial offers a detailed walkthrough of creating an Expert Advisor in MQL5 that calculates and displays the profit from the last closed position. It showcases the intricacies of MQL5 functions and their applications, making it an invaluable resource for traders aiming to delve deeper into automated trading.