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.2.tar.gz (25.5 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.2-py3-none-any.whl (47.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: baostock-0.9.2.tar.gz
  • Upload date:
  • Size: 25.5 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.2.tar.gz
Algorithm Hash digest
SHA256 7a3fbba3417b308f774e84a62fc10909a70e81384051b3d693cbe54213686544
MD5 133acc29b8421730899636d90bdebfc2
BLAKE2b-256 fa2e32f7c36a6d021b7cb8bf3bd75b4de8d74e2405e21a5ba1bf1e3706b2d34b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: baostock-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 47.9 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f7aaca22eb010db268a2b8bc3ed1a8743cc6f1989902477c65c1c3c022d2ecbf
MD5 7a065856ad80577e221b7b57398ce5a6
BLAKE2b-256 28b8ccba7fa0dd4d1809a6244c04e58173b648fbb64d7cb158387b70d6695e7f

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