Generate code in 3 minutes
Enter your idea and AI generates runnable code.
Here is a complete, ready-to-use MQL4 script.
(Equity / Margin * 100). If you have no trades, it displays "N/A" to avoid division-by-zero errors.Comment() function to print the text in the top-left corner of the chart.//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+
AccountInfoDisplay, and click Finish.AccountInfoDisplay in the Navigator -> Scripts section and drag it onto your chart.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.
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.
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.
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.