Skip to main content

A Python wrapper for Tushare, a Chinese financial data provider. The project provides a simple and easy-to-use automation tool for **downloading** financial data from Tushare, and **storing** the data in a local mysql database.

Project description

bagel-tushare

Author: Yanzhong(Eric) Huang bagelquant

A Python wrapper for Tushare, a Chinese financial data provider. The project provides a simple and easy-to-use automation tool for downloading financial data from Tushare, and storing the data in a local mysql database.

Features

  1. Download data from Tushare
  2. Store data in a local mysql database
  3. Query quickly from the local mysql database
  4. Update data in the local mysql database

Installation

pip install bagel-tushare

Development

uv sync
uv run pytest

See docs/testing.md for test strategy, markers, and how to run legacy unittest tests.

By default tests run fully offline using an in-memory SQLite database.

If you add integration tests (e.g. MySQL/Postgres/Tushare network), mark them with @pytest.mark.integration and run them explicitly:

uv run pytest -m integration

Usage

from bageltushare import download, update_by_date, update_by_code
from bageltushare import create_index, create_all_tables, get_engine


# configs
HOST = "localhost"
PORT = 3306
USER = "root"
PASSWORD = "<YOUR_PASSWORD>"
DB = "<DATABASE_NAME>"

# db_type supports: "sqlite", "mysql", "mariadb"
ENGINE = get_engine(host=HOST, port=PORT, user=USER, password=PASSWORD, database=DB, db_type="mysql")
TOKEN = "<YOUR_TOKEN>"

def main() -> None:
    by_date_apis = [
        "daily",
        "adj_factor",
    ]

    by_code_apis = [
        "balancesheet",
        "cashflow",
        "income",
    ]

    create_all_tables(engine=ENGINE)

    # download and replace table
    download(engine=ENGINE, token=TOKEN, api_name="trade_cal")
    download(engine=ENGINE,
             token=TOKEN,
             api_name="stock_basic",
             params={"list_status": "L, D, P"},
             fields=[
                 "ts_code",
                 "symbol",
                 "name",
                 "area",
                 "industry",
                 "cnspell",
                 "market",
                 "list_date",
                 "act_name",
                 "act_ent_type",
                 "fullname",
                 "enname",
                 "exchange",
                 "curr_type",
                 "list_status",
                 "delist_date",
                 "is_hs"
             ])

    for api in by_date_apis:
        update_by_date(engine=ENGINE, token=TOKEN, api_name=api)
        create_index(engine=ENGINE, table_name=api)

    for api in by_code_apis:
        update_by_code(engine=ENGINE, token=TOKEN, api_name=api)
        create_index(engine=ENGINE, table_name=api)

if __name__ == "__main__":
    main()

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

bagel_tushare-3.0.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bagel_tushare-3.0.0-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file bagel_tushare-3.0.0.tar.gz.

File metadata

  • Download URL: bagel_tushare-3.0.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for bagel_tushare-3.0.0.tar.gz
Algorithm Hash digest
SHA256 d255eba5d9457208e1d3036f089b417f155a9b6665547dc37bcba83c62179909
MD5 8f289831d1e2535fb7757051bb6575b2
BLAKE2b-256 4d1cb74b6210ee24ae88e9906637b76dc50d993e94e9e900b040f2bbb3552c3e

See more details on using hashes here.

File details

Details for the file bagel_tushare-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: bagel_tushare-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for bagel_tushare-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 273a2ee94f5033dc607ccaa33247d3574338a410d01970368fe97c5cd9630b82
MD5 fceb66e31ee435bcc57263128f90b7b2
BLAKE2b-256 db27de588bba71867ea594c6d222bfdc3d882fbc2b4c85c9eb93c9cf1ed54437

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page