
Intro
Anyone building quantitative backtesting or real-time market scrapers for Hong Kong Stock Connect will inevitably run into one annoying pitfall: incorrect effective dates for index constituent updates.
Early in my development work, I hardcoded eligible stock symbols directly into config files. Every quarterly rebalance would break my dataset — newly added stocks got included too early, while removed tickers lingered in calculations and distorted backtest outcomes entirely.
After researching exchange rules and testing multiple data sourcing methods, I’ve settled on using AllTick’s WebSocket API to dynamically pull constituent changes and their official effective timestamps. This post breaks down exchange scheduling rules, compares data acquisition approaches, and shares fully functional Python code ready to drop into your project.
Understanding Two Categories of Stock Connect Constituent Changes
Stock Connect revisions are jointly announced by SSE, SZSE and HKEX and split into scheduled quarterly updates and unplanned ad-hoc revisions. A critical detail: the announcement publish date is never the live effective trading date. Exchanges set a buffer window so brokerages and settlement systems can finish backend configuration changes.
-Quarterly periodic adjustment: Changes go live on the second trading day following the formal announcement release.
-Ad-hoc temporary adjustment: Triggered by corporate incidents or regulatory risk, effective between 1 ~ 3 trading days post notice release.
Official announcements list change type, stock ticker, effective date and benchmark reference date. Still, manually parsing exchange websites to compute valid trading days is inefficient and prone to missed sudden adjustments, which isn’t practical for automated trading pipelines.
Comparing Three Common Data Collection Strategies
I’ve tested three standard approaches during production development and outlined their pros and use cases in plain dev terms.
First, manual announcement lookup is free and sourced directly from official exchange releases. The downsides are obvious: manually calculating business days creates human error, and emergency constituent changes often get picked up late. This only works for occasional manual research instead of automated systems.
Second is building custom scrapers to crawl exchange announcement pages. You retain full control over your data pipeline, but exchange portal UI updates regularly break parsing logic, resulting in constant scraper maintenance overhead. This only makes sense for large teams with dedicated backend engineers to maintain crawlers long-term.
Third is integrating a dedicated market data API like AllTick. The provider pre-calculates all effective dates server-side and syncs both scheduled and emergency constituent updates in real time. You only need valid API access to start pulling data, making this the ideal pick for independent quant developers and small automation projects.
Working Python WebSocket Implementation
Below is runnable WebSocket subscription code that streams real-time tick data alongside Stock Connect membership status and corresponding effective change dates:
import websocket
import json
ws_url = "wss://ws.alltick.co/stock"
def on_message(ws, message):
data = json.loads(message)
# Includes tick data + Stock Connect adjustment effective dates
print("Received real-time payload:", data)
def on_open(ws):
# Subscribe sample HK tickers: 00001.HK, 00700.HK
subscribe_msg = {
"action": "subscribe",
"stocks": ["00001.HK", "00700.HK"]
}
ws.send(json.dumps(subscribe_msg))
def on_error(ws, error):
print("WebSocket error occurred:", error)
def on_close(ws):
print("WebSocket connection closed")
ws = websocket.WebSocketApp(
ws_url,
on_message=on_message,
on_open=on_open,
on_error=on_error,
on_close=on_close
)
ws.run_forever()
Once executed, your application automatically receives quarterly rebalance updates plus unplanned constituent adjustments with verified effective timestamps without extra manual maintenance.
Key Production Best Practices
- Avoid hardcoding your allowed stock list entirely. Quarterly rebalancing makes static configuration a top source of inconsistent historical data; always fetch ticker lists dynamically via API endpoints.
- Base all your filtering logic strictly on official effective dates. Use this timestamp as the cutoff point when adding or removing stocks during backtesting and data aggregation.
- Keep monitoring unscheduled ad-hoc updates. Unexpected constituent removals or additions happen randomly, and real-time API sync protects your pipeline from silent data bugs.
Wrap Up
Solid Stock Connect data management relies on pairing deep exchange rule knowledge with automated API data fetching.
Following this workflow drastically reduces backtesting and scraping inaccuracies caused by unaccounted constituent shifts.
I’m currently building a custom alert script to notify on large constituent shifts — leave a comment if you’d like access to extended code.
Discussion
Have you faced backtest inaccuracies from outdated Stock Connect ticker lists? Share your fixes in the comments!
Top comments (0)