python-numpy股价统计分析应用

data.csv提取码:yccr文件中存储了股票的信息, 其中第4-8列,即EXCEL表格中的D-H列,
分别为股票的开盘价,最高价,最低价,收盘价,成交量。
分析角度:

  1. 计算成交量加权平均价格
    概念:成交量加权平均价格,英文名VWAP(Volume-Weighted Average Price,成交量加权平均价格)是一个非常重要的经
    济学量,代表着金融资产的“平均”价格。 某个价格的成交量越大,该价格所占的权重就越大。VWAP就是以成交量为权重计算出来的加权平均值。

  2. 计算最大值和最小值: 计算股价近期最高价的最大值和最低价的最小值

  3. 计算股价近期最高价的最大值和最小值的差值;----(极差)
    计算股价近期最低价的最大值和最小值的差值

  4. 计算收盘价的中位数

  5. 计算收盘价的方差

  6. 计算对数收益率, 股票收益率、年波动率及月波动率
    ***收盘价的分析常常是基于股票收益率的。
    股票收益率又可以分为简单收益率和对数收益率。
    简单收益率:是指相邻两个价格之间的变化率。
    对数收益率:是指所有价格取对数后两两之间的差值。
    # [1, 2,3 4] ======>[-1, ]
    ***使用的方法: NumPy中的diff函数可以返回一个由相邻数组元素的差值构成的数组。
    不过需要注意的是,diff返回的数组比收盘价数组少一个元素。

    ***在投资学中,波动率是对价格变动的一种度量,历史波动率可以根据历史价格数据计算得出。计算历史波动率时,需要用
    到对数收益率。
    年波动率等于对数收益率的标准差除以其均值,再乘以交易日的平方根,通常交易日取252天。
    月波动率等于对数收益率的标准差除以其均值,再乘以交易月的平方根。通常交易月取12月。

  7. 获取该时间范围内交易日周一、周二、周三、周四、周五分别对应的平均收盘价

  8. 平均收盘价最低,最高分别为星期几

import numpy as np
print("*******************1.计算成交量加权平均价格***************************")

params1 = dict(
    fname="doc/data.csv",
    delimiter=",",
    usecols=(6, 7),
    unpack=True)
# delimiter 分隔符 usecols 6-7列

# 收盘价,成交量,**params1 解包字典
endPrice, countNum = np.loadtxt(**params1)
# print(endPrice, countNum)

# average加权平均值
VWAP = np.average(endPrice, weights=countNum)
print("1. 计算成交量加权平均价格:", VWAP)

输出:

*******************1.计算成交量加权平均价格***************************
1. 计算成交量加权平均价格: 350.5895493532009
print("*****************2.计算最大值和最小值**************************")
params2 = dict(
    fname="doc/data.csv",
    delimiter=",",
    usecols=(4, 5),
    unpack=True)

# 最高价和最低价
highPrice, lowPrice = np.loadtxt(**params2)

# 最高价的最大值和最低价的最小值
print("2.最高价的最大值: ", highPrice.max())
print("2.最低价的最小值: ", lowPrice.min())

输出:

*****************2.计算最大值和最小值**************************
2.最高价的最大值:  364.9
2.最低价的最小值:  333.53
print("********************3.最大值和最小值的差值**************************")
# 计算股价近期最高价的最大值和最小值的差值;----(极差)
#     计算股价近期最低价的最大值和最小值的差值

print("3. 近期最高价的极差: ", np.ptp(highPrice))
print("3. 近期最低价的极差: ", np.ptp(lowPrice))

print("*******************4. 计算收盘价的中位数***************************")
# 计算收盘价的中位数
print("4. 计算收盘价的中位数:", np.median(countNum))

print("********************5. 计算收盘价的方差**************************")
# 计算收盘价的方差
print("5. 计算收盘价的方差:", np.var(endPrice))

输出:

********************3.最大值和最小值的差值**************************
3. 近期最高价的极差:  24.859999999999957
3. 近期最低价的极差:  26.970000000000027
*******************4. 计算收盘价的中位数***************************
4. 计算收盘价的中位数: 16557250.0
********************5. 计算收盘价的方差**************************
5. 计算收盘价的方差: 50.126517888888884
print("************************6. 对数收益率、年波动率、月波动率***********************************")
# 简单收益率:两两之差
simpleReturn = np.diff(endPrice)
print(simpleReturn)
# 对数收益率: 所有价格取对数后两两之间的差值。
logReturn = np.diff(np.log(endPrice))
print("6. 对数收益率:", logReturn)
# 年波动率等于对数收益率的标准差除以其均值,再乘以交易日的平方根,通常交易日取252天。
annual_vol = logReturn.std()/logReturn.mean()*np.sqrt(252)
print("6. 年波动率:",  annual_vol)
#  月波动率等于对数收益率的标准差除以其均值,再乘以交易月的平方根。通常交易月取12月。
month_vol = logReturn.std()/logReturn.mean()*np.sqrt(12)
print("6. 月波动率:", month_vol)

输出:

************************6. 对数收益率、年波动率、月波动率***********************************
6.简单收益率: [  3.22   5.71  -0.71  -0.88   3.06   5.38   3.32   2.96  -3.62   2.31
   2.33   0.72   3.23  -4.83  -7.74 -11.95   4.01   0.26   5.28   5.05
  -3.9    2.81   7.44   0.44  -4.64   0.4   -3.29  -5.8    5.32]
6. 对数收益率: [ 0.00953488  0.01668775 -0.00205991 -0.00255903  0.00887039  0.01540739
  0.0093908   0.0082988  -0.01015864  0.00649435  0.00650813  0.00200256
  0.00893468 -0.01339027 -0.02183875 -0.03468287  0.01177296  0.00075857
  0.01528161  0.01440064 -0.011103    0.00801225  0.02090904  0.00122297
 -0.01297267  0.00112499 -0.00929083 -0.01659219  0.01522945]
6. 年波动率: 129.27478991115134
6. 月波动率: 28.210071915112593

做第七题时需要知道如何获取星期数:
在这里插入图片描述在这里插入图片描述

print("***********************7.周一到周五的平均收盘价***********************")
def get_week(date):
    """根据传入的日期28-01-2011获取星期数, 0-星期一"""
    from datetime import datetime
    # 默认传入的不是字符串, 是bytes类型;
    date = date.decode('utf-8')
    return datetime.strptime(date, "%d-%m-%Y").weekday()
params3 = dict(
    fname="doc/data.csv",
    delimiter=",",
    usecols=(1, 6),
    converters={1: get_week},
    unpack=True)
# 星期数和收盘价
week, endPrice = np.loadtxt(**params3)
allAvg = []
for weekday in range(5):
    average = endPrice[week == weekday].mean()
    allAvg.append(average)
    print("7. 星期%s的平均收盘价:%s" % (weekday + 1, average))

输出:

*********************8.最高、最低平均收盘价星期*************************
8.平均收盘价最低是星期 5
8. 平均收盘价最高是星期 3

完整代码:


import numpy as np

print("*******************1.计算成交量加权平均价格***************************")

params1 = dict(
    fname="doc/data.csv",
    delimiter=",",
    usecols=(6, 7),
    unpack=True)
# delimiter 分隔符 usecols 6-7列
#
# 收盘价,成交量,**params1 解包字典
endPrice, countNum = np.loadtxt(**params1)
print(endPrice, countNum)

# average加权平均值
VWAP = np.average(endPrice, weights=countNum)
print("1. 计算成交量加权平均价格:", VWAP)

print("*****************2.计算最大值和最小值**************************")
params2 = dict(
    fname="doc/data.csv",
    delimiter=",",
    usecols=(4, 5),
    unpack=True)
#
# # 最高价和最低价
highPrice, lowPrice = np.loadtxt(**params2)
#
# # 最高价的最大值和最低价的最小值
print("2.最高价的最大值: ", highPrice.max())
print("2.最低价的最小值: ", lowPrice.min())

print("********************3.最大值和最小值的差值**************************")
# 计算股价近期最高价的最大值和最小值的差值;----(极差)
#     计算股价近期最低价的最大值和最小值的差值

print("3. 近期最高价的极差: ", np.ptp(highPrice))
print("3. 近期最低价的极差: ", np.ptp(lowPrice))

print("*******************4. 计算收盘价的中位数***************************")
# 计算收盘价的中位数
print("4. 计算收盘价的中位数:", np.median(countNum))

print("********************5. 计算收盘价的方差**************************")
# 计算收盘价的方差
print("5. 计算收盘价的方差:", np.var(endPrice))



print("************************6. 对数收益率、年波动率、月波动率***********************************")
# 简单收益率:两两之差
simpleReturn = np.diff(endPrice)
print('6.简单收益率:',simpleReturn)
# 对数收益率: 所有价格取对数后两两之间的差值。
logReturn = np.diff(np.log(endPrice))
print("6. 对数收益率:", logReturn)
# 年波动率等于对数收益率的标准差除以其均值,再乘以交易日的平方根,通常交易日取252天。
annual_vol = logReturn.std()/logReturn.mean()*np.sqrt(252)
print("6. 年波动率:",  annual_vol)
#  月波动率等于对数收益率的标准差除以其均值,再乘以交易月的平方根。通常交易月取12月。
month_vol = logReturn.std()/logReturn.mean()*np.sqrt(12)
print("6. 月波动率:", month_vol)

print("***********************7.周一到周五的平均收盘价***********************")
def get_week(date):
    """根据传入的日期28-01-2011获取星期数, 0-星期一"""
    from datetime import datetime
    # 默认传入的不是字符串, 是bytes类型;
    date = date.decode('utf-8')
    return datetime.strptime(date, "%d-%m-%Y").weekday()
params3 = dict(
    fname="doc/data.csv",
    delimiter=",",
    usecols=(1, 6),
    converters={1: get_week},
    unpack=True)
# 星期数和收盘价
week, endPrice = np.loadtxt(**params3)
allAvg = []
for weekday in range(5):
    average = endPrice[week == weekday].mean()
    allAvg.append(average)
    print("7. 星期%s的平均收盘价:%s" % (weekday + 1, average))



print("*********************8.最高、最低平均收盘价星期*************************")
# [12, 23, 34, 45, 56]
print("8.平均收盘价最低是星期", np.argmin(allAvg) + 1)
print("8. 平均收盘价最高是星期", np.argmax(allAvg) + 1)

在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43067754/article/details/87966948