这几天刚刚学了数据分析,今天就拿一些数据来做一个简单的分析练练手。有什么不对的地方欢迎批评指教。
样本数据↓
从第一列开始,这些字段分别是,公司名称,日期,开盘价,最高价,最低价,收盘价,成交量。
1.通过收盘价分析股价高低对成交量的影响以及这只股票近期的价格趋势
第一步先加载收盘价和成交量
c, v = np.loadtxt('data.csv', delimiter=',', usecols=(6, 7), unpack=True )
得到的数据如下:
c
array([336.1 , 339.32, 345.03, 344.32, 343.44, 346.5 , 351.88, 355.2 ,
358.16, 354.54, 356.85, 359.18, 359.9 , 363.13, 358.3 , 350.56,
338.61, 342.62, 342.88, 348.16, 353.21, 349.31, 352.12, 359.56,
360. , 355.36, 355.76, 352.47, 346.67, 351.99])
v
array([21144800., 13473000., 15236800., 9242600., 14064100., 11494200.,
17322100., 13608500., 17240800., 33162400., 13127500., 11086200.,
10149000., 17184100., 18949000., 29144500., 31162200., 23994700.,
17853500., 13572000., 14395400., 16290300., 21521000., 17885200.,
16188000., 19504300., 12718000., 16192700., 18138800., 16824200.])
第二步计算收盘价的平均价格及加权平均价格
平均价格
mean = np.average(c) # 相当于np.mean(c)
mean
得到平均价格为:
351.0376666666667
以成交量为权重的平均价格
vmean = np.average(c, weights=v)
vmean
得到的加权平均价格为:
350.5895493532009
以时间为权重的平均价格
通过观察,数据是通过时间升序排列的
t = np.arange(len(c)) # 模拟时间权重
tmean = np.average(c, weights=t)
tmean
得到的加权平均价格为:
352.4283218390804
绘制时间-收盘价曲线图
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
# 读取时间序列
datelist = pd.read_csv('data.csv', header=None)[1]
# 对时间序列进行格式化操作
x = []
for i in datelist:
i = datetime.strptime(i, '%d-%m-%Y')
x.append(datetime.strftime(i, '%Y-%m-%d'))
# 调整画布
plt.figure(figsize=(8, 4))
plt.subplot(111, facecolor='black')
# 画出时间-收盘价曲线图
plt.plot(x, c, color='g')
# 对图形进行调整
plt.title('时间-收盘价曲线图')
plt.grid(axis='x', alpha=.2)
plt.xticks(rotation=60)
plt.show()
结论:
以成交量为权重的平均价格相比平均价格要低一些,说明股票的价格对成交量的影响是价格越低,其成交量越高。
以时间为为权重的平均价格相比平均价格要高一些,再观察时间-收盘价曲线图,可以得出结论,时间越靠后,价格越稳定,比较前几天的价格数据,后面时间的股价要较高一些。
2.计算这些日期股票的收益率并绘制图表
首先第一步先加载出每一天的收盘价
c = np.loadtxt('data.csv', delimiter=',', usecols=(6, ), unpack=True)
c
array([336.1 , 339.32, 345.03, 344.32, 343.44, 346.5 , 351.88, 355.2 ,
358.16, 354.54, 356.85, 359.18, 359.9 , 363.13, 358.3 , 350.56,
338.61, 342.62, 342.88, 348.16, 353.21, 349.31, 352.12, 359.56,
360. , 355.36, 355.76, 352.47, 346.67, 351.99])
计算每一天的收益率(第一天的收益率等于后一天的价格减前一天的价格除以前一天的价格)
profit = np.diff(c) / c[:-1] # 长度减一
profit
array([ 0.00958048, 0.01682777, -0.00205779, -0.00255576, 0.00890985,
0.0155267 , 0.00943503, 0.00833333, -0.01010721, 0.00651548,
0.00652935, 0.00200457, 0.00897472, -0.01330102, -0.02160201,
-0.03408832, 0.01184253, 0.00075886, 0.01539897, 0.01450483,
-0.01104159, 0.00804443, 0.02112916, 0.00122372, -0.01288889,
0.00112562, -0.00924781, -0.0164553 , 0.01534601])
绘制日收益率曲线图
# 读取数据
dates = pd.read_csv('data.csv', header=None)[1][:-1]
# 除去日期的最后一天,并对日期进行格式化操作
x = []
for date in dates:
date= datetime.strptime(date, '%d-%m-%Y')
x.append(date.strftime('%Y-%m-%d'))
# 找出收益率最高与最低的点的位置
max_index = np.where(profit == profit.max())[0][0]
min_index = np.where(profit == profit,min())[0][0]
# 这里[0][0]是因为得到的index是一个数组,所以通过这种方式见它取出来
# 找出平均值
profitmean = np.mean(profit)
# 绘制日收益图
plt.figure(figsize=(10, 6))
plt.subplot(111, facecolor='black')
plt.plot(x, returns, color='c')
plt.xticks(rotation=60)
plt.grid(axis='x', alpha=.3)
# 画出平均值线
plt.axhline(y=profitmean, ls='-.', color='b', alpha=.5)
# 设置最大最小值的说明文字
max = x[max_index]+'\nmax:'+str('%.4f' % (profit.max()*100)) + '%'
min = x[min_index]+ '\nmin:'+str("%.4f" % (returns.min()*100))+'%'
plt.annotate(s=max, xy=(x[max_index], profit.max()), color='r', xytext=(24, profit.max()-0.0015), arrowprops=dict(arrowstyle='->', color='r'))
plt.annotate(s=min, xy=(x[min_index], returns.min()), color='g',
xytext=(10,returns.min()),
arrowprops=dict(arrowstyle='->', color='g'))
plt.show()