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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file baostock-0.9.1.tar.gz.
File metadata
- Download URL: baostock-0.9.1.tar.gz
- Upload date:
- Size: 25.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ec3a44cbec9471872b0a4ae02c39aea29db688bb740b769167393bc6e244a39
|
|
| MD5 |
86fa7b4a8fd2a55a299a0a40d402302d
|
|
| BLAKE2b-256 |
435e471c14eebb204ed89de0fe11740d6f4513d44454fe5e35bc13b29ef3e510
|
File details
Details for the file baostock-0.9.1-py3-none-any.whl.
File metadata
- Download URL: baostock-0.9.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a30749f095fd58ecd7ba3799dad605d355ad8e6d19310bc437039e1e526f15fe
|
|
| MD5 |
3138d44d1c4ae0932f6a2af35bf9cb34
|
|
| BLAKE2b-256 |
c8ebfadb92e38fbb2fefa3d9d3c14d016cf3a5920e34666aeb9950bc5311c856
|