Tdx Professional Data Source
Paste the following prompt into your AI chat to install this skill:
Please follow https://skillhub.cn/install/skillhub.md to install @user_8c7f0f65/tdx-pro.
About this skill
Why This Skill Exists
When building quantitative trading systems or financial analysis tools, obtaining reliable stock market data is the first major hurdle. The pytdx library wraps Tongdaxin's data protocol into Python interfaces, but using it directly presents several challenges: difficulty selecting stable servers (performance varies significantly), complex API parameters (market codes, K-line periods, etc.), and lack of systematic documentation.
This skill addresses these pain points by providing a comprehensive reference system and intelligent connection strategy, allowing you to focus on data analysis rather than wrestling with data acquisition details.
Core Capabilities and Key Steps
Dual-Engine API Architecture
The skill encapsulates Tongdaxin's two core interfaces, covering virtually all financial instruments:
| API Type | Use Cases | Typical Data |
|---|---|---|
TdxHq_API |
A-shares, B-shares, indices | Real-time quotes, daily/minute K-lines |
TdxExHq_API |
Futures, options, HK/US stocks | Futures quotes, options chains, foreign markets |
Market codes follow Tongdaxin standards: 0 for Shenzhen Stock Exchange, 1 for Shanghai Stock Exchange. K-line periods range from 0 (5-minute) to 11 (yearly).
Intelligent Server Selection Mechanism
A core highlight is the automated server optimization strategy:
- Multi-dimensional scoring: Combines preset weights, historical success rates, response times, and freshness
- Automatic failover: Servers with 3+ consecutive failures are automatically downgraded
- Seamless switching: Default 2 automatic retries without manual intervention
Pre-configured optimal servers (e.g., 115.238.56.198:7709, weight 1.0) are empirically validated for out-of-the-box reliability.
Standard Workflow
A typical usage scenario:
# 1. Connect (with auto resource management)
with TdxHq_API() as api:
# 2. Select market and stock code
market = 1 # Shanghai
code = "600519"
# 3. Fetch K-line data
kline = api.get_kline_data(
market=market,
code=code,
category=9, # Daily K-line with volume
start=0
)
# 4. Convert to DataFrame for analysis
df = kline.to_df()
The skill includes references/api-reference.md (detailed parameter documentation) and references/usage-examples.md (scenario-based code samples).
Limitations and Considerations
Data source limitations: pytdx retrieves data via Tongdaxin protocol, essentially scraping public market data. It does not include Level 2 depth data or real-time tick-by-tick trades. For Level 2 data, you'll need alternative sources.
Network dependency: As data is fetched in real-time, network instability directly impacts data retrieval. Use in stable network environments and implement timeout/retry mechanisms.
Extended market specifics: Futures and options require TdxExHq_API and dedicated server addresses, different from A-share standard servers. Pay attention to API type selection.
Historical data indexing: The start parameter begins from 0 (counting backwards from latest). To fetch data for specific dates, calculate offset values accordingly.
In summary, this skill suits scenarios requiring batch stock market data retrieval for quantitative analysis. It solves data acquisition engineering challenges, but analysis and strategy development still require your domain expertise.
Use Cases
- When developing a stock quantitative strategy, need to batch-fetch daily K-line data for A-shares over the past three years via `pytdx` to build a backtest database and validate strategy performance curves.
- Building a real-time stock monitoring dashboard, need to call the extended market API to get real-time quotes for Hong Kong and US stocks, aggregating multi-market data into a single display interface.
- Analyzing sector capital flow, need to query Tongdaxin sector classification API to get concept sector constituent lists, then fetch financial indicators stock-by-stock for fundamental screening.
- Backtesting intraday futures strategies, need to obtain 1-minute K-line data for commodities like rebar and crude oil, calculating technical indicators to generate trading signals for simulated trades.
Best For
- Quantitative hedge fund researcher, needs stable access to A-share historical K-line data to build backtest frameworks and validate multi-factor stock selection strategies.
- Financial data platform engineer, needs to wrap Tongdaxin market API as microservices for unified real-time data access across multiple internal trading systems.
- Futures CTA strategy developer, needs to batch-fetch commodity futures tick data to calculate Bollinger Bands for automated trend-following trades.
- Independent quantitative trader, needs Python scripts to automatically monitor real-time quotes from watchlist stocks and send alerts when preset conditions are triggered.
Related Skills
Systematically identify and evaluate the economic moats of listed companies using the Tang Shu Fang investment methodology for long-term investment analysis.
A skill that converts natural language questions into A-stock data queries and returns verifiable structured analysis conclusions, covering multi-dimensional analysis of market trends, fundamentals, and news.
An AI financial copilot by Wind, integrating financial databases and multimodal analysis to provide end-to-end support across investment research, asset allocation, risk control, quant, and report generation.
Searches for latest policies and generates GB/T 9704-2020 compliant official documents (requests or reports) in Word and PDF formats.