Tushare - China Stock Data Utility

1.4.29 · maintenance · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Tushare with a Pro API token (recommended) and fetch historical daily stock data using the Tushare Pro interface. The `TUSHARE_PRO_TOKEN` should be obtained from tushare.pro after registration and set as an environment variable for security. The example fetches daily quotes for a specific stock within a date range.

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}")

view raw JSON →