Python 金融之均线图
本文将探讨如何下载股票历史行情数据,利用pyecharts来画股票的均线图
#调入pyecharts相应的库
from pyecharts import options as opts
from pyecharts.charts import Kline
from pyecharts.globals import ThemeType #主题
from pyecharts.charts import Bar, Grid, Line,Scatter
import pandas as pd
import numpy as np
#登录jqdata
import jqdatasdk as jdd
jdd.auth('ID’,'密码') # 填写申请jqdata的ID和密码
from pyecharts.globals import ThemeType
#获取所有股票代码
allStock=jdd.get_all_securities(types=['stock'], date=None)
#取股票历史行情数据
stock =jdd.get_price(allStock.index[0], start_date='2019-02-12', end_date='2020-02-12', frequency='daily')
stockData=stock[['open','close','low','high']].values.tolist()
stockDate=stock.index.astype(str).tolist()
data=stock['close'].values.tolist()
#计算移动平均值
def calculate_ma(day_count: int, d):
result: List[Union[float, str]] = []
for i in range(len(d)):
if i < day_count:
result.append("-")
continue
sum_total = 0.0
for j in range(day_count):
sum_total += float(d[i - j][1])
result.append(abs(float("%.2f" % (sum_total / day_count))))
return result
def line() -> Line:
c = (
Line(init_opts=opts.InitOpts(theme=ThemeType.DARK,page_title=allStock.display_name[0]+'走势图'))
.add_xaxis(stockDate)
.add_yaxis(
series_name="行情",
y_axis=data,
is_smooth=True,
is_hover_animation=False,
linestyle_opts=opts.LineStyleOpts(width=2, opacity=1),
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
series_name="MA2",
y_axis=calculate_ma(day_count=2, d=stockData),
is_smooth=True,
is_hover_animation=False,
linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5),
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
series_name="MA5",
y_axis=calculate_ma(day_count=5, d=stockData),
is_smooth=True,
is_hover_animation=False,
linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5),
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
series_name="MA10",
y_axis=calculate_ma(day_count=10, d=stockData),
is_smooth=True,
is_hover_animation=False,
linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5),
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
series_name="MA20",
y_axis=calculate_ma(day_count=20, d=stockData),
is_smooth=True,
is_hover_animation=False,
linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5),
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
title_opts=opts.TitleOpts(title=allStock.display_name[0]+'走势图'),
datazoom_opts=[opts.DataZoomOpts(type_="inside")],
yaxis_opts=opts.AxisOpts(min_='dataMin'),
)
)
return c
line().render('pingantu.html')
将输出pingantu.html的文件,截图如下: