I spent last weekend trying to build a proper equity curve for my 2025 trades. The built-in "Account History" tab in MT4 is fine for a quick glance, but try exporting that data to build a proper chart in Excel or Python and you'll quickly hit a wall.
The default right-click > "Save as Report" gives you an HTML file. It looks pretty, but parsing that mess to get clean equity data is a nightmare. I needed raw OHLC of my balance changes.
Method 1: The Built-in Report (The Wrong Way)
If you just want a quick statement, right-click the History tab, select "Save as Report". This generates an HTM file. Open it in Excel, but you'll spend 20 minutes cleaning up merged cells and formatting before you can even start calculating drawdown. It's fine for auditors, not for analysts.
Method 2: The Script Way (The Right Way)
This is where MQL4 shines. After digging through the MQL4 Documentation on History Functions, I realized the
HistorySelect() and HistoryDealGetDouble() functions give us direct access to raw data. I wrote a quick script that loops through all closed orders and prints a clean CSV with timestamp, balance, and equity.Here is the snippet that saved my weekend:
``
mql4
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
int total = HistoryDealsTotal();
if(total == 0) { Print("No history found."); return; }
int handle = FileOpen("EquityHistory.csv", FILE_WRITE|FILE_CSV, ",");
if(handle == INVALID_HANDLE) { Print("File open failed"); return; }
FileWrite(handle, "Time", "Balance", "Equity", "Profit");
for(int i = 0; i < total; i++)
{
ulong ticket = HistoryDealGetTicket(i);
datetime dt = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME);
double balance = HistoryDealGetDouble(ticket, DEAL_BALANCE);
double equity = HistoryDealGetDouble(ticket, DEAL_EQUITY);
double profit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
if(balance > 0 || equity > 0) // Filter out zero entries
FileWrite(handle, TimeToString(dt), DoubleToString(balance, 2), DoubleToString(equity, 2), DoubleToString(profit, 2));
}
FileClose(handle);
Print("Exported ", total, " deals to EquityHistory.csv");
}
`
The Hidden Tip
One thing the official docs don't tell you: DEAL_EQUITY is not stored for every single deal. It only gets recorded on closed positions. If you want a continuous equity curve that updates during open trades, you need to use AccountInfoDouble(ACCOUNT_EQUITY) in an EA running on a 1-minute chart and log it separately. I learned this after three failed attempts to backfill a smooth curve.
Importing to Excel
After running the script, go to MQL4\Files` folder, grab the CSV. In Excel, use Data > From Text/CSV and set delimiter to comma. Insert a line chart with "Time" as the X-axis and "Equity" as the series. That gives you your true equity curve in under 2 minutes.Reference: MQL4 Documentation - History Functions (docs.mql4.com).
This article was originally published on FXEAR.com. All rights reserved. Unauthorized reproduction is prohibited.