encoding=utf-8
利用numpy 进行历史股价分析
import numpy as np
import sys
读入文件
c, v = np.loadtxt(‘data.csv’, delimiter=’,’, usecols=(4, 6), unpack=True)
print ‘c=’, c
print ‘v=’, v
计算成交量加权平均价格
vwap = np.average(c, weights=v)
print ‘vwap = ‘, vwap
算术平局值函数
print “mean = “, np.mean(c)
时间加权平均价格
t = np.arange(len(c))
print ‘t=’, t
print ” 加权平均价格: twap = “, np.average(c, weights=t)
print ‘test1’
h, l = np.loadtxt(‘data.csv’, delimiter=’,’, usecols=(4, 5), unpack=True)
print ‘h = ‘, h
print ‘l = ‘, l
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 ‘max-min=’, np.max(h) - np.min(h)
print ‘Spread low.price ‘, np.ptp(l)
print ‘max-min=’, np.max(l) - np.min(l)
统计分析
c = np.loadtxt(‘data.csv’, delimiter=’,’, usecols=(6,), unpack=True)
print ‘c=’, c
数据进行排序
sorted = np.msort(c)
print ‘c:sorted ‘, sorted
中间平均值的获取,如果是奇数,获取中间的那个数字,如果是偶数,最中间的两个数值取平均值
print ‘median = ‘, np.median(c)
N = len(c)
print ‘middle = ‘, sorted[(N - 1) / 2]
print ‘average middle =’, (sorted[N / 2] + sorted[(N - 1) / 2]) / 2
计算方差
print ‘variance =’, np.var(c)
print ‘variance from definition = ‘, np.mean((c - c.mean()) ** 2)
计算股票收益率
c = np.loadtxt(‘data.csv’, delimiter=’,’, usecols=(6,), unpack=True)
print ‘c=’, c
print ‘c[:-1]’, c[:-1]
print ‘np.diff(c):’, np.diff(c)
print ‘c[:-1]:’, c[:-1]
returns = np.diff(c) / c[:-1]
print ‘returns:’, returns
简单的收益率计算
print ‘standard deviatio = ‘, np.std(returns)
对数收益率
print ‘c=’, c
returns = np.diff(np.log(c))
print returns
查看历史波动率,需要用到对数波动率
posretindices = np.where(returns > 0)
logreturns = np.diff(np.log(c))
print ‘test1’
获取年化波动率
annual_volatility = np.std(logreturns) / np.mean(logreturns)
print ‘annual_volatility’, annual_volatility
annual_volatility = annual_volatility / np.sqrt(1. / 252.)
print “annual_volatility”, annual_volatility
获取月度交易率
print “Monthly valatility “, annual_volatility * np.sqrt(1. / 12.)
print ‘test’