Trade History in R: Comprehensive Analysis
Info
The Trade History in R: Comprehensive Analysis is a Script for MetaTrader 5 that understanding trading performance goes beyond simple monetary profit. A $100 profit, for example, could represent a well-executed trade with a tight stop-loss or a lucky outcome with a wide one.
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 in R: Comprehensive Analysis
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
Understanding trading performance goes beyond simple monetary profit.
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 Cost Analysis: Commission, Swap, and Fees by Symbol - another powerful script for MetaTrader 5 traders.
Understanding trading performance goes beyond simple monetary profit. A $100 profit, for example, could represent a well-executed trade with a tight stop-loss or a lucky outcome with a wide one.Also recommended: Risk Lot Calculator: Accurate Trade Sizing - similar script with strong performance on MetaTrader 5.
Measuring each trade in R, which is the profit divided by the initial risk, allows for a standardized comparison across trades with varying volumes, symbols, and stop-loss levels. An expectancy of +0.2 R per trade signifies consistent performance regardless of account size, whether it's $1,000 or $100,000.This script analyzes the closed positions on your account. It identifies the initial stop-loss for each position and presents detailed statistics in terms of R on a chart panel. This is a read-only tool; it does not open, modify, or close any trades.
Reported Statistics
- Number of closed positions, including those with a stop-loss and those without (risk unknown).
- Net result of all positions in the account currency.
- Total R, expectancy (average R per trade), win rate, profit factor, best trade, and worst trade.
- Maximum drawdown in R and the longest losing streak.
- Statistics broken down by symbol.
Input Parameters
- Days back: Analyze history from the last specified number of days (0 for the entire history).
- Magic number: Filter trades by Expert Advisor magic number (-1 for all trades, 0 for manual trades only, or a specific magic number).
- Symbol: Filter trades for a specific trading symbol (leave empty for all symbols).
- Export to CSV: Save detailed trade data (initial stop, risk, net result, R) to MQL5\Files\TradeHistoryInR.csv.
- Show the panel: Display the analysis panel on the chart (run the script again to refresh).
Calculating R
The initial risk (R) is determined by the initial stop-loss set at the time of the trade entry (DEAL_SL). If DEAL_SL is not available, the stop-loss of the order that opened the position is used. Subsequent adjustments to the stop-loss, such as moving to breakeven or implementing a trailing stop, do not affect the initial R calculation. The monetary risk is calculated based on the loss at the initial stop-loss for the opening volume, using OrderCalcProfit. If unavailable, the tick value is used as a fallback. The final result incorporates swap, commission, and fees. For cross-currency symbols, OrderCalcProfit uses current exchange rates, meaning the risk for older trades may be an approximation.
Verification
To ensure accuracy, a test was conducted using the Strategy Tester. An Expert Advisor opened 369 random trades on XAUUSD H1, with a stop-loss set at 1 ATR and a take-profit at 2 ATR. The script correctly reported trades closed at the stop-loss as -1.00 R (median) and trades closed at the take-profit as +2.00 R (median), aligning with expectations. The accompanying image displays the results of this test run.
You may also like: Trade List Report: A Pure MQL5 PDF Generator - excellent alternative for script users on MetaTrader 5.
Source Code
#property copyright "RobotFX"
#property link "https://robotfx.org"
#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
input int InpDays = 0;
input long InpMagic = -1;
input string InpSymbol = "";
input bool InpCsv = true;
input bool InpPanel = true;
.......
⚠ 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.