从tushare获取股票交易数据并写入mysql后,从mysql中读取数据,进行分析(使用pyecharts画K线)
参考文章: pyecharts官方文档
pyecharts官方实例
tushare结合pyecharts生成股票K线图
import os
import pandas as pd
from sqlalchemy import create_engine
import tushare as ts
from pyecharts import options as opts
from pyecharts.charts import Kline
conn = create_engine('mysql+pymysql://root:123456@localhost:3306/qtrade', encoding='utf8')
df = pd.read_sql("SELECT * FROM dailytrade WHERE ts_code = '000001.SZ' ", conn)
df=df.iloc[::-1]
ochl = df[['open', 'close', 'high', 'low']]
ochl_tolist = [ochl.iloc[i].tolist() for i in range(len(ochl))]
stocks = '000001.SZ'
c = (
Kline()
.add_xaxis(xaxis_data=list(df.trade_date))
.add_yaxis("", ochl_tolist)
.set_global_opts(
xaxis_opts=opts.AxisOpts(is_scale=True),
yaxis_opts=opts.AxisOpts(
is_scale=True,
splitarea_opts=opts.SplitAreaOpts(
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
),
datazoom_opts=[opts.DataZoomOpts()],
title_opts=opts.TitleOpts(title=stocks),
)
.render("kline_datazoom_slider.html")
)
os.system(c)
生成的K线图,与实际一致。
东方财富截图:
|