Skip to main content

BaoStock

  • 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

Release files for baostock 0.9.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for baostock 0.9.4
File Size Uploaded
baostock-0.9.4.tar.gz 28.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for baostock 0.9.4
File Interpreter ABI Platform
baostock-0.9.4-py3-none-any.whl Python 3 none any Details

Total release size: 80.6 kB

Release files / baostock-0.9.4.tar.gz

Download URL baostock-0.9.4.tar.gz
Size 28.1 kB
Tags Source
SHA-256 checksum
How to use checksums
54dd26ec53100da02dcd47a21fc487ed5b2e12a4124af7961143d8ca0bc682e9
BLAKE2b-256 checksum
How to use checksums
66618ad575eb83d4b22852a922e669bc13c9723675343e885b0040ab11a6a321
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Release files / baostock-0.9.4-py3-none-any.whl

Download URL baostock-0.9.4-py3-none-any.whl
Size 52.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bf71c6069ab5890ff3596632f9c3f8f1fbc6bfcac582c2f9d6a5c11ab2cfaf8
BLAKE2b-256 checksum
How to use checksums
56df6984deaa036cc3467f85411d01a48018eb86ed7477e74f33aa5b012ace05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.9.4 This release

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.8.9

2 release files

0.8.8

2 release files

0.7.5

2 release files

0.7.2

2 release files

0.7.1

1 release file

0.7.0

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.5.5

2 release files

0.5.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page