__TITLE__ = "利用Numpy进行历史股价分析"
__DATASOURCE__ = "ATAGURU"
# CSV文件读取
import numpy as np
# 文件名,分隔符,使用字段7\8,分开储存
# 编号,公式名称,日期,空格,开市,最高,最低,收市价,成交量
c,v = np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True)
print c # 收市价
print v # 成交量
[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]
[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.]
# 计算成交量加权平均价格
vwap = np.average(c, weights=v) # mean计算平均值,average(数值,权重)
print vwap
350.5895493532009
# 算数平均值
print "mean =", np.mean(c)
mean = 351.0376666666667
# 时间加权平均价格
t = np.arange(len(c))
print "twap =", np.average(c, weights=t)
twap = 352.4283218390804
# 最大值最小值
h,l = np.loadtxt('data.csv', delimiter=',', usecols=(4,5), unpack=True)
print "highest =", np.max(h)
print "lowest =", np.min(l)
print (np.max(h) + np.min(l)) / 2
# 极差
print "Spread high price", np.ptp(h)
print "Spread low price", np.ptp(l)
highest = 364.9
lowest = 333.53
349.215
Spread high price 24.859999999999957
Spread low price 26.970000000000027
# 统计分析
# 中位数
c = np.loadtxt('data.csv', delimiter=',', usecols=(6,), unpack=True)
print "median =", np.median(c)
sorted = np.msort(c)
print "sorted =", sorted
median = 352.055
sorted = [336.1 338.61 339.32 342.62 342.88 343.44 344.32 345.03 346.5 346.67
348.16 349.31 350.56 351.88 351.99 352.12 352.47 353.21 354.54 355.2
355.36 355.76 356.85 358.16 358.3 359.18 359.56 359.9 360. 363.13]
# 方差
print "variance =", np.var(c) # 方差公式np.mean((c - c.mean()) ** 2)
variance = 50.126517888888884
# 股票收益率
# 差分 diff
returns = np.diff(c) / c[:-1]
print "Standard deviation =", np.std(returns)
logreturns = np.diff(np.log(c))
print "Logreturns =", logreturns
Standard deviation = 0.012922134436826306
Logreturns = [ 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]
posretindices = np.where(returns > 0) # 收益率为正
print "Indices with positive returns", posretindices
# 波动率
annual_volatility = np.std(logreturns)/np.mean(logreturns)
anunual_volatility = annual_volatility / np.sqrt(1./252.)
print "Annual volatility", annual_volatility
print "Monthly volatility", annual_volatility * np.sqrt(1./12.)
Indices with positive returns (array([ 0, 1, 4, 5, 6, 7, 9, 10, 11, 12, 16, 17, 18, 19, 21, 22, 23,
25, 28], dtype=int64),)
Annual volatility 8.14354630702448
Monthly volatility 2.3508393262593827
# 日期分析
from datetime import datetime
# Monday 0
def datestr2num(s):
return datetime.strptime(s, "%d-%m-%Y").date().weekday()
dates, close = np.loadtxt('data.csv', delimiter=',', usecols=(1,6),
converters={1: datestr2num}, unpack=True)
print "date =", dates
date = [4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4. 1. 2. 3. 4. 0. 1. 2. 3.
4. 0. 1. 2. 3. 4.]
averages = np.zeros(5)
for i in range(5):
indices = np.where(dates == i)
prices = np.take(close, indices)
avg = np.mean(prices)
print "Day", i, "prices", prices, "Averages", avg
averages[i] = avg
Day 0 prices [[339.32 351.88 359.18 353.21 355.36]] Averages 351.7900000000001
Day 1 prices [[345.03 355.2 359.9 338.61 349.31 355.76]] Averages 350.63500000000005
Day 2 prices [[344.32 358.16 363.13 342.62 352.12 352.47]] Averages 352.1366666666666
Day 3 prices [[343.44 354.54 358.3 342.88 359.56 346.67]] Averages 350.8983333333333
Day 4 prices [[336.1 346.5 356.85 350.56 348.16 360. 351.99]] Averages 350.0228571428571
# 周汇总
dates, open, high, low, close = np.loadtxt('data.csv', delimiter=',',
usecols=(1,3,4,5,6), converters={1:datestr2num}, unpack=True)
close = close[:16]
dates = [:16]
# 获取第一个周一(去掉不完整的周)
first_Monday = np.ravel(np.where(dates))
csv文件下载地址:https://note.youdao.com/ynoteshare1/index.html?id=a0e7d5e500be2311c06299d44237a095&type=note#/