好友
阅读权限10
听众
最后登录1970-1-1
|
import baostock as bs
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Kline, Grid, Line, Bar
code = "sh.603335"
start_date = '2021-11-01'
end_date = '2021-12-03'
#### 登陆系统 ####
lg = bs.login()
# 显示登陆返回信息
print('login respond error_code:' + lg.error_code)
print('login respond error_msg:' + lg.error_msg)
#### 获取沪深A股历史K线数据 ####
# 详细指标参数,参见“历史行情指标参数”章节;“分钟线”参数与“日线”参数不同。“分钟线”不包含指数。
# 分钟线指标:date,time,code,open,high,low,close,volume,amount,adjustflag
# 周月线指标:date,code,open,high,low,close,volume,amount,adjustflag,turn,pctChg
rs = bs.query_history_k_data_plus(code,
"date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST",
start_date=start_date, end_date=end_date,
frequency="d", adjustflag="3")
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)
# print(result.dtypes)
# exit()
result['当日振幅'] = (pd.to_numeric(result['high']) - pd.to_numeric(result['low'])) / pd.to_numeric(result['low']) * 100
#### 结果集输出到csv文件 ####
# result.to_csv("D:\\history_A_stock_k_data.csv", index=False)
# print(result)
#### 登出系统 ####
bs.logout()
x_line = result['date'].tolist() # 列表
k_oclh = result[['open', 'close', 'low', 'high']].values.tolist() # 获取K线数据转换成列表
# K线主图
kline = (
Kline()
.add_xaxis(x_line)
.add_yaxis(code, k_oclh)
.set_global_opts(
xaxis_opts=opts.AxisOpts(is_scale=True, ), # is_show=False
yaxis_opts=opts.AxisOpts(
is_scale=True,
splitarea_opts=opts.SplitAreaOpts(
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
),
datazoom_opts=[ # 两个zoom可以起到上下两张图都能控制缩放,否则只有最上面的一张图控制缩放
opts.DataZoomOpts(is_show=False, type_="inside", xaxis_index=[0, 1, 2]),
opts.DataZoomOpts(is_show=True, type_="slider", xaxis_index=[0, 1, 2]),
opts.DataZoomOpts(is_show=True, type_="slider", xaxis_index=[0, 1, 2], pos_top="98%"),
],
title_opts=opts.TitleOpts(title='爿θ垲垲'),
tooltip_opts=opts.TooltipOpts(
trigger="axis",
is_show=True,
trigger_on='mousemove|click',
axis_pointer_type="cross",
background_color="rgba(245, 245, 245, 0.8)",
border_width=1,
border_color="#ccc",
textstyle_opts=opts.TextStyleOpts(color="#000")),
axispointer_opts=opts.AxisPointerOpts( # 坐标轴指示器,上下图十字线连在一起
is_show=True, # 默认显示坐标轴指示器
link=[{"xAxisIndex": "all"}],
# 不同轴的 axisPointer 可以进行联动,在这里设置。
# 联动表示轴能同步一起活动。link 是一个数组,其中每一项表示一个 link group,一个 group 中的坐标轴互相联动。
label=opts.LabelOpts(background_color="#777")), # 坐标轴指示器的文本标签,坐标轴标签配置项,
)
)
zf = result['当日振幅'].tolist()
line = (
Line()
.add_xaxis(xaxis_data=x_line)
# .add_yaxis("涨跌幅", zdf, is_connect_nones=True,)
.add_yaxis("当日振幅", zf, is_connect_nones=True, )
.set_global_opts(
xaxis_opts=opts.AxisOpts(is_scale=True, is_show=False), # 不显示x轴
title_opts=opts.TitleOpts(title="", subtitle="", pos_top="47%"),
legend_opts=opts.LegendOpts(pos_top="47%"), )
.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) # 隐藏图例标签数字
)
volume = result['volume'].tolist()
bar = (
Bar()
.add_xaxis(x_line)
.add_yaxis("成交量", volume, itemstyle_opts=opts.ItemStyleOpts(color="#F948F7"), )
.set_global_opts(
title_opts=opts.TitleOpts(title="", subtitle="", pos_top="70%"),
legend_opts=opts.LegendOpts(pos_top="72%"), )
.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) # 隐藏图例标签数字
)
grid = (
Grid(init_opts=opts.InitOpts(width="1600px", height="720px", animation_opts=opts.AnimationOpts(animation=False)))
.add(kline, grid_opts=opts.GridOpts(pos_top="1%", height="42%")) # bottm为底部距离,top为顶部距离,也可以为百分数
.add(line, grid_opts=opts.GridOpts(pos_top="48%", height="20%"), ) # height为单个图的高度
.add(bar, grid_opts=opts.GridOpts(pos_top="70%", height="20%"), )
# .render('C:/Users/lizy1/Desktop/' + code + "日线图.html")
.render(code + "日线图.html")
)
print(code + 'K线图_日线图 已完成')
exit()
|
|