Problem Description
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
AccountBalance(): Retrieves the fixed balance of the account (closed trades only).AccountEquity(): Retrieves the floating equity (Balance + Floating Profit/Loss).AccountProfit(): Retrieves the current floating profit or loss of all open positions.DoubleToString(value, 2): Converts the numerical values into strings with exactly 2 decimal places for cleaner formatting.Comment(): This is the specific function that renders the text string to the top-left corner of the chart.OnTick(): We call the update function here so that the Equity and Profit values update in real-time as the market price moves.OnDeinit(): This ensures that when you remove the EA from the chart, the text disappears rather than getting stuck there.
How to Use
- Open MetaEditor (press F4 in MT4).
- Create a new Expert Advisor and name it
AccountInfoDisplay. - Paste the code above into the file.
- Click Compile.
- 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).