Python is a strong research environment for cleaning data, estimating relationships, testing features, and comparing models. MT5 is the execution environment that must make a timely decision from broker data and handle real orders. A reliable workflow treats the boundary between them as a contract, not as an informal file exchange.
Cointegration and machine learning illustrate the same challenge in different ways. A statistical relationship can decay, and a predictive model can look convincing after leakage or repeated tuning. Neither should reach execution until its inputs, timing, validation, and failure behaviour are reproducible.
From research notebook to live decision
Each stage produces an artifact that the next stage can verify.
- 01DataFix symbols, timezone, bar policy, corporate or contract adjustments, and missing-data rules.
- 02ResearchEstimate relationships or features without future leakage and retain out-of-sample periods.
- 03ContractFreeze model version, inputs, transformations, outputs, timestamps, and fallback behaviour.
- 04ExecuteReproduce the decision in MT5, validate freshness, apply risk permission, and log the result.
Start with the data contract
Research and execution must agree on price source, timezone, bar close, spread treatment, symbol mapping, missing values, and calculation order. A model trained on clean midpoint bars can behave differently when MT5 receives bid/ask ticks, incomplete history, or a broker-specific session boundary.
Cointegration is a monitored relationship
A stationary spread in one sample is not a permanent trading law. Pair selection, hedge-ratio estimation, test windows, structural breaks, transaction costs, and re-estimation policy all affect the result. The operational question is not only whether a relationship existed, but when the live system must declare it stale.
Machine learning needs a time boundary
Feature scaling, target construction, cross-validation, and hyperparameter search must respect chronology. Random splits can leak market regimes across training and validation. Repeatedly selecting the best backtest also creates a hidden test set made from the researcher’s own decisions.
Deploy a reproducible artifact
The live system should receive a frozen model or an exact decision output with a version, timestamp, feature schema, and expiry. It also needs a safe response to stale files, unavailable Python processes, malformed values, or a model that lies outside its approved operating conditions.
- Version the dataset, feature code, model, and MT5 adapter together.
- Recreate research features from MT5 data before deployment.
- Use chronological validation and untouched final periods.
- Define freshness and fallback rules before live execution.
- Monitor input drift, relationship decay, and decision parity.
Questions you may have
Does MT5 need to run Python for every decision?
No. Models can be exported, signals can be exchanged through a controlled interface, or Python can run as a separate service. The correct choice depends on latency, reliability, and maintenance needs.
Does cointegration prove two assets will converge?
No. It describes a historical statistical relationship under specific assumptions. The relationship can weaken or break, and trading costs can remove any apparent advantage.