🚀 AI One-Click Generation of mql4 Strategy Code

How to Build a Custom Trading Dashboard in Mql4 Using Only the Comment() Function

mql4 | Published on: 2025/12/5
The following content is generated by EasyQuant .

Problem Description

How do I display my account equity, balance, and current trade profit on the top-left corner of the chart?

Solution

To display account information such as Equity, Balance, and Profit on the top-left corner of the chart in MetaTrader 4, the most efficient method is using the Comment() function. This function prints text directly to the top-left corner of the chart window.

Here is the complete MQL4 code for an Expert Advisor (EA) that performs this task.

MQL4 Code Implementation

//+------------------------------------------------------------------+
//|                                       AccountInfoDisplay.mq4     |
//|                                  Copyright 2023, Quantitative AI |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Quantitative AI"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Perform an initial update when the EA is loaded
   UpdateAccountInfo();
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // Clear the comment from the chart when the EA is removed
   Comment(""); 
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // Update the display on every price tick to ensure Profit/Equity is real-time
   UpdateAccountInfo();
}

//+------------------------------------------------------------------+
//| Custom function to format and display data                       |
//+------------------------------------------------------------------+
void UpdateAccountInfo()
{
   // Retrieve account information using built-in API functions
   double balance = AccountBalance();
   double equity  = AccountEquity();
   double profit  = AccountProfit();
   
   // Format the string
   // DoubleToString is used to ensure prices show 2 decimal places
   string text = "=== Account Summary ===\n";
   text += "Balance: " + DoubleToString(balance, 2) + " " + AccountCurrency() + "\n";
   text += "Equity:  " + DoubleToString(equity, 2) + " " + AccountCurrency() + "\n";
   text += "Profit:  " + DoubleToString(profit, 2) + " " + AccountCurrency();
   
   // Display the text in the top-left corner
   Comment(text);
}

Code Explanation

  1. AccountBalance(): Retrieves the fixed balance of the account (closed trades only).
  2. AccountEquity(): Retrieves the floating equity (Balance + Floating Profit/Loss).
  3. AccountProfit(): Retrieves the current floating profit or loss of all open positions.
  4. DoubleToString(value, 2): Converts the numerical values into strings with exactly 2 decimal places for cleaner formatting.
  5. Comment(): This is the specific function that renders the text string to the top-left corner of the chart.
  6. OnTick(): We call the update function here so that the Equity and Profit values update in real-time as the market price moves.
  7. OnDeinit(): This ensures that when you remove the EA from the chart, the text disappears rather than getting stuck there.

How to Use

  1. Open MetaEditor (press F4 in MT4).
  2. Create a new Expert Advisor and name it AccountInfoDisplay.
  3. Paste the code above into the file.
  4. Click Compile.
  5. Go back to the MT4 terminal and drag the EA onto your chart.

Q&A

Q: Can I change the font size or color using Comment()?
A: No, the Comment() function uses the default system font and color. If you need custom colors, fonts, or positioning, you must use ObjectCreate() to generate OBJ_LABEL objects instead.

Q: Why does the profit not update when the market is closed?
A: The OnTick() function only triggers when a new price tick is received. If the market is closed or there is no liquidity, no ticks are generated, and the display will remain static until the next tick.

Q: Will this work if I am running another EA on the same chart?
A: If the other EA also uses the Comment() function, they will conflict, and one will overwrite the other. A chart can only have one comment block at a time. In that case, you would need to merge the logic or use graphical objects (OBJ_LABEL).