Skip to content
All library documents

Updating Cointegrated Stock Basket Weights in Real Time

Article MQL5 articles

Summary

This installment explains how to update a cointegrated stock portfolio’s weights while an Expert Advisor is running. It uses a database as the interface between Python analysis and MQL5 trading: a strategy table stores basket symbols, weights, and timeframe, while a unified Python pipeline refreshes market data and runs correlation, cointegration, and stationarity tests. The EA can read the latest strategy parameters at closed-bar intervals, keeping analysis and execution activities decoupled.

The article describes schema choices, data flow, and software organization, including moving EA functions into a header and retaining event handlers in the main file. It presents an example basket and a complete pipeline as implementation guidance, but the supplied text does not provide performance results for live or backtested trading. The framework is described as a resource-conscious retail setup; database portability and the accuracy of statistical estimates remain practical considerations, and changing weights do not by themselves guarantee profitable mean reversion.

Key ideas

  • The system recalculates basket weights as new closed-bar data becomes available.
  • A SQLite strategy table passes symbols, weights, and timeframe from analysis tools to the trading EA.
  • A unified Python process performs correlation, cointegration, and stationarity checks and refreshes database inputs.
  • Separating analysis from execution makes updated strategy parameters available without embedding every test in the EA.
  • The article focuses on engineering workflow and does not establish profitability or live trading performance.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.