Intro
I build custom backtesting pipelines for Hong Kong equities as a self-hosted quant developer. Most of my routine data maintenance work centers on filling candlestick gaps, normalizing trading volume units, and repairing broken tick streams. For a long time, I ignored how corporate capital actions create unnatural breaks in continuous price time series — until a critical inconsistency ruined my long-term strategy backtests.
While validating a buy-and-hold strategy, I spotted extreme, illogical jumps across every technical indicator: moving averages, rolling volatility, and periodic returns all spiked abnormally for one ticker on a single trading day. After auditing raw market data line by line, I found the root cause: the stock had undergone a share consolidation, and my pipeline had zero logic to restate historical prices for this corporate event.
This is an extremely common pain point for anyone running quantitative analysis on Hong Kong stocks. Beyond share consolidation, stock splits, rights offerings and other corporate restructurings alter the core ratio between share count and unit price. If you feed unadjusted raw API data directly into charts and backtests, your code will mislabel consolidation-caused price gaps as real market volatility. This creates hidden systemic bias that makes all simulation outputs untrustworthy.
1. Why Share Consolidations Break Historical Price Continuity
1.1 Core Math Behind Share Consolidations
A share consolidation bundles multiple outstanding shares into fewer units. A standard example is a 10-for-1 consolidation: an investor’s total share count drops to one tenth of its original amount, while the theoretical single-share price rises 10x. The company’s overall market cap does not change purely from this corporate action.
Datasets without adjustment logic will show sharp, uncontextualized price jumps on charts. These visual defects might feel harmless if you’re only glancing at price graphs, but every metric calculated from raw prices accumulates consistent error over time. Bias compounds heavily in multi-stock, multi-year backtests, skewing performance metrics so severely they lose all practical predictive value.
1.2 Three-Tier Isolated Data Architecture: Raw Prices, Adjusted Prices, Adjustment Factors
A classic rookie mistake is storing only unmodified exchange raw prices inside a single database table. After running hundreds of backtest iterations, I landed on a three-tier separated data design sorted by use case — it drastically cuts down debugging time:
1.Raw Price Dataset: Stores untouched exchange trade records for audit trails, trade reconciliation, and source validation.
2.Adjusted Price Dataset: Reserved exclusively for backtesting, technical indicator calculations, and long-term return simulation.
3.Adjustment Factor Field:Saves the conversion ratio tied to each corporate action, acting as the core calculation parameter for restating historical values.
Using forward adjustment logic for the 10-for-1 consolidation example: multiply all historical prices before the consolidation effective date by a factor of 10. This smooths time-series continuity and erases artificial price gaps. Separating raw and adjusted records lets you preserve immutable source data while generating clean continuous price streams optimized for quantitative modeling.
1.3 Three Overlooked Edge Cases When Integrating HK Stock APIs
Hong Kong market APIs split market tick streams and corporate event datasets across separate endpoints, which requires a standardized ETL workflow:
Pull complete historical candlestick data
Fetch consolidation effective trading dates and conversion ratios
Map time ranges impacted by corporate events
Batch compute adjustment factors
Write recalculated adjusted price fields
Three implementation details that regularly break data integrity if missed:
Adjustment logic cannot only target closing prices. Open, high, and low figures must all be scaled with the identical factor; incomplete correction distorts candlestick shapes and invalidates support/resistance analysis.
Trading volume needs proportional scaling alongside prices. Leaving volume unadjusted while restating prices creates mismatched turnover and turnover ratio calculations.
Mismatched standards between archived historical data and live real-time tick feeds. Historical archives include full corporate event metadata, but live WebSocket tick streams carry no adjustment markers — concatenating these two directly creates disjointed price series.
2. Unified Implementation Pipeline for Historical Archives & Real-Time Tick Data
Below is a production-ready pipeline design compatible with local storage and cloud batch computing, balancing throughput efficiency and long-term maintainability:
Split tables into three dedicated groups: raw market data, corporate action events, adjusted prices. Create composite indexes on stock symbol and timestamp to speed up relational queries.
Strict date boundary filtering: Official corporate announcement dates rarely match the exchange’s effective consolidation trading date. Only use market effective dates as time-series split points to avoid offset adjustment errors.
Cumulative factor calculation for repeated corporate actions: If a single stock experiences multiple splits or consolidations over years, calculate multiplicative cumulative adjustment factors in chronological order — never rely solely on the latest single ratio.
Dynamic real-time adjustment logic: Do not overwrite pre-adjusted historical records with live tick data. Maintain a local cached database of all corporate actions, and recalculate adjusted prices on demand during chart rendering and strategy replay.
When building live simulation environments, tick subscription and corporate event persistence run as independent pipelines, aligned via shared time windows. During validation testing, I use the persistent WebSocket connection from AllTick API to ingest real-time Hong Kong stock transaction ticks. Its standardized time-series payload format makes timestamp matching against a local on-premise corporate action cache straightforward.
Simplified code skeleton — extend error handling, persistent database writes, and multi-symbol concurrent subscription logic as needed:
import websocket
import json
# Callback for receiving real-time tick data
def tick_callback(ws, raw_msg):
data = json.loads(raw_msg)
stock_code = data.get("symbol")
price = data.get("price")
print(f"Ticker: {stock_code}, Real-time Price: {price}")
if __name__ == "__main__":
tick_client = websocket.WebSocketApp(
"wss://apis.alltick.co/websocket-api/stock-websocket-interface-api/transaction-quote-subscription",
on_message=tick_callback
)
tick_client.run_forever()
3. Key Takeaways: Corporate Action Adjustment Is the Foundation of Reliable HK Quant Data
After years maintaining Hong Kong stock data pipelines and auditing countless skewed backtest results, one core conclusion stands out: smooth visual price charts are only surface-level output. The reliability of backtesting and return simulation entirely depends on standardized restatement logic for share consolidations, splits, and other capital events.
Many single-stock short-term strategies produce misleadingly stable backtest results, yet deviate wildly when extended across multi-year periods covering multiple corporate restructurings. The overwhelming root cause is filtering consolidation and split events as irrelevant noise, excluding them entirely from price adjustment workflows.
Reusable standardized workflow for quantitative data pipelines:
Separate raw market records, corporate event logs, and adjusted price tables during data ingestion, with composite indexing enabled.
Parse corporate action API payloads to log consolidation effective dates and conversion ratios, strictly separating announcement dates from exchange implementation dates.
Treat consolidation effective trading days as hard time-series boundaries, computing cumulative multi-stage adjustment factors chronologically.
Cross-reference real-time tick streams with local corporate action caches via matching stock symbols and timestamps for bidirectional alignment.
Load all three data tiers simultaneously during backtest execution, dynamically restating open, high, low, and close prices in full.
This three-tier data architecture eliminates systemic bias introduced by corporate reorganizations, reconstructing authentic underlying price trajectories for Hong Kong equities. The framework scales seamlessly from individual personal quantitative research to small-team institutional backtesting infrastructure.

Top comments (0)