起因是有个不知道什么专业的同学问了我一题
cs:
import baostock as bs
import pandas as pd
import datetime
'''
日线指标参数包括:'date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM,pbMRQ,psTTM,pcfNcfTTM,isST'
周、月线指标参数包括:'date,code,open,high,low,close,volume,amount,adjustflag,turn,pctChg'
分钟指标参数包括:'date,time,code,open,high,low,close,volume,amount,adjustflag'
adjustflag:复权类型,默认不复权:3;1:后复权;2:前复权。已支持分钟线、日线、周线、月线前后复权。
'''
# 是否删除停盘数据
DROP_SUSPENSION = True
def update_stk_list(date=None):
# 获取指定日期的指数、股票数据
stock_rs = bs.query_all_stock(date)
stock_df = stock_rs.get_data()
stock_df.to_csv('./stk_data/all_list.csv', encoding='gbk', index=False)
stock_df.drop(stock_df[stock_df.code < 'sh.600519'].index, inplace=True)
stock_df.drop(stock_df[stock_df.code > 'sh.600519'].index, inplace=True)
stock_df = stock_df['code']
stock_df.to_csv('./stk_data/stk_list.csv', encoding='gbk', index=False)
return stock_df.tolist()
def load_stk_list():
df = pd.read_csv('./stk_data/stk_list.csv')
return df['code'].tolist()
def convert_time(t):
H = t[8:10]
M = t[10:12]
S = t[12:14]
return H + ':' + M + ':' + S
def download_data(stk_list=[], fromdate='2013-1-1', todate=datetime.date.today(),
datas='date,open,high,low,close,volume,amount,turn,pctChg',
frequency='d', adjustflag='2'):
for code in stk_list:
print("Downloading :" + code)
k_rs = bs.query_history_k_data_plus(code, datas, start_date=fromdate, end_date=todate.strftime('%Y-%m-%d'),
frequency=frequency, adjustflag=adjustflag)
datapath = './stk_data/' + frequency + '/' + code + '.csv'
out_df = k_rs.get_data()
if DROP_SUSPENSION and 'volume' in list(out_df):
out_df.drop(out_df[out_df.volume == '0'].index, inplace=True)
# 做time转换
if frequency in ['5', '15', '30', '60'] and 'time' in list(out_df):
out_df['time'] = out_df['time'].apply(convert_time)
out_df.to_csv(datapath, encoding='gbk', index=False)
if __name__ == '__main__':
bs.login()
# 首次运行
stk_list = update_stk_list(datetime.date.today() - datetime.timedelta(days=31))
# 非首次运行
# stk_list = load_stk_list()
# 下载日线
download_data(stk_list)
# 下载周线
download_data(stk_list, frequency='w')
# 下载月线
download_data(stk_list, frequency='m')
# 下载5分钟线
download_data(stk_list, fromdate='2013-6-1', frequency='5',
datas='date,time,open,high,low,close,volume,amount,adjustflag')
# 下载15分钟线
download_data(stk_list, fromdate='2013-6-1', frequency='15',
datas='date,time,open,high,low,close,volume,amount,adjustflag')
# 下载30分钟线
download_data(stk_list, fromdate='2013-6-1', frequency='30',
datas='date,time,open,high,low,close,volume,amount,adjustflag')
# 下载60分钟线
download_data(stk_list, fromdate='2013-6-1', frequency='60',
datas='date,time,open,high,low,close,volume,amount,adjustflag')
bs.logout()
cs2:
import pandas as pd
import matplotlib.pyplot as plt
# 从CSV文件中读取股票信息
df = pd.read_csv('./stk_data/m/sh.600519.csv') # 请替换为你的CSV文件路径
# 将日期列转换为日期时间类型
df['date'] = pd.to_datetime(df['date'])
# 设置图表字体为支持中文的字体(例如SimHei或Microsoft YaHei)
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示为方块的问题
# 绘制股票信息的图表
plt.figure(figsize=(12, 6))
plt.plot(df['date'], df['close'], marker='o', linestyle='-', color='b', label='收盘价')
plt.plot(df['date'], df['open'], marker='o', linestyle='-', color='g', label='开盘价')
# 自定义图表标签和标题
plt.xlabel('日期')
plt.ylabel('价格')
plt.title('股票收盘价和开盘价')
plt.xticks(rotation=45) # 旋转x轴标签,使其更易读
# 添加图例
plt.legend()
# 显示图表
plt.tight_layout() # 自动调整图表布局,防止标签重叠
plt.show()