微信公众号:数据皮皮侠
如果你觉得该公众号对你有帮助,欢迎关注、推广和宣传
内容目录:时间序列 Python实现
时间序列——股票分析入门获取股票数据
时间序列——股票分析入门
目前,获取股票数据的渠道有很多,而且基本上是免费的,比如,行情
软件有同花顺、东方财富等,门户网站有新浪财经、腾讯财经、和讯网等。
Python也有不少免费的开源api可以获取交易行情数据,如pandas自带的
库,tushare和baostock等。由于pandas库不再支持yahoo数据库后变
得很不好用,而baostock最早记录的数据是2006年,因此本文主要讲讲如
何使用tushare获取股票交易数据和可视化分析,tushare基本上记录了股
票自上市之日起所有的日交易数据,是目前分析国内A股(支持其他非股票行
情数据,如期货)比较好用的开源接口。
获取股票数据
0_1
使用tushare包的get_hist_data('')函数来获取股票交易数据,具体可以通过命令help(ts.get_hist_data)了解函数和参数含义。
import tushare as ts
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.ticker as ticker
from mpl_finance import candlestick2_ochl
data=ts.get_hist_data('002115')
data=data.iloc[:,1:]
data.head()
data.index=pd.to_datetime(data.index)
显示结果:
Out[2]:
high close low ... v_ma5 v_ma10 v_ma20
date ...
2020-04-22 9.67 9.65 9.44 ... 141323.04 221818.26 215873.91
2020-04-21 9.57 9.56 9.41 ... 177963.39 240545.90 221940.49
2020-04-20 9.68 9.63 9.45 ... 195733.58 259686.87 226655.42
2020-04-17 9.77 9.49 9.46 ... 199176.87 260217.42 228673.13
2020-04-16 9.63 9.58 9.43 ... 235355.33 262151.49 233287.14
0_2
#收盘价格
close=data.close
plt.rcParams['font.sans-serif'] = [u'SimHei']
fig1 = plt.figure(figsize=(18,10))
plt.plot(close['2016'],linewidth=3)
plt.plot(close['2017'],'k',linewidth=3)
plt.plot(close['2018'],'r--',linewidth=3)
plt.plot(close['2019'],'b-',linewidth=3)
plt.grid(True,axis='y')
plt.title('湖南黄金2016-2019年收盘价曲线',fontsize='20',fontweight='light')
plt.xlabel('日期',fontsize=15)
plt.ylabel('价格',fontsize=15)
显示结果:

从图上可以湖南黄金在2017-10-2020-01经历了一波大牛市,股票价格增长1倍,涨幅为100%,但是如果你没有把握机会,结果一不小心有回到了解放前。
0_3
#三种成交量对比
v_ma5=data.v_ma5
v_ma10=data.v_ma10
v_ma20=data.v_ma20
fig2 = plt.figure(figsize=(22,10))
plt.plot(v_ma5,c='r',label='5日平均成交量',linewidth=3)
plt.plot(v_ma10,c='b',label='10日平均成交量',linewidth=3)
plt.plot(v_ma20,c='black',label='20日平均成交量',linewidth=3)
plt.legend(loc='best')
plt.title('湖南黄金2016-2019年三种价格曲线对比图',fontsize='20',fontweight='light')
plt.xlabel('日期',fontsize=15)
plt.ylabel('价格',fontsize=15)
plt.grid(True,axis='both')

0_4
#三种价格对比
close=data.close
high=data.high
low=data.low
fig3 = plt.figure(figsize=(22,10))
plt.plot(close,c='r',label='收盘价',linewidth=3)
plt.plot(high,c='silver',label='最高价',linewidth=3)
plt.plot(low,c='black',label='最低价',linewidth=3)
plt.legend(loc='best')
plt.title('湖南黄金2016-2019年三种价格曲线对比图',fontsize='20',fontweight='light')
plt.xlabel('日期',fontsize=15)
plt.ylabel('价格',fontsize=15)
plt.grid(True,axis='both')
显示结果:

0_5
#收盘价格核估计的图
close.describe()
plt.hist(close,20)
import seaborn as sns
sns.kdeplot(close)
sns.distplot(close,color="g")
plt.grid(True,axis='both')
显示结果:

0_6
#K线图
begin_time = '2018-10-24'
end_time = '2019-4-24'
code = "601988"
df = ts.get_hist_data(code, start=begin_time, end=end_time)
df = df.sort_index(0)
df_idx = df.index.values
fig, ax = plt.subplots(figsize=(20, 12))
candlestick2_ochl(ax = ax,
opens=df["open"].values, closes=df["close"].values,
highs=df["high"].values, lows=df["low"].values,
width=0.75, colorup='r', colordown='g', alpha=0.75)
ax.xaxis.set_major_locator(ticker.MaxNLocator(20))
# 设置自动格式化时间。
def mydate_formatter(x,pos):
try:
return df_idx[int(x)]
except IndexError:
return ''
ax.xaxis.set_major_formatter(ticker.FuncFormatter(mydate_formatter))
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
ax.grid(True)
plt.rcParams['font.sans-serif'] = [u'SimHei']
plt.title('湖南黄金(股票代码:002115)半年以来的K线图',fontsize='20',fontweight='light')
plt.xlabel('日期',fontsize=15)
plt.ylabel('价格',fontsize=15)
plt.show()
显示结果:

这次先可视化看看,下次做一些统计分析!

