The article replaces hardcoded cost assumptions in triple-barrier labeling with measured inputs. An MQL5 script captures spread distribution, swap rates, and symbol metadata from your broker, and a Python model converts them into a broker-calibrated min ret you can pass to get events. Labels then reflect the actual round-trip friction for your instrument and holding period.
Imagine building a bridge but omitting the structure's own weight from your load calculations: the design fails for a cost that was visible from the start. The same operational blindness affects many triple‑barrier labeling pipelines. Researchers commonly set min_ret to an arbitrary constant (0.5–1%) or to legacy spread/commission assumptions, then treat every historical price move above that threshold as genuine signal. The missing step is a disciplined, repeatable way to answer: what is the actual round‑trip transaction cost for this symbol, at this broker, given my strategy's typical holding period and entry hours?
Figure 1. 2-stage pipeline from broker data collection to labeling threshold
Stage 1 (MQL5): TransactionCostCollector.mq5 runs on any chart, samples CopySpread() history, reads swap rates and symbol properties via SymbolInfoDouble(), and writes a structured CSV to the terminal's Files directory.
Stage 2 (Python): load_cost_model() parses the CSV into a TransactionCostModel, which exposes min_ret_for_symbol() for the labeling pipeline and summary() for cost inspection before committing to a threshold.
This article gives that answer. It presents a reproducible two-stage pipeline: (1) a compact MQL5 script that samples the broker's spread history, swap rates, and symbol properties and writes a structured CSV; and (2) a Python TransactionCostModel that ingests the CSV, converts all components to a common fractional‑return unit, and exposes min ret for symbol() plus diagnostic routines. The output you should expect to obtain and use is explicit: a CSV with spread percentiles and hourly means, a model object that returns per‑trade cost breakdowns, a cost‑calibrated min ret ready for get events(), and the same parameters to pass into your per‑trade P&L calculation. Note: min ret here is a labeling threshold (for training data construction), not the execution profit‑taking target an EA will place in the terminal.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Check out the new article: MetaTrader 5 Machine Learning Blueprint (Part 14): Transaction Cost Modeling for Triple-Barrier Labels in MQL5.
Imagine building a bridge but omitting the structure's own weight from your load calculations: the design fails for a cost that was visible from the start. The same operational blindness affects many triple‑barrier labeling pipelines. Researchers commonly set min_ret to an arbitrary constant (0.5–1%) or to legacy spread/commission assumptions, then treat every historical price move above that threshold as genuine signal. The missing step is a disciplined, repeatable way to answer: what is the actual round‑trip transaction cost for this symbol, at this broker, given my strategy's typical holding period and entry hours?
Figure 1. 2-stage pipeline from broker data collection to labeling threshold
This article gives that answer. It presents a reproducible two-stage pipeline: (1) a compact MQL5 script that samples the broker's spread history, swap rates, and symbol properties and writes a structured CSV; and (2) a Python TransactionCostModel that ingests the CSV, converts all components to a common fractional‑return unit, and exposes min ret for symbol() plus diagnostic routines. The output you should expect to obtain and use is explicit: a CSV with spread percentiles and hourly means, a model object that returns per‑trade cost breakdowns, a cost‑calibrated min ret ready for get events(), and the same parameters to pass into your per‑trade P&L calculation. Note: min ret here is a labeling threshold (for training data construction), not the execution profit‑taking target an EA will place in the terminal.
Author: Patrick Murimi Njoroge