Tushare - China Stock Data Utility
Tushare (pypi-slug: tushare) is a Python utility designed for collecting, cleaning, and storing financial market data, primarily focusing on China stocks, futures, and funds. While the original 'Org' version of Tushare had its own data sources, it is now largely superseded by 'Tushare Pro,' a more robust and maintained data service. The `tushare` library acts as the Python SDK to access both the legacy 'Org' data (though deprecated) and the recommended 'Pro' data interfaces. This library is currently at version 1.4.29 and is in maintenance mode for its legacy features, with active development focused on its Pro API integration.
Warnings
- breaking The original 'Org' version of Tushare, which is directly used without `ts.pro_api()` and a token, is no longer maintained, and its data sources are deprecated. Reliability and availability of data from these legacy interfaces are not guaranteed. Users should migrate to Tushare Pro.
- gotcha Tushare Pro APIs require an API token for authentication. Failing to set the token will result in data access failures.
- gotcha Some Tushare API functions are sensitive to date format. Dates must be provided in 'YYYYMMDD' string format; otherwise, APIs may return empty data or errors.
- gotcha Frequent data calls, especially with different parameters or without adherence to usage policies, can lead to rate limiting or temporary data call restrictions by the Tushare service.
- gotcha There have been reported incompatibilities with newer versions of the pandas library, specifically where Tushare might not adapt correctly to recent pandas updates.
Install
-
pip install tushare
Imports
- tushare
import tushare as ts
Quickstart
import tushare as ts
import os
# Tushare Pro requires a token. Register at tushare.pro to get one.
# It's recommended to set it as an environment variable.
PRO_TOKEN = os.environ.get('TUSHARE_PRO_TOKEN', 'YOUR_TUSHARE_PRO_TOKEN')
ts.set_token(PRO_TOKEN)
# Initialize Pro API client
pro = ts.pro_api()
# Fetch stock daily quotes (example: Ping An Bank, TS_CODE: 000001.SZ)
df = pro.daily(ts_code='000001.SZ', start_date='20230101', end_date='20230110')
print(df.head())
# Example: Get real-time quotes (requires Pro, but might be via old interface or specific Pro interface)
# Note: Real-time data access might have specific Pro API calls or rate limits.
# For historical data, pro.daily is the recommended way in Tushare Pro.
# For older tushare (non-Pro) historical data, which is deprecated:
# try:
# legacy_df = ts.get_hist_data('600848')
# print("\nLegacy historical data (deprecated):")
# print(legacy_df.head())
# except Exception as e:
# print(f"\nCould not fetch legacy data (expected for deprecated service): {e}")