transaction cost collector: a script for accurate broker cost profiling

transaction cost collector: a script for accurate broker cost profiling
Download ALL MT5 scripts (351)
YouTube Video Thumbnail



Similar MetaTrader Tools

transaction cost collector: a script for accurate broker cost profiling

Info

The transaction cost collector: a script for accurate broker cost profiling is a Script for MetaTrader 5 that the transaction cost collector is a powerful tool for addressing a critical issue in triple-barrier labeling pipelines. these pipelines often rely on arbitrary constants or outdated spread assumptions for the min_ret threshold, which can lead to inaccurate labeling and model overfitting.

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 transaction cost collector: a script for accurate broker cost profiling

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

the transaction cost collector is a powerful tool for addressing a critical issue in triple-barrier labeling pipelines.

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: Cost and Slippage Sensitivity Analyzer - another powerful script for MetaTrader 5 traders.

the transaction cost collector is a powerful tool for addressing a critical issue in triple-barrier labeling pipelines. these pipelines often rely on arbitrary constants or outdated spread assumptions for the min_ret threshold, which can lead to inaccurate labeling and model overfitting.

Also recommended: Real Cost XAU Session Cost Snapshot MT5 - similar script with strong performance on MetaTrader 5.


transactioncostcollector.mq5 is designed to address this challenge by providing a comprehensive solution for data collection. it samples historical spread distributions, retrieves broker swap rates, and gathers commission diagnostics for a specific chart symbol. the collected data is then exported to a structured csv file, ensuring a systematic approach to cost analysis.

## what the script collects

the script efficiently gathers three essential cost components for a single symbol in one run:


### spread

- historical distribution using copyspread() across a specified bar count. results are presented as mean, standard deviation, and percentiles (p25, p50, p75, p90, p95, p99) in pips. additionally, the script calculates the mean spread for each utc hour to reveal session-specific variations.


### swap

- long and short overnight swap rates are directly sourced from symbol_swap_long and symbol_swap_short, along with the swap mode (points, currency, interest) and the triple-swap weekday.


### commission

- the script provides a diagnostic approach to commission calculation, as mql5 does not universally expose per-lot commission as a direct api call. it records account_commission_blocked and offers guidance on deriving the per-lot rate from a reference trade.


## input parameters and csv output format

the csv output is structured with four columns: section, key, value, and unit. sections include symbol_properties, swap, commission, spread_summary, and spread_by_hour, each containing relevant cost-related data.


## how to run

- place transactioncostcollector.mq5 in the mql5\scripts\downloads\ folder and compile it in metaeditor.
- open a chart of the target symbol on any timeframe. h1 is recommended for a balance between granularity and history length.
- drag the script onto the chart to access the input dialog. set inpbars to the desired sample count and click ok.
- retrieve the generated csv from mql5\files\ in the terminal data folder.


## integrating with python

the python class transactioncostmodel is designed to work seamlessly with the csv output. it calculates min_ret for triple-barrier labeling, ensuring that models are trained on accurate data.


## notes on commission

to address the challenge of per-lot commission rates, the script recommends a practical approach: open a reference trade of 1.0 standard lot on a demo account, read account_commission_blocked, and then close the trade. this method provides a reliable way to derive the per-lot rate, which is essential for accurate cost profiling.


## why p95 is preferred over the mean

the mean spread, while useful, is heavily influenced by calm market periods. however, trading strategies often activate during moments of heightened uncertainty, which is when models generate signals. by using the 95th-percentile spread as the min_ret input, the script accounts for the cost environment at these critical entry points, ensuring more accurate labeling.


## references and companion article

for further exploration, refer to lópez de prado's 2026 book, chapter 3 (labels), pages 44–47. the full implementation and derivation are provided by patrick m. njoroge, along with the companion python class transactioncostmodel and usage examples in the article download package.

You may also like: key finder - excellent alternative for script users on MetaTrader 5.

Source Code

#property script_show_inputs
input int    inpbars        = 50000;
input string inpoutputfile  = "";
#define csv_sep ","
void writecsvrow(int    handle,
string section,
string key,
string value,
string unit,
string note);

.......

Leave your opinion, ask a question, share some knowledge

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.
© ROBOTFX Free educational tools by RobotFX. Use entirely at your own risk; we are not liable for any financial losses incurred.