MQL5 TUTORIAL – HOW TO CALCULATE THE HISTORY PROFIT

video
play-sharp-fill

 

  1. Introduction to Calculating Historical Profit (00:00 – 00:09)
    • Introduction to creating an expert advisor for calculating historical profit for a specific currency pair.
    • Overview of displaying the calculated profit on the chart.
  2. Starting with MetaEditor (00:09 – 00:16)
    • Instructions on how to start the MetaEditor for coding the expert advisor.
    • Brief mention of using the icon or pressing F4 on the keyboard.
  3. Including Trade Library and Creating CTrade Instance (00:16 – 00:31)
    • Explanation of including the Trade.mqh library for trade operations.
    • Creation of an instance of the CTrade class named ‘trade’ for executing buy and sell operations.
  4. Calculating Current Ask and Bid Prices (00:31 – 00:38)
    • Description of calculating the current Ask and Bid prices within the OnTick function.
    • Importance of these prices for executing buy and sell orders.
  5. Executing Buy and Sell Orders (00:38 – 00:46)
    • Checking for less than two open positions before executing orders.
    • Execution of both buy and sell orders for demonstration purposes.
  6. Calculating and Displaying Historical Profit (00:46 – 01:01)
    • Use of the GetCurrencyPairHistoryProfit function to calculate historical profit.
    • Displaying the calculated profit on the chart.
  7. Functioning of GetCurrencyPairHistoryProfit Function (01:01 – 01:09)
    • Detailed explanation of how the function calculates total historical profit for a specific currency pair.
  8. Initializing Variables and Fetching Historical Data (01:09 – 01:17)
    • Initialization of variables for storing deal information.
    • Use of the HistorySelect function to fetch historical deal data.
  9. Iterating Through Historical Deals (01:17 – 01:25)
    • Iteration through historical deals using a for loop.
    • Obtaining profit, swap, and order type for each deal.
  10. Ensuring Matching Currency Pair for Deals (01:25 – 01:34)
  • Checking the currency pair for each deal to match the current symbol.
  1. Compiling the Code and Further Learning Resources (01:34 – 01:49)
    • Instructions on compiling the code in MetaEditor.
    • Recommendation for the premium course or basic videos for further learning.
  2. Testing the Expert Advisor in MetaTrader (01:49 – 02:04)
    • Returning to MetaTrader and starting the strategy tester.
    • Selection of the created expert advisor and enabling visual mode for testing.
  3. Invitation for Premium Course Members (02:04 – 02:09)
    • Invitation for premium course members to suggest ideas for new videos.
  4. Conclusion and Summary (02:09 – 02:24)
    • Conclusion on how to calculate and display historical profit for a currency pair using MQL5.
    • Emphasis on the functionality of the created expert advisor with minimal MQL code.

 

In this video, we are going to create an expert advisor that calculates the historical profit for a specific currency pair and displays it on the chart. Let’s see how we can do that. To begin, start the MetaEditor by clicking on the little icon or pressing F4 on your keyboard. The code starts by including the Trade.mqh library, which provides functions and methods for performing trade operations.

 

We then create an instance of the CTrade class called trade to execute trade operations like buying and selling. Inside the OnTick function, we calculate the current Ask and Bid prices for the symbol. These prices are essential for executing buy and sell orders. The code checks if there are less than two open positions. If true, it executes both a buy and a sell order.

 

This is just for demonstration purposes. The historical profit for the currency pair is calculated using the GetCurrencyPairHistoryProfit function and then displayed on the chart. The GetCurrencyPairHistoryProfit function calculates the total historical profit for the specific currency pair. Several variables are initialized to store information like the total number of deals, ticket numbers, order types, and profits. The HistorySelect function is used to fetch historical deal data. A for loop iterates through all the historical deals.

 

For each deal, the profit and swap for the deal are obtained, the type of order (buy or sell) is determined, and the currency pair for the deal is checked to ensure it matches the current symbol. Once you’ve written the code, press F7 to compile it. If this was too fast for you or if you don’t understand what all the code is doing, you may want to check out the premium course on our website or watch one of the basic videos first. After compiling, go back to MetaTrader by pressing F4 or clicking on the icon. In MetaTrader, press control and R to start the strategy tester.

 

Pick the expert advisor that we have just created, enable the visual mode, and start a strategy test. You should then see the expert advisor on the chart. If you’re already a premium course member and have an idea for a video like this one, please send us an email. In conclusion, you’ve now learned how to calculate and display the historical profit for a specific currency pair using MQL5. With just a few lines of MQL code, you’ve created a functional expert advisor. Thanks for watching, and I’ll see you in the next video!