Collect complete data for a specified stock: historical K-lines, real-time quotes, and chip distribution (A-shares only).
Execute Command
python scripts/collect_stock_data.py <股票代码> [--days N] [--provider akshare|tushare] --date YYYY-MM-DD
# 示例
python scripts/collect_stock_data.py 600519 --date 2025-01-01 # A股,60天
python scripts/collect_stock_data.py 000001 --days 90 --date 2025-01-01 # 指定90天
python scripts/collect_stock_data.py AAPL --days 30 --date 2025-01-01 # 美股30天
python scripts/collect_stock_data.py 600519 --provider tushare --date 2025-01-01 # 用户选择 tushare
Parameter Description:
--days: Number of days to retrieve (default 60 days)--provider: Data source (defaultakshare; only used when the user selectstushare)--date: Date identifier for the saved file (required, used to ensure subsequent analysis and decisions are reproducible; does not affect the data time range)
When --provider tushare :
- Currently only supports A-share K-lines
- Need to set environment variables
TUSHARE_TOKEN realtimeandchipfield will returnnull
Market Support
Automatically identify the market type:
- A-shares: 6-digit numbers (600519, 000001)
- Hong Kong stocks: 5-digit numbers (00700, 09988)
- US stocks: alphabetic codes (AAPL, MSFT)
- ETF: starting with 51/52/56/58 (512880)
See markets.md
Output Structure
output/<股票代码>/<日期>/data.json
Output includes:
klines: Historical K-lines (date, open, high, low, close, volume and turnover, change percentage)realtime: Real-time quotes (price, volume ratio, turnover rate, P/E ratio, etc.)chip: Chip distribution (A-shares only, profit ratio, chip concentration, etc.)
For complete field descriptions, see fields.md
Failure Handling
- Invalid stock code/market not supported: Check
markets.mdidentification rules - K-line data retrieval failed: The script will automatically switch data sources; if it still fails, it may be due to network/source site restrictions, retry later
- Real-time quotes/chip distribution failed: A warning will be output but it will not block (K-lines will still be saved)
Error Handling
A-share Data Sources: Automatically switch from East Money → Sina → Tencent, error is reported only if all sources fail
Optional Data: Failure to retrieve real-time quotes and chip data does not affect K-line output (a warning will be issued)
Anti-Ban: The script automatically sleeps randomly for 2-5 seconds between requests
Dependency Installation
pip install akshare tushare pandas