Skip to main content

A tool for obtaining historical data of China stock market

Project description

  • It’s easy to use because most of the data returned are pandas DataFrame objects

  • We have our own data server, efficient and stable operation

  • Free china stock market data

  • Friendly to machine learning and data mining

Target Users

  • China Financial Market Analyst

  • Financial data analysis enthusiasts

  • Quanters who are interested in china stock market

Installation

pip install baostock

Upgrade

pip install baostock –upgrade

Quick Start

import baostock as bs
import pandas as pd

#### 登陆系统 ####
lg = bs.login()
# 显示登陆返回信息
print('login respond error_code:'+lg.error_code)
print('login respond  error_msg:'+lg.error_msg)

#### 获取历史K线数据 ####
# 详细指标参数,参见“历史行情指标参数”章节
rs = bs.query_history_k_data_plus("sh.600000",
        "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM,pbMRQ,psTTM,pcfNcfTTM,isST",
        start_date='2025-06-01', end_date='2025-12-31',
        frequency="d", adjustflag="2") #frequency="d"取日k线,adjustflag="3"默认不复权,"2"前复权

print('query_history_k_data_plus respond error_code:'+rs.error_code)
print('query_history_k_data_plus respond  error_msg:'+rs.error_msg)

#### 打印结果集 ####
data_list = []
while (rs.error_code == '0') & rs.next():
        # 获取一条记录,将记录合并在一起
        data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)
#### 结果集输出到csv文件 ####
result.to_csv("D:/history_k_data.csv", encoding="gbk", index=False)
print(result)

#### 登出系统 ####
bs.logout()

return:

login success!
login respond error_code:0
login respond  error_msg:success
query_history_k_data_plus respond error_code:0
query_history_k_data_plus respond  error_msg:success
date       code           open  ...     psTTM  pcfNcfTTM isST
0    2025-06-03  sh.600000  11.9476797700  ...  2.148197  -9.209045    0
1    2025-06-04  sh.600000  12.1126761600  ...  2.120788  -9.091545    0
2    2025-06-05  sh.600000  12.0544421400  ...  2.110509  -9.047483    0
3    2025-06-06  sh.600000  11.9670911100  ...  2.110509  -9.047483    0
4    2025-06-09  sh.600000  11.9476797700  ...  2.108796  -9.040139    0
..          ...        ...            ...  ...       ...        ...  ...
141  2025-12-25  sh.600000  11.8000000000  ...  2.263479  -1.849434    0
142  2025-12-26  sh.600000  11.7700000000  ...  2.253864  -1.841577    0
143  2025-12-29  sh.600000  11.7400000000  ...  2.340403  -1.912286    0
144  2025-12-30  sh.600000  12.1700000000  ...  2.382711  -1.946855    0
145  2025-12-31  sh.600000  12.3500000000  ...  2.392326  -1.954712    0

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

baostock-0.9.3.tar.gz (26.8 kB view details)

Uploaded Source

Built Distribution

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

baostock-0.9.3-py3-none-any.whl (51.1 kB view details)

Uploaded Python 3

File details

Details for the file baostock-0.9.3.tar.gz.

File metadata

  • Download URL: baostock-0.9.3.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for baostock-0.9.3.tar.gz
Algorithm Hash digest
SHA256 16699d82d05037a8c133577fcdeb9ac0d5a7f31edc2432c4e883004e0a95e3f7
MD5 926655eaa70203b751249b59ae7cc53b
BLAKE2b-256 f31de62f47577d49d5dd98ff20c2e3fccc318e2b7984a06fb9abb9b1136f046c

See more details on using hashes here.

File details

Details for the file baostock-0.9.3-py3-none-any.whl.

File metadata

  • Download URL: baostock-0.9.3-py3-none-any.whl
  • Upload date:
  • Size: 51.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for baostock-0.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 acbd19403285bc4e254cee8297cf0e2646ae2276e5af7e549deed3988ab02293
MD5 3337b41aaf1e6281143e00b2d68aab21
BLAKE2b-256 d216f28fef16a5d9af31def7435b73efd91ed66b40b9c249387d6c11cdf2ba35

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