Problem Description
Solution
Here is a complete, ready-to-use MQL4 script.
How it works
- Retrieves Data: It calls standard MQL4 functions to get Balance, Equity, and Leverage.
- Calculates Margin Level: It checks if you have used margin. If you have open trades, it calculates the level
(Equity / Margin * 100). If you have no trades, it displays "N/A" to avoid division-by-zero errors. - Displays Data: It uses the
Comment()function to print the text in the top-left corner of the chart.
MQL4 Code
//+------------------------------------------------------------------+
//| AccountInfoDisplay.mq4 |
//| Copyright 2023 |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023"
#property link ""
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// --- 1. Retrieve Basic Account Data ---
double balance = AccountBalance();
double equity = AccountEquity();
long leverage = AccountLeverage();
string currency = AccountCurrency();
// --- 2. Calculate Margin Level ---
// We retrieve used margin to calculate the level manually for precision.
// Formula: (Equity / Margin) * 100
double margin = AccountMargin();
string marginLevelStr;
if(margin > 0)
{
double marginLevel = (equity / margin) * 100.0;
marginLevelStr = DoubleToString(marginLevel, 2) + "%";
}
else
{
// If margin is 0, it means no trades are open, so Margin Level is infinite/not applicable
marginLevelStr = "N/A (No Open Positions)";
}
// --- 3. Format the Output String ---
// We use StringFormat for clean alignment
string text = "========== ACCOUNT INFO ==========\n";
text += StringFormat("Balance: %.2f %s\n", balance, currency);
text += StringFormat("Equity: %.2f %s\n", equity, currency);
text += StringFormat("Margin Level: %s\n", marginLevelStr);
text += StringFormat("Leverage: 1:%d\n", leverage);
text += "==================================";
// --- 4. Display on Chart ---
Comment(text);
// Optional: Print to the "Experts" log tab as well
Print("Script executed. " + text);
}
//+------------------------------------------------------------------+
How to Install and Run
- Open MetaTrader 4.
- Press F4 to open the MetaEditor.
- In the Navigator (left side), right-click on the Scripts folder and select New File.
- Select Script, click Next, name it
AccountInfoDisplay, and click Finish. - Delete everything in the new window and paste the code above.
- Press F7 to Compile (ensure there are no errors).
- Go back to the main MT4 terminal.
- Find
AccountInfoDisplayin the Navigator -> Scripts section and drag it onto your chart.
Note on Usage
Since this is a Script, it runs once and stops. It takes a "snapshot" of your account at that exact moment. The text on the chart will remain, but the numbers will not update automatically as price moves. To update the numbers, simply drag the script onto the chart again.
To remove the text from the chart, you can create a separate script containing only the line Comment(""); or simply change the timeframe of the chart.
Q&A
How can I make the information update automatically in real-time?
To have the information update automatically with every tick (price change), the code must be written as a Custom Indicator or an Expert Advisor (EA) rather than a Script. Scripts are designed for single-execution tasks. If you move this logic into the OnCalculate() function of an Indicator or OnTick() function of an EA, the Comment() will update dynamically.
Why does Margin Level show "N/A"?
Margin Level is calculated as (Equity / Used Margin) * 100. If you have no open trades, your Used Margin is 0. Mathematically, you cannot divide by zero. Therefore, the script checks if AccountMargin() > 0. If it is 0, it displays "N/A" to indicate that the concept of a margin level is not currently applicable to your account.
Can I change the font size or color of the text?
The Comment() function used in this script is the simplest way to display text, but it does not support changing font size, color, or position (it always appears in the top-left). To customize the look, you would need to use ObjectCreate() functions to draw OBJ_LABEL objects. This requires significantly more code to manage the object coordinates and properties.