Scripts: TransactionCostCollector — Broker Cost Profiling Script

 

TransactionCostCollector — Broker Cost Profiling Script:

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.

TransactionCostCollector — Broker Cost Profiling Script

Author: Patrick Murimi Njoroge

 
CopySpread returns the lowest spread registered within the bars timepan and therefor is not even remotely close to the real spread. You should use CopyTicks(range) instead. Slower but atleast free from self deception.
 
Enrique Dangeroux #:
CopySpread returns the lowest spread registered within the bars timepan and therefor is not even remotely close to the real spread. You should use CopyTicks(range) instead. Slower but atleast free from self deception.

Thank you — the criticism is largely valid, and the script has a real design gap worth addressing.

To be precise about the mechanism: CopySpread stores the spread at bar open, not the minimum spread over the bar's lifespan. The downward bias still holds, because bar opens systematically coincide with tighter-spread periods — round clock hours, session transitions — so the distribution reported understates what a trade executed mid-bar would actually incur. On H1 or higher timeframes the distortion is severe enough to make the output unreliable for cost modeling. The script compounds this by running on PERIOD_CURRENT with no timeframe enforcement, so attaching it to an H4 chart is silent self-deception, as you say.

CopyTicks is the right tool for a true spread distribution, but it comes with a constraint worth noting: most brokers retain tick history for only 7–30 days, whereas CopySpread over M1 bars can reach back months or years. A two-tier approach handles both windows — CopyTicks for recent, high-fidelity session profiling; CopySpread locked to M1 (enforced in code, not left to the chart) for the long-run mean. At M1 the open-spread bias is still present but small enough to be acceptable as a first-order cost estimate.

I will update the script with mandatory M1 enforcement and an optional tick-level mode in the next revision. Good catch.