Trade History Exporter: Detailed Closed Trade Analysis to CSV
Info
The Trade History Exporter: Detailed Closed Trade Analysis to CSV is a Script for MetaTrader 5 that this tool reinterprets metatrader's deal history to provide a comprehensive trade-by-trade analysis in a csv format. The standard terminal history displays individual deals, not complete trades.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Script works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Scripts folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use Trade History Exporter: Detailed Closed Trade Analysis to CSV
1. Installation: Move your script file into the MQL/Scripts directory and restart the platform.
2. Execution: Drag the script onto a chart; it will perform a one-time action, such as closing all open orders or clearing chart objects.
3. Editing: Use MetaEditor (F4) to modify code, click "Compile," and verify no errors appear in the terminal before running.
4. Removing: Scripts stop automatically, but you can remove them manually by right-clicking the chart and choosing "Remove Script."
Frequently Asked Questions
Q: How are scripts different from EAs? A: Scripts execute a single action and then stop; EAs monitor the market and trade continuously.
Q: Can I assign a hotkey to a script? A: Yes, right-click the script in the Navigator, select "Set Hotkey," and define your preferred keyboard shortcut.
Q: Why did my script stop? A: Scripts are designed to stop immediately after finishing their programmed command; this is normal behavior.
What this tool does
This tool reinterprets MetaTrader's deal history to provide a comprehensive trade-by-trade analysis in a CSV format.
Typical Use Case
This Script excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Script works on MetaTrader 5. Place the file in the MQL5/Scripts folder and restart the terminal.
Description & Settings
Related: Trade History Stats - another powerful script for MetaTrader 5 traders.
This tool reinterprets MetaTrader's deal history to provide a comprehensive trade-by-trade analysis in a CSV format.Also recommended: Export Closed Trades to CSV with Summary by Magic Number - similar script with strong performance on MetaTrader 5.
The standard terminal history displays individual deals, not complete trades. Trade History Exporter reconstructs each closed trade from its constituent deals, generating a single, informative row per trade in a CSV file that can be easily imported into spreadsheet software.
Exported Columns Include:
- Position ID
- Symbol
- Trade Type (Buy/Sell)
- Volume Traded
- Open Time (Server Time)
- Open Price
- Close Time (Server Time)
- Close Price
- Points Gained/Lost
- Profit
- Commission
- Swap Fees
- Other Fees
- Net Profit/Loss
- Trade Duration
- Initial Stop Loss Level
- R Multiple (Gross)
- Maximum Adverse Excursion (MAE) in Points
- Maximum Favorable Excursion (MFE) in Points
- Magic Number
- Comment
Handling Partial Closes:
A trade closed in multiple parts will be consolidated into a single row in the output. Open and close prices are calculated as volume-weighted averages across all deals within the trade.
Calculation of Financials:
Commission, swap, and any other fees associated with all deals in a trade are aggregated. The 'Net' column accurately reflects the final profit or loss after all costs.
R Multiple (Gross) Calculation:
This is calculated as the total points won or lost divided by the initial risk (points between the open price and the stop loss at the time of opening). This value is left blank if the trade was opened without a stop loss.
MAE and MFE Calculation:
Maximum Adverse Excursion (MAE) and Maximum Favorable Excursion (MFE) represent the most significant price movements against and in favor of the trade, respectively, measured in points. These are derived from 1-minute bars between the trade's open and close times. Please note this is an approximation: the first and last minute of data might include prices just before entry or just after exit. Also, as bars are based on Bid prices, the spread is not accounted for in Sell trades. MAE and MFE will be blank if 1-minute history is unavailable for the entire trade duration.
Data Formatting:
Dates are formatted as yyyy-mm-dd hh:mm:ss (server time), ensuring compatibility with spreadsheet date recognition. Monetary values are presented using the account's currency decimal precision.
File Output Details:
The CSV file is saved using UTF-8 encoding with a Byte Order Mark (BOM). Comments starting with '=', '+', '-', or '@' are prepended with a single quote ('). This prevents spreadsheets from interpreting them as formulas. Trades are listed in the chronological order they were opened.
User Inputs:
- Trades closed in the last N days: Specify a number of days, or '0' to include all historical trades.
- Symbol Scope: Choose to export trades for 'all symbols' or 'this chart's symbol only'.
- Magic Number Filter: Use '-1' for any magic number, '0' for trades without a magic number (typically manual trades), or enter a specific magic number to filter trades.
- MAE/MFE Calculation from M1 bars: 'True' (default) enables calculation using 1-minute bars. Set to 'False' for faster exports, especially with extensive history.
- Output File Name: Define a custom name for the exported CSV file.
- Save in Common Files Folder: Select 'True' to save the file in the shared 'Commoniles' directory accessible by all MetaTrader terminals, or 'False' to save it in the current terminal's MQL5\Files folder.
- Column Separator: Choose between comma ',', semicolon ';', or tab '\t' as the delimiter for the CSV file.
- Decimal Comma Usage: Set to 'True' to use a comma as the decimal separator (e.g., 1,0950) instead of a period (e.g., 1.0950). This option is active only when using semicolon or tab separators, and is intended for spreadsheets configured to use a decimal comma.
Completion Notification:
Upon completion, a message will display the full path to the generated file, the total number of trades analyzed, the count of profitable trades, and the overall net result. The earliest trade recorded for analysis is from 2026.
You may also like: MetaTrader Backtest Data Validation: Broker Bar and Tick History Check - excellent alternative for script users on MetaTrader 5.
Source Code
#property copyright "RobotFX"
#property version "1.00"
#property description "This code is educational. Feel free to use, modify and upgrade as you wish. Visit ROBOTFX.ORG for professional trading tools"
#property script_show_inputs
#include <Generic\HashMap.mqh>
enum ENUM_SYMBOL_FILTER
{
SYMBOLS_ALL=0,
SYMBOLS_CHART=1
};
.......
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.