万达院线股价分析
数据来源:新浪财经、腾讯财经、上交所和深交所
分析时间:2017/9/11
分析目的:分析维度练习
数据采集
1.基本面数据
#coding=utf-8 import pandas as pd import tushare as ts #获取万达院线(股票代码002739)第2季度的基本面数据,可能未更新没有输出 b1 = ts.get_report_data(2017,2) #业绩报告 b2 = ts.get_profit_data(2017,2) #盈利能力 b3 = ts.get_operation_data(2017,2) #营运能力 b4 = ts.get_growth_data(2017,2) #成长能力 b5 = ts.get_debtpaying_data(2017,2) #偿债能力 b6 = ts.get_cashflow_data(2017,2) #现金流量 basic = pd.concat([b1,b2,b3,b4,b5,b6]) bData = basic.ix[{'code':'002739'}] #print bData #basic.to_csv('C:/Users/HP/Desktop/601988basic.csv',encoding = 'utf-8')
2.历史行情
#coding=utf-8 import pandas as pd import numpy as np import matplotlib.pyplot as plt from pandas import Series,DataFrame from matplotlib.pylab import datestr2num import matplotlib.finance as mpf data = ts.get_hist_data('002739',start='2017-01-01') data.describe()
数据分析
频率分析
1.可视化维度信息
import scipy.stats as st openY = st.norm.fit(data['open']) print '拟合函数',openY mu, sigma = 55.37, 2.17 xs = data['open'] F = st.norm.pdf(xs, loc=mu, scale=sigma) plt.plot(xs, F,label='Norm Distribution') plt.legend(loc = 'best') plt.show() print 'mean of data is: ' + str(np.mean(F)) print 'standard deviation of data is: ' + str(np.std(F))
2.计算收益分布
import tushare as ts %matplotlib inline data = ts.get_hist_data('002739',start='2017-01-01')['p_change'] #p_change涨跌幅 print data data[-1] = 1 returns = (1 + data).cumprod()-1 #print returns #累计收益 returns.plot(legend = True,figsize = (15,10))
注:收益分布图两段趋平,暂时还没搞清楚是什么原因
探索性分析
1.分析均价、收盘价、均量、成交量、换手率等指标相关性
#散点图分析各指标的相关性 data = ts.get_hist_data('002739',start='2017-01-01') pieces = data[['close', 'price_change', 'ma20','volume', 'v_ma20', 'turnover']] pd.scatter_matrix(pieces) cov = np.corrcoef(pieces.T) img = plt.matshow(cov,cmap=plt.cm.winter) plt.colorbar(img, ticks=[-1,0,1]) plt.show()
2.移动窗口和指数加权
data = ts.get_hist_data('002739',start='2017-01-01')['close'] fig,ax = plt.subplots(figsize=(20,10)) #移动窗口中位数 ma100 = pd.rolling_mean(data,10,min_periods = 10) plt.subplot(211) data.plot(style = 'k-',label = 'nomal') ma100.plot(style = 'k--',label = 'Simple MA') plt.legend(loc = 'best') plt.grid(True) plt.subplot(212) ewma100 = pd.ewma(data,span = 10) data.plot(style = 'k-',label = 'nomal') ewma100.plot(style = 'k--',label = 'Exponetially-weighted MA') plt.legend(loc = 'best') plt.grid(True)
关系分析
from pyecharts import Kline data = ts.get_hist_data('002739',start='2017-01-01') new_data = data.ix[:,['open','close','high','low']] train_data = np.array(new_data) train_index = np.array(data.index) kline = Kline("2017年万达院线K线图") kline.add("每日K线",train_index.tolist() ,train_data.tolist()) kline
注:matplotlib画K线图可能出现断裂,使用pyechart画K线图比matplotlib简便,pyechart需要的数据类型为列表
plt.rc('font', family='SimHei', size=13) #解决图例中文乱码 """ 画图的三种写法: fig,axes = plt.subplots(1,1,figsize=(22,10)) 1.plt.plot(data):最直接的写法 2.plt.plot_date(x_date,data['high']):用于多个趋势图 3.data[['close','volume']].plot(ax =axes): 数据类型直接绘图 """ #最高价、开盘价与收盘价 data = ts.get_hist_data('002739',start='2017-01-01') x = range(len(data)) x_date = [datestr2num(i) for i in data.index] plt.figure(figsize=(22,10)) plt.title(u"2017年万达院线历史最高价、开盘价与收盘价走势折线图") plt.xlabel(u"时间") plt.ylabel(u"指数") plt.plot_date(x_date,data['high'],'-',color='r',label=u"最高价") plt.plot_date(x_date,data['open'],'-',color='g',label=u"开盘价") plt.plot_date(x_date,data['close'],'-',color='b',label=u"收盘价") plt.legend() plt.grid(True) #收盘价与成交量 fig,axes = plt.subplots(1,1,figsize=(22,10)) plt.title(u"2017年万达院线收盘价和成交量趋势折线图") data[['close','volume']].plot(ax =axes,secondary_y='volume', grid=True) #日均线与换手率 fig = plt.figure(figsize=(22,15)) ax1 = plt.subplot2grid((4,4),(0,0),rowspan = 3,colspan = 4) #plt.subplot(211) 这样两子图的大小相同,展示不好看 Ma5 = data['ma5'] Ma5.plot(kind = 'line',ax = ax1,color = 'r',label=u"5日均价") Ma10 = data['ma10'] Ma10.plot(kind = 'line',ax = ax1,color = 'g',label=u"10日均价") Ma20 = data['ma20'] Ma20.plot(kind = 'line',ax = ax1,color = 'b',label=u"20日均价") plt.legend(loc = 'best') plt.grid(True) #plt.subplot(212) ax2 = plt.subplot2grid((4,4),(3,0),rowspan = 1,colspan = 4) TurnOver = data['turnover'] TurnOver.plot(kind='bar',ax = ax2,color = 'black') ax1.set_title(u"2017年万达院线日均价折线图") ax1.set_ylabel(u"股价(元)") ax2.set_ylabel(u"换手率") ax2.set_xlabel(u"日期") plt.grid(True) plt.setp(ax1.get_xticklabels(),visible = False) plt.setp(ax1.yaxis.get_ticklabels()[0],visible = False) #隐藏y轴的第一个标签 plt.subplots_adjust(bottom = 0.2,top = 0.9,hspace = 0) #调整子图间距 plt.show()
其它
tushare提供获取全国影院单日票房排行数据的接口,可以通过特定日期(比如股价涨停日等)观察万达影院票房信息
df = ts.day_cinema('2017-06-07') #取指定日期的数据 df.head(10)
注:tushare数据只更新到2017/07/03