tushare ID:505405
import pyecharts.options as opts
from pyecharts.charts import Kline
import tushare as ts
def daily_kline(token,ts_code,start_date,end_date):
#调用获取股票交易日数据(x轴数据)
date = get_tushare_kline_date(token, ts_code, start_date, end_date)
# 调用获取股票open,close,low,high数据(y轴数据)
data = get_tushare_kline_data(token, ts_code, start_date, end_date)
# #调用画图函数
plot_kline(date, data)
def get_tushare_kline_date(token,ts_code,start_date,end_date):
pro = ts.pro_api(token)
# 拉取日期数据
date = pro.daily(**{
"ts_code": ts_code ,
"trade_date": "",
"start_date": start_date,
"end_date": end_date,
"offset": "",
"limit": ""
}, fields=[
"trade_date"
])
date.values.tolist()
return date
def get_tushare_kline_data(token,ts_code,start_date,end_date):
pro = ts.pro_api(token)
#拉取k线数据
data = pro.daily(**{
"ts_code": ts_code,
"trade_date": "",
"start_date": start_date,
"end_date": end_date,
"offset": "",
"limit": ""
}, fields=[
"open",
"high",
"low",
"close"
])
data.values.tolist()
return data
def plot_kline(date,data):
c = (
Kline()
.add_xaxis(date.values.tolist())
.add_yaxis(
"kline",
data[['open','close','low','high']].values.tolist(),
itemstyle_opts=opts.ItemStyleOpts(#系列配置项-图元样式
color="#ec0000",#涨
color0="#00da3c",#跌
border_color="#8A0000",#涨外框
border_color0="#008F28",#跌外框
),
)
.set_global_opts(
xaxis_opts=opts.AxisOpts(is_scale=True),#全局-x坐标刻度不会强制包含零刻度
yaxis_opts=opts.AxisOpts(#全局-y轴
is_scale=True,#y坐标刻度不会强制包含零刻度
splitarea_opts=opts.SplitAreaOpts(#系列配置项-分割线
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)#分割线 线风格-系列配置项-线样式-分割线透明度
),
),
datazoom_opts=[opts.DataZoomOpts(type_="slider")],#全局-区域缩放——组件类型slider 原本是inside
title_opts=opts.TitleOpts(title="Kline-ItemStyle"),#全局-标题-标题
)
.render("kline.html")#保存本地
)
if __name__ == "__main__":
#tushare的token
token = '你的tusharetoken'
#股票代码(tushare格式的)
ts_code = '600519.SH'
#开始日期(tushare格式的)例:20220101
start_date = None
#结束日期(tushare格式的)例:20220601
end_date = None
#画日k线
daily_kline(token, ts_code, start_date, end_date)
我们首先导入需要的库
数据用tushare——我目前用的 很不错
画图用pyecharts——做出来的图功能很全
import pyecharts.options as opts
from pyecharts.charts import Kline
import tushare as ts
我们定义四个函数
第一个函数 画日k线 里面嵌套获取x轴数据——日期 以及获取y轴数据——开盘收盘最低最高
以及画图函数
def daily_kline(token,ts_code,start_date,end_date):
#调用获取股票交易日数据(x轴数据)
date = get_tushare_kline_date(token, ts_code, start_date, end_date)
# 调用获取股票open,close,low,high数据(y轴数据)
data = get_tushare_kline_data(token, ts_code, start_date, end_date)
# #调用画图函数
plot_kline(date, data)
def get_tushare_kline_date(token,ts_code,start_date,end_date):
pro = ts.pro_api(token)
# 拉取日期数据
date = pro.daily(**{
"ts_code": ts_code ,
"trade_date": "",
"start_date": start_date,
"end_date": end_date,
"offset": "",
"limit": ""
}, fields=[
"trade_date"
])
date.values.tolist()
return date
def get_tushare_kline_data(token,ts_code,start_date,end_date):
pro = ts.pro_api(token)
#拉取k线数据
data = pro.daily(**{
"ts_code": ts_code,
"trade_date": "",
"start_date": start_date,
"end_date": end_date,
"offset": "",
"limit": ""
}, fields=[
"open",
"high",
"low",
"close"
])
data.values.tolist()
return data
def plot_kline(date,data):
c = (
Kline()
.add_xaxis(date.values.tolist())
.add_yaxis(
"kline",
data[['open','close','low','high']].values.tolist(),
itemstyle_opts=opts.ItemStyleOpts(#系列配置项-图元样式
color="#ec0000",#涨
color0="#00da3c",#跌
border_color="#8A0000",#涨外框
border_color0="#008F28",#跌外框
),
)
.set_global_opts(
xaxis_opts=opts.AxisOpts(is_scale=True),#全局-x坐标刻度不会强制包含零刻度
yaxis_opts=opts.AxisOpts(#全局-y轴
is_scale=True,#y坐标刻度不会强制包含零刻度
splitarea_opts=opts.SplitAreaOpts(#系列配置项-分割线
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)#分割线 线风格-系列配置项-线样式-分割线透明度
),
),
datazoom_opts=[opts.DataZoomOpts(type_="slider")],#全局-区域缩放——组件类型slider 原本是inside
title_opts=opts.TitleOpts(title="Kline-ItemStyle"),#全局-标题-标题
)
.render("kline.html")#保存本地
)
最后我们主函数:
if __name__ == "__main__":
#tushare的token
token = '4214d9fcf9740e5d3369bfbb18bdb1a4ce88c532a88ce2af61c370a3'
#股票代码(tushare格式的)
ts_code = '600519.SH'
#开始日期(tushare格式的)例:20220101
start_date = None
#结束日期(tushare格式的)例:20220601
end_date = None
#画日k线
daily_kline(token, ts_code, start_date, end_date)
仅作分享
import pyecharts.options as opts from pyecharts.charts import Kline import tushare as ts def daily_kline(token,ts_code,start_date,end_date): #调用获取股票交易日数据(x轴数据) date = get_tushare_kline_date(token, ts_code, start_date, end_date) # 调用获取股票open,close,low,high数据(y轴数据) data = get_tushare_kline_data(token, ts_code, start_date, end_date) # #调用画图函数 plot_kline(date, data) def get_tushare_kline_date(token,ts_code,start_date,end_date): pro = ts.pro_api(token) # 拉取日期数据 date = pro.daily(**{ "ts_code": ts_code , "trade_date": "", "start_date": start_date, "end_date": end_date, "offset": "", "limit": "" }, fields=[ "trade_date" ]) date.values.tolist() return date def get_tushare_kline_data(token,ts_code,start_date,end_date): pro = ts.pro_api(token) #拉取k线数据 data = pro.daily(**{ "ts_code": ts_code, "trade_date": "", "start_date": start_date, "end_date": end_date, "offset": "", "limit": "" }, fields=[ "open", "high", "low", "close" ]) data.values.tolist() return data def plot_kline(date,data): c = ( Kline() .add_xaxis(date.values.tolist()) .add_yaxis( "kline", data[['open','close','low','high']].values.tolist(), itemstyle_opts=opts.ItemStyleOpts(#系列配置项-图元样式 color="#ec0000",#涨 color0="#00da3c",#跌 border_color="#8A0000",#涨外框 border_color0="#008F28",#跌外框 ), ) .set_global_opts( xaxis_opts=opts.AxisOpts(is_scale=True),#全局-x坐标刻度不会强制包含零刻度 yaxis_opts=opts.AxisOpts(#全局-y轴 is_scale=True,#y坐标刻度不会强制包含零刻度 splitarea_opts=opts.SplitAreaOpts(#系列配置项-分割线 is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)#分割线 线风格-系列配置项-线样式-分割线透明度 ), ), datazoom_opts=[opts.DataZoomOpts(type_="slider")],#全局-区域缩放——组件类型slider 原本是inside title_opts=opts.TitleOpts(title="Kline-ItemStyle"),#全局-标题-标题 ) .render("kline.html")#保存本地 ) if __name__ == "__main__": #tushare的token token = '4214d9fcf9740e5d3369bfbb18bdb1a4ce88c532a88ce2af61c370a3' #股票代码(tushare格式的) ts_code = '600519.SH' #开始日期(tushare格式的)例:20220101 start_date = None #结束日期(tushare格式的)例:20220601 end_date = None #画日k线 daily_kline(token, ts_code, start_date, end_date)