Problem Description
Solution
Here is the complete MQL4 code to create a dashboard on your chart displaying Account Balance, Spread, and Open Profit.
I have implemented this using Graphical Objects (OBJ_LABEL) rather than the simple Comment() function. This allows for better formatting, positioning, and font control, and it ensures the text doesn't overlap with other indicators that might use the comment corner.
MQL4 Code
Save this code as an Expert Advisor (e.g., InfoDisplay.mq4) or an Indicator.
//+------------------------------------------------------------------+
//| InfoDisplay.mq4 |
//| Copyright 2023, Quantitative|
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Quantitative"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//--- Input Parameters
input color InpColorBase = clrWhite; // Base Text Color
input color InpColorProfit = clrLime; // Profit Color (Positive)
input color InpColorLoss = clrRed; // Profit Color (Negative)
input int InpFontSize = 10; // Font Size
input string InpFontName = "Arial"; // Font Name
input int InpX = 20; // X Distance from corner
input int InpY = 20; // Y Distance from corner
input int InpYStep = 20; // Vertical spacing between lines
input ENUM_BASE_CORNER InpCorner = CORNER_LEFT_UPPER; // Chart Corner
//--- Global Constants for Object Naming
const string Prefix = "InfoPanel_";
const string NameBalance = Prefix + "Balance";
const string NameSpread = Prefix + "Spread";
const string NameProfit = Prefix + "Profit";
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create the labels immediately upon loading
UpdateDisplay();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Clean up all objects created by this tool
ObjectsDeleteAll(0, Prefix);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Update the values on every tick
UpdateDisplay();
}
//+------------------------------------------------------------------+
//| Custom function to update the dashboard |
//+------------------------------------------------------------------+
void UpdateDisplay()
{
// 1. Get Data
double balance = AccountBalance();
double profit = AccountProfit();
// Get spread in points (integer)
long spread = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
// 2. Format Strings
string strBalance = "Balance: " + DoubleToString(balance, 2) + " " + AccountCurrency();
string strSpread = "Spread: " + IntegerToString(spread) + " pts";
string strProfit = "Open Profit: " + DoubleToString(profit, 2) + " " + AccountCurrency();
// 3. Determine Profit Color
color currentProfitColor = InpColorBase;
if(profit > 0) currentProfitColor = InpColorProfit;
else if(profit < 0) currentProfitColor = InpColorLoss;
// 4. Draw/Update Labels
// Balance (Line 1)
CreateLabel(NameBalance, strBalance, InpX, InpY, InpColorBase);
// Spread (Line 2)
CreateLabel(NameSpread, strSpread, InpX, InpY + InpYStep, InpColorBase);
// Profit (Line 3)
CreateLabel(NameProfit, strProfit, InpX, InpY + (InpYStep * 2), currentProfitColor);
// Force chart redraw to ensure smooth updates
ChartRedraw();
}
//+------------------------------------------------------------------+
//| Helper function to create or update a label object |
//+------------------------------------------------------------------+
void CreateLabel(string name, string text, int x, int y, color clr)
{
// Check if object exists, if not create it
if(ObjectFind(0, name) < 0)
{
ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0);
// Set static properties (only need to be set once)
ObjectSetInteger(0, name, OBJPROP_CORNER, InpCorner);
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, InpFontSize);
ObjectSetString(0, name, OBJPROP_FONT, InpFontName);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); // Hide from object list
}
// Update dynamic properties
ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
ObjectSetString(0, name, OBJPROP_TEXT, text);
ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
}
//+------------------------------------------------------------------+
Key Features of This Code
- Dynamic Profit Coloring: The "Open Profit" text changes color automatically: Green (Lime) if you are in profit, Red if you are in a loss, and White (Base) if it is 0.
- Clean Cleanup (
OnDeinit): When you remove the EA from the chart, it automatically deletes the text labels so they don't get stuck on your screen. - Customizable Inputs: You can change the X/Y coordinates, font size, and colors directly from the EA settings window without changing the code.
- Efficiency: It uses
SymbolInfoIntegerfor the spread, which is the modern and robust way to retrieve symbol properties in MQL4.
How to Use
- Open MetaTrader 4.
- Press F4 to open the MetaEditor.
- Click New -> Expert Advisor (template) -> Name it
InfoDisplay. - Paste the code above into the file, replacing everything currently there.
- Press F7 or click Compile.
- Go back to the main MT4 terminal.
- Find
InfoDisplayin your Navigator panel under Expert Advisors. - Drag and drop it onto your chart.
Q&A
Q: Why does the spread number jump around?
A: The spread is dynamic on most brokers. It changes with every tick based on market liquidity. The display updates on every tick (OnTick), so you see the real-time spread.
Q: Can I use this while another EA is trading?
A: Yes. However, a chart can only have one Expert Advisor attached at a time. If you want to use this alongside a trading EA, you should save this code as a Custom Indicator instead of an Expert Advisor (move the file to the MQL4/Indicators folder). The code is compatible with both formats.
Q: Does "Open Profit" include closed trades?
A: No. AccountProfit() returns the floating profit/loss of currently open positions only. AccountBalance() shows your closed, realized balance. To see the total (Balance + Floating), you would calculate AccountEquity().