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.
pip install tushareVerified import paths — ran on the pinned version, not inferred.
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.
Register for a Tushare Pro account at `tushare.pro`, obtain an API token, and use `ts.set_token()` followed by `ts.pro_api()` to access the Pro data interfaces.
Set your Tushare Pro token using `ts.set_token('YOUR_TOKEN')` before making any Pro API calls. It's best practice to load the token from an environment variable.Always ensure date parameters are passed as strings in 'YYYYMMDD' format, e.g., '20230101'.
Consult Tushare Pro documentation for rate limits and best practices. Implement delays or caching mechanisms in your code to avoid excessive requests.
If encountering issues, try pinning your pandas version to an older, compatible release (e.g., `pandas<2.0.0`) or check the Tushare GitHub issues for specific compatibility updates or workarounds.
Initialize the Pro API client with your token and use the equivalent Pro API methods, such as `pro.daily()` for daily historical data: `import tushare as ts; ts.set_token('YOUR_PRO_TOKEN'); pro = ts.pro_api(); df = pro.daily(ts_code='000001.SZ', start_date='20180101', end_date='20181101')`.Obtain your Tushare Pro token from the official website and set it in your script or environment variable: `import tushare as ts; ts.set_token('YOUR_PRO_TOKEN'); pro = ts.pro_api()`.Refactor your code to fetch data in smaller batches using loops and pagination, or adjust the date range to reduce the single request volume to stay within the API limits.
Install the package using pip: `pip install tushare`.