A Python library for accessing Tushare financial data through optimized API
Project description
TuData
A Python library for accessing Tushare financial data through optimized API.
Features
- 🚀 Optimized Performance: Built-in connection pooling and retry mechanisms
- 📊 Comprehensive Data: Access to stocks, funds, futures, options, and cryptocurrency data
- 🔧 Easy to Use: Simple and intuitive API interface
- 🛡️ Robust: Automatic error handling and retry logic
- 📈 Real-time: Support for real-time and historical financial data
Installation
pip install tudata
Quick Start
1. Set Your Token
import tudata as ts
# Set your Tushare token (only need to do this once)
ts.set_token('your_tushare_token_here')
2. Initialize API Client
# Initialize the API client
api = ts.pro_api()
# Or initialize with token directly
api = ts.pro_api(token='your_token_here')
3. Fetch Data
# Get stock basic information
stocks = api.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
print(stocks.head())
# Get daily trading data
daily_data = api.daily(ts_code='000001.SZ', start_date='20240101', end_date='20241201')
print(daily_data.head())
# Get trading calendar
cal = api.trade_cal(exchange='', start_date='20240101', end_date='20241201')
print(cal.head())
4. Use Pro Bar for Time Series Data
# Get historical price data with pro_bar
df = ts.pro_bar(ts_code='000001.SZ', start_date='20240101', end_date='20241201', freq='D')
print(df.head())
API Reference
Core Classes
pro_api
The main API client class for accessing Tushare data.
api = ts.pro_api(token=None)
Parameters:
token(str, optional): Your Tushare API token. If not provided, will use stored token.
Main Methods
Stock Data
stock_basic(**kwargs)- Get basic stock informationdaily(**kwargs)- Get daily trading dataweekly(**kwargs)- Get weekly trading datamonthly(**kwargs)- Get monthly trading dataadj_factor(**kwargs)- Get adjustment factorsdaily_basic(**kwargs)- Get daily basic trading metrics
Financial Statements
income(**kwargs)- Get income statement databalancesheet(**kwargs)- Get balance sheet datacashflow(**kwargs)- Get cash flow statement datafina_indicator(**kwargs)- Get financial indicators
Market Data
moneyflow(**kwargs)- Get money flow datatop_list(**kwargs)- Get top gainers/loserslimit_list_d(**kwargs)- Get daily limit up/down stocks
Index Data
index_basic(**kwargs)- Get index basic informationindex_daily(**kwargs)- Get index daily dataindex_weight(**kwargs)- Get index constituent weights
Fund Data
fund_basic(**kwargs)- Get fund basic informationfund_nav(**kwargs)- Get fund net asset valuefund_daily(**kwargs)- Get fund daily trading data
And many more...
Utility Functions
set_token(token)
Store your Tushare API token locally.
Parameters:
token(str): Your Tushare API token
get_token()
Retrieve the stored Tushare API token.
Returns:
str: The stored token, or None if not set
pro_bar(ts_code, start_date, end_date, freq='D', **kwargs)
Get time series data with enhanced functionality.
Parameters:
ts_code(str): Stock code (e.g., '000001.SZ')start_date(str): Start date in 'YYYYMMDD' formatend_date(str): End date in 'YYYYMMDD' formatfreq(str): Data frequency ('D', 'W', 'M', etc.)
Configuration
Connection Settings
The library includes optimized connection settings:
- Connection Pooling: 10 connections with max 20 connections
- Retry Logic: Automatic retry on 500, 502, 503, 504 errors
- Timeouts: 5 seconds connection timeout, 30 seconds read timeout
Error Handling
The library automatically handles:
- Network timeouts and retries
- Invalid token errors
- Rate limiting
- JSON parsing errors
Examples
Get Stock List
import tudata as ts
api = ts.pro_api()
# Get all listed stocks
stocks = api.stock_basic(list_status='L')
print(f"Total stocks: {len(stocks)}")
print(stocks[['ts_code', 'name', 'industry']].head())
Get Historical Prices
# Get 1 year of daily data for a specific stock
data = api.daily(
ts_code='000001.SZ',
start_date='20230101',
end_date='20231231'
)
print(data[['trade_date', 'open', 'high', 'low', 'close', 'vol']].head())
Get Financial Indicators
# Get financial indicators for a stock
indicators = api.fina_indicator(
ts_code='000001.SZ',
start_date='20230101',
end_date='20231231'
)
print(indicators[['end_date', 'roe', 'roa', 'eps']].head())
Real-time Data
# Get real-time quotes (requires special permission)
quotes = api.realtime_quote(ts_code='000001.SZ,000002.SZ')
print(quotes)
Requirements
- Python 3.7+
- pandas >= 1.0.0
- requests >= 2.25.0
- urllib3 >= 1.26.0
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you encounter any issues or have questions, please file an issue on the GitHub repository.
Disclaimer
This library is for educational and research purposes. Please ensure you comply with Tushare's terms of service and any applicable financial data regulations.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tudata-1.0.7.tar.gz.
File metadata
- Download URL: tudata-1.0.7.tar.gz
- Upload date:
- Size: 26.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a96282b2b33b288cdb82585b0cf201ad8c9b3551e66bced045b82295ed171d9e
|
|
| MD5 |
42963861838299a35b5b16aa22620027
|
|
| BLAKE2b-256 |
6f6de5b4bd5c3482a95fc3dfc2154002398f81f3abcd00ac0753f4466c14a4e9
|
File details
Details for the file tudata-1.0.7-py3-none-any.whl.
File metadata
- Download URL: tudata-1.0.7-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9bda0111c799070a1120ff5bee588af366f3dd8a06aa16ee92801251c0409c7
|
|
| MD5 |
fc7e49e0f6c04cae661aaf7835da79d8
|
|
| BLAKE2b-256 |
b1d1d19c57cdc964c72b13d9f2248b73a5032486f6a75eecf4ee1f661da7ac8a
|