Tushare Data Retrieval
Task Objective
- This Skill is used to: obtain A-share market data, financial indicators, and macroeconomic data through the Tushare data interface
- Capabilities include: real-time quotes, historical K-lines, financial data, indicator data, stock information, macroeconomic data, index data
- Trigger conditions: when users mention Tushare or need to obtain A-share data, financial information, or macroeconomic data
Prerequisites
- Dependency description: install the tushare library (already included in dependency)
- Credential configuration: Tushare Token must be configured (credential name: TUSHARE_TOKEN, environment variable: TUSHARE_TOKEN)
- Account application: visit https://tushare.pro/ register an account and obtain an API Token (see references/tushare_quickstart.md for details)
Operation Steps
- Standard process:
- Determine data requirements — the agent understands the user's intent (quotes/K-lines/financials/macroeconomic/index, etc.)
- Call the corresponding script — select the script according to the data type
- Parse results — the agent analyzes the returned JSON-format data
- Script call mapping:
- Quotes/K-lines/adjusted data →
python scripts/market_data.py --type daily --ts_code 000001.SZ --start_date 20240101 --end_date 20240131
- Financial data/income statement/balance sheet →
python scripts/financial_data.py --type financial --ts_code 000001.SZ --period 2023
- Stock list/trading calendar →
python scripts/stock_info.py --type stock_basic
- Index data/macroeconomic data →
python scripts/macro_index_data.py --type index_daily --ts_code 000001.SH --start_date 20240101 --end_date 20240131
- Optional branches:
- When data for multiple stocks is needed: use the --ts_code parameter separated by commas
- When adjusted data is needed: use --type adj_factor to obtain adjustment factors
- When a reporting period needs to be specified: use the --period parameter (YYYY or YYYYMM)
Usage Examples
- Example 1: Obtain daily K-line data for a stock
- Scenario/Input: the user says "view the K-line data for Ping An Bank over the last month"
- Expected output: return OHLCV data such as date, opening price, highest price, lowest price, closing price, and trading volume
- Key point: call
python scripts/market_data.py --type daily --ts_code 000001.SZ --start_date 20240301 --end_date 20240331
- Example 2: Query financial data
- Scenario/Input: the user says "obtain China Merchants Bank's 2023 financial indicators"
- Expected output: return financial indicators such as ROE, ROA, gross margin, and net margin
- Key point: call
python scripts/financial_data.py --type fina_indicator --ts_code 600036.SH --period 2023
- Example 3: Obtain index data
- Scenario/Input: the user says "query the recent trend of the Shanghai Composite Index"
- Expected output: return data such as the date, closing price, and trading volume of the Shanghai Composite Index
- Key point: call
python scripts/macro_index_data.py --type index_daily --ts_code 000001.SH --start_date 20240301 --end_date 20240331
Quick Reference
Common Code Examples
import tushare as ts
import pandas as pd
# 设置 Token(需先设置环境变量 TUSHARE_TOKEN)
ts.set_token(os.environ.get('TUSHARE_TOKEN', ''))
pro = ts.pro_api()
# 获取日K线数据
df = pro.daily(ts_code='000001.SZ', start_date='20240101', end_date='20241231')
# 获取财务指标
df = pro.fina_indicator(ts_code='600036.SH', period='2023')
# 获取指数数据
df = pro.index_daily(ts_code='000001.SH', start_date='20240101', end_date='20241231')
Comparison with Other Data Sources
| Feature |
tushare |
jqdatasdk |
baostock |
akshare |
| Cost |
Free/Paid |
Requires registration points |
Free |
Free |
| Registration requirement |
✅ Required |
✅ Required |
❌ Not required |
❌ Not required |
| Data scope |
Comprehensive (including macroeconomic) |
A-shares + factors |
Mainly A-shares |
Comprehensive |
| Macro data |
✅ Rich |
❌ |
❌ |
✅ |
| Call frequency |
Free with limits |
Yes |
Unlimited |
Unlimited |
Resource index
- Scripts:
- scripts/market_data.py (Purpose and parameters: retrieve market quotes/K-line/adjusted data, supports --type/--ts_code/--start_date/--end_date)
- scripts/financial_data.py (Purpose and parameters: retrieve financial data/income statement/balance sheet/cash flow statement/indicators, supports --type/--ts_code/--period)
- scripts/stock_info.py (Purpose and parameters: retrieve stock list/trading calendar/name changes, supports --type/--ts_code/--start_date/--end_date)
- scripts/macro_index_data.py (Purpose and parameters: retrieve index data/macro data/Shibor, supports --type/--ts_code/--start_date/--end_date)
- References:
Notes
- Script execution requires configuring a Tushare Token; credential configuration must be completed before first use
- Tushare stock code format: Shanghai market is XXXXXX.SH, Shenzhen market is XXXXXX.SZ
- Some data (such as financial data) may be delayed; it is recommended to retrieve it outside trading hours 9:30-15:00
- The script returns data in JSON format; the agent is responsible for parsing and converting it into a user-friendly presentation
- There is a call frequency limit; free accounts can make at most 120 calls per minute (see the official Tushare documentation for details)
- For first use, it is recommended to first read tushare_quickstart.md to complete authentication configuration