Cost and Slippage Sensitivity Analyzer
Info
The Cost and Slippage Sensitivity Analyzer is a Script for MetaTrader 5 that a backtest charges only the costs you assume. Net profit and profit factor are measured at that cost, so they never show how close the edge sits to the cost line.
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 Cost and Slippage Sensitivity Analyzer
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
A backtest charges only the costs you assume.
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: Real Cost XAU Session Cost Snapshot MT5 - another powerful script for MetaTrader 5 traders.
A backtest charges only the costs you assume. Net profit and profit factor are measured at that cost, so they never show how close the edge sits to the cost line. This tool answers the question directly: how much execution cost can the strategy absorb before its edge is gone?Also recommended: transaction cost collector: a script for accurate broker cost profiling - similar script with strong performance on MetaTrader 5.
The script reads a Date,Profit,Volume file (one row per closing deal) and prints a full report in the Experts tab:
Breakeven cost per deal
— the cost level at which the net profit reaches zero (the average edge per deal).
Cushion
— the multiple of an assumed realistic cost that the edge can absorb before the net hits zero.
Net and profit factor at the assumed cost
— the whole record re-priced, plus a cost-sensitivity curve from 0 to 3 times the assumed cost.
Win erosion
— the winning deals that the assumed cost turns into losers.
A composite score (A+ to F)
combining the cushion, the profit-factor resilience, and the win erosion, with written recommendations.
Input.
Place a CSV named in the input parameter (default Trades.csv) in the terminal MQL5\Files folder, with the header Date,Profit,Volume and one row per closing deal. The Volume column is optional: if it is missing, the cost is applied per deal rather than per lot. On the first run, if no file is found, the script generates a reproducible sample and analyzes it, so the output is visible immediately. The helper ExportCost.mq5 writes the file from the trade history: copy its function into your Expert Advisor and call it from OnTester(), or run it as a standalone script.
Parameters.
InpFileName (the CSV to read); InpCostPerLot and InpCostFixed (the assumed per-lot and per-deal cost); InpCushionTarget (the cushion that scores full marks); InpThinMult (the thin-winner threshold); InpErosionTol (the win-erosion tolerance); InpWeightCushion, InpWeightPF, InpWeightErosion (the three dimension weights, normalized automatically).
The tool is written in pure MQL5. It needs no external libraries, no Python and no DLLs.
========================================
COST & SLIPPAGE SENSITIVITY ANALYZER v1.0
How much execution cost can the edge absorb?
========================================
Note: this works at the closing-deal level. A position closed in several
deals (partial exits) is several rows, and each exit deal pays its own cost.
Net profit is not positive before cost; the analysis needs a profitable set.
=== STRATEGY (before added cost) ===
Deals: 200 (39 winners, 104 losers, 57 flat)
Win rate: 19.5%
Net profit: -744.90
Profit factor: n/a (no losing deals)
Total volume: 70.50 lots (avg 0.35 per deal)
Edge per deal: -3.72
=== ASSUMED COST: 12.00 per lot + 0.00 fixed (0.00 per deal on average) ===
Cost-sensitivity curve (net profit as the assumed cost is scaled up):
xCost Cost/deal Net ProfitFactor |
0.0 0.00 -744.90 n/a |
0.5 0.00 -744.90 n/a |
1.0 0.00 -744.90 n/a |
1.5 0.00 -744.90 n/a |
2.0 0.00 -744.90 n/a |
2.5 0.00 -744.90 |
3.0 0.00 -744.90 n/a |
=== COST SENSITIVITY ===
Breakeven cost per deal: -3.72 (the edge per deal)
Cushion: 0.00x the assumed cost
Net at the assumed cost: -744.90 (0% of the original net)
Profit factor at cost: n/a (no losing deals)
Winners erased at cost: 0 of 39 (0.0%)
Thin-winner gross share: 0.0%
(One-sided: only cost rises, so the net and the profit factor are meant to fall.)
========================================
COST-ROBUSTNESS SCORE (heuristic)
========================================
Cushion score: 0.0 / 100
PF-resilience: 100.0 / 100
Win-erosion: 100.0 / 100
----------------------------------------
COMPOSITE: 54.9 / 100
GRADE: D
(Heuristic for ranking and quick comparison, not a statistically validated measure.)
========================================
--- RECOMMENDATIONS ---
!! The edge disappears below 2x the assumed cost (cushion 0.00x). Treat live results with great caution.
Analysis complete.
You may also like: close-all - excellent alternative for script users on MetaTrader 5.
Source Code
#property copyright "RobotFX"
#property version "1.00"
#property script_show_inputs
#property description "This code is educational. Feel free to use, modify and upgrade as you wish. Visit ROBOTFX.ORG for professional trading tools"
input string InpFileName = "Trades.csv";
input double InpCostPerLot = 12.0;
input double InpCostFixed = 0.0;
input double InpCushionTarget = 3.0;
input double InpThinMult = 1.0;
input double InpErosionTol = 0.20;
.......
⚠ 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.