Triple-barrier labeling pipelines frequently use an arbitrary constant (0.5–1.0%) or a legacy spread assumption as the min_ret threshold. A threshold set below the actual round-trip transaction cost causes the pipeline to label cost-driven noise as tradeable signal. The labeled dataset then systematically overstates edge, and any model trained on those labels overfits to an artifact of the labeling scheme rather than to genuine market structure.
TransactionCostCollector.mq5 is a standalone script that resolves the data-collection step of this problem. Version 3.00 samples the spread distribution directly from tick data via CopyTicksRange() — not from bar-resolution spread history — reads the broker's swap rates, symbol and execution properties, and commission diagnostics for the attached chart symbol, and exports everything to a structured CSV file. The CSV is consumed by the companion Python class TransactionCostModel, which converts all costs to fractional returns and computes the instrument-specific min_ret threshold for your labeling call.

Three-panel illustration of TransactionCostCollector output: intraday cost breakdown by session (a), full cost distribution (b), and percentile curve for min_ret selection (c)
The script collects the following for a single symbol in one run:
| Parameter | Default | Description |
|---|---|---|
| InpDays | 30 | Lookback window in calendar days. Ticks are fetched one day at a time across the window; a longer window gives a more representative distribution across sessions but takes longer to run and needs more tick history held by the terminal. |
| InpExecTF | PERIOD_H1 | Execution timeframe used only for the execution-conditional spread distribution — the prevailing quote at each boundary of this timeframe. Independent of the chart's own timeframe, which no longer affects sampling now that spread comes from ticks rather than bars. |
| InpAuditBarField | true | When true, also reconstructs each M1 bar from its own ticks and audits it against the broker-stored M1 spread field. Set false to skip this (slightly faster; unnecessary once you've confirmed the field's behavior for a given broker and terminal build). |
| InpMaxDwellSec | 60 | Cap, in seconds, on how long a quote is assumed to prevail before the next tick. Used by the time-weighted distribution and the execution-boundary check, so a single weekend or holiday gap can't dominate either one. |
| InpRetryMs | 200 | Sleep, in milliseconds, between CopyTicksRange() retries for a given day when the terminal is still fetching history rather than reporting a genuinely empty day. |
| InpMaxRetries | 25 | Retries per day before that day is declared to have no usable ticks and skipped. |
| InpOutputFile | (blank) | Override the output filename. When blank (default), the file is named <SYMBOL>_costs.csv — for example, EURUSD_costs.csv for EURUSD. The file is written to MQL5\Files\ in the terminal data folder (FILE_WRITE | FILE_CSV mode). |
The CSV uses a five-column structure: section, key, value, unit, note. Sections are:
The companion Python class TransactionCostModel loads the CSV and computes min_ret for triple-barrier labeling:
from afml.transaction_costs import load_cost_model import pandas as pd model = load_cost_model( csv_path = "EURUSD_costs.csv", spread_section = "spread_exec_conditional", # default; matches this script's own recommendation spread_percentile = "p95_pips", # conservative slippage_pips = 0.4, commission_per_lot = 7.0, # from reference trade lot_size = 0.01, ) close = pd.read_parquet("EURUSD_H1.parquet")["close"] min_ret = model.min_ret_for_symbol( price_series = close, holding_days = 1.0, cost_multiplier = 1.5, # 1.5x break-even ) print(f"min_ret = {min_ret:.6f}")
load_cost_model() raises a specific error if it's pointed at a CSV from v1.00/v2 — it will not silently accept the old spread_summary section. swap_cost_frac() also recognizes every swap-mode string this version of the collector writes, including reopen_current/reopen_bid, for which it raises rather than approximating (those modes recompute swap by reopening the position at a new reference price each night, which no fixed per-night rate models correctly).
The per-lot commission rate cannot be read programmatically on all brokers. The recommended procedure is: open a reference trade of exactly 1.0 standard lot on a demo account, read ACCOUNT_COMMISSION_BLOCKED from the terminal immediately after the trade opens, then close the trade. The blocked value divided by 1.0 gives the per-lot rate. Confirm against the account statement. This needs to be done once per broker relationship.
The mean spread is dominated by quiet periods: Asian session, overnight consolidation, and orderly trending conditions. Strategy entries, however, often coincide with the moments of highest uncertainty (which is when models generate signals). Using the 95th-percentile spread — read from spread_exec_conditional by default — as min_ret input accounts for the cost environment at typical entry conditions rather than at average market conditions.
Spread Monitor
Allows to monitor spread and swaps!
Institutional Harmonic Volumetric Gravity Center
A quantitative volume density engine utilizing weighted Harmonic Mean mathematics to eliminate arithmetic outliers and map the true institutional liquidity center of gravity.
Heiken ashi as series
Heiken ashi with all buffers set as series
TickValue_Compare - diagnose differences between TICK_VALUE, LOSS and PROFIT
Diagnostic script that compares SYMBOL_TRADE_TICK_VALUE, SYMBOL_TRADE_TICK_VALUE_LOSS and SYMBOL_TRADE_TICK_VALUE_PROFIT for every symbol in Market Watch. Classifies each symbol into one of four categories (ALL_EQUAL, TV_MATCHES_PROFIT, TV_MATCHES_LOSS, ALL_DIFFER) and provides an aggregated summary plus interpretation tip. Useful for verifying which tick-value property to rely on when implementing risk-based lot sizing in EAs. Exports full per-symbol report to CSV in MQL5/Files.