python学习---numpy例子股票的简单操作

###利用numpy进行历史股价分析
import sys
import numpy as np
#读入文件
c,v = np.loadtxt('data.csv',delimiter= ',',usecols= (6,7),unpack=True)
#print(c)
#print(v)
#计算成交量加权平均价格
vwap = np.average(c,weights = v)
print("VWAP =",vwap)
#算数平均值函数
print("mean = ",c.mean())

#时间加权平均价格
t = np.arange(len(c))
print("twap = ",np.average(c,weights= t))

#寻找最大值和最小值
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))

#统计分析
c = np.loadtxt('data.csv',delimiter= ',',usecols= (6,),unpack= True)
print("median = ",np.median(c))
sorted = np.msort(c)
print("sorted =",sorted)

N = len(c)
# 注意list的下标不能是小数,所以要整型化;
print("middle =",sorted[int((N-1)/2)])
print("average meddle =",(sorted[int(N/2)]+sorted[int((N-1)/2)])/2)

# 总体方差与样本方差的区别,
print("variance =",np.var(c))  #方差 numpy自带的公式
print("variance from definition =",np.mean((c-c.mean())**2)) #根据定义来求得方差

# 股票收益率
c = np.loadtxt('data.csv',delimiter=',',usecols=(6,),unpack=True)
# 注意np.sort(c)与c.sort()的区别,一个返回新的数组,一个等于内部自排
# sorted1 =np.sort(-c)  这里是降序排列,注意!!!
# print(sorted1)

# 差分函数,数组相邻元素之间的差值
returns = np.diff( c )/(c[ : -1])
print(returns)
print("Standard deviation =",np.std(returns))

logreturns = np.diff(np.log(c))

# 返回一个数组,里面是收益率大于0的数据
posretindices = np.where(returns >0)
print(posretindices)
# 年度波动率
annua_volatility = np.std(logreturns)/np.mean(logreturns)
annua_volatility =annua_volatility / np.sqrt(1./252.)
print("Annual volatility",annua_volatility)

#月度波动率
print("Monthly volatility",annua_volatility * np.sqrt(1./12.))

# 日期分析
from datetime import datetime
'''
Monday 0
Tuesday 1
Wednesday 2
Thursday 3
Friday 4
Saturday 5
Sunday 6
'''

#编译器在打开data.csv文件时,将表格里的第2列数组值提取出来返回给dates,
# 第二列值是日期格式字符串,但因为我们是以二进制编码的格式打开第二列值是,
# 返回的值字节字符串bytes,所以需要把它便会string,则对字符串解码用函数
# decode('asii'),变成string格式。
def datestr2num(s):
    return datetime.strptime(s.decode('ascii'),"%d-%m-%Y").date().weekday()

dates,close=np.loadtxt("data.csv",delimiter= ',',usecols=(1,6),
                       converters={1: datestr2num},unpack= True)
print("Dates =",dates)

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,"Average",avg)
    averages[i] = avg

top = np.max(averages)
print("Heigest average",top)
print("Top day of the week",np.argmax(averages))

# 简单移动平均线
from matplotlib.pyplot import  plot
from matplotlib.pyplot import  show

N = 5

weights = np.ones(N)/N
print("Weight",weights)

c = np.loadtxt("data.csv",delimiter=',',usecols=(6,),unpack=True)
sma = np.convolve(weights,c)[N-1:-N+1] # N-1到-N+1的用权值为weight计算
t = np.arange(N-1,len(c))
plot(t,c[N-1:],lw=1.0)
plot(t,sma,lw=2.0)
#show()


#指数移动平均线
x = np.arange(5)
print("Exp",np.exp(x))
print("Linspace",np.linspace(-1,0,5))

N = 5

weight = np.exp(np.linspace(-1.,0.,N))
weight/=weight.sum()
print("Weights",weights)

ema = np.convolve(weight,c)[N-1:-N+1]
t = np.arange(N-1,len(c))
plot(t,c[N-1:],lw=1.0)
plot(t,ema,lw=2.0)
show()

# 布林带

你可能感兴趣的:(python学习记录)