import sys
import tushare as ts
import numpy as np
data=ts.get_h_data('601066')
print(data)
#读出兴业银行7列数据
date open high close low volume amount
data.to_csv('E:/csv statistic/day/601066.csv') 把读取的股票数据存入某个内存空间中
#开始读某列数据
c,v=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,5),unpack=True)#收盘价,交易量
vwap=np.average(c,weights=v)#成交量加权平均值,
print('vwap=',vwap)
vwap= 9.63994147552952
print(np.mean(c))
9.0965625
#计算时间加权平均值
t=np.arange(len(c))
print(np.average(c,weights=t))
9.534940476190476
#寻找最大值和最小值
h,l=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(2,4),unpack=True)
print(np.max(h))
11.98
print(np.min(l))
6.5
#计算中程数和极差
print(np.max(h)+np.min(l)/2)#计算中程数
print(np.ptp(h))#计算最大值和最小值的差值
print(np.ptp(l))#计算交易量化的差值
15.254999999999999
4.19
4.92
#统计分析c=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,),unpack=Trueprint(np.median(c))#中间数
8.850000000000001
print('sorted:',np.msort(c))#从小到大排列
sorted: [ 7.77 7.83 7.87 7.92 7.95 8. 8.06 8.15 8.17 8.19 8.2 8.2 8.23 8.27 8.28 8.32 8.32 8.35 8.36 8.36 8.36 8.36
8.48 8.5 8.53 8.53 8.54 8.55 8.61 8.61 8.64 8.82 8.88 9.09 9.15 9.37 9.38 9.38 9.39 9.45 9.45 9.48 9.52 9.67 9.7 9.73 9.8 9.83
9.86 9.91 9.93 10.06 10.23 10.27 10.31 10.33 10.33 10.41 10.42 10.56 10.8 11.09 11.46 11.85]
print('方差:',np.var(c))
方差: 0.9610756835937502
print('手动计算方差:',np.mean((c-c.mean())**2))
手动计算方差: 0.9610756835937502
#股票收益率的计算
c=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,),unpack=True)
returns=np.diff(c)/c[:-1] #diff计算差分,总体是求股票收益率
print(np.std(returns)) # 求标准差
log_returns=np.diff(np.log(c)) #股票对数收益率
print(log_returns)
posretindices=np.where(returns>0)#股票收益率大于0的那几天
print(posretindices)
ann_vol=np.std(log_returns)/np.mean(log_returns)#日收益率
print(ann_vol)
ann_vol=ann_vol/np.sqrt(1./252.)#年收益率
print(ann_vol)
print(ann_vol*np.sqrt(1./12.))#月收益率
#定义一个 把日期转换为数字的函数
from datetime import datetime
def date2num(s):
return datetime.strptime(s,'%Y-%m-%d').date().weekday()
#读取收盘价
close=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(3,),unpack=True)
#读取日期
dates=np.loadtxt('E:/csv statistic/day/601066.csv', skiprows=1,delimiter=',',usecols=(0,),unpack=True,dtype=bytes).astype(str)
#读取的str数据转换为 数字
for i in range (dates.size):
dates[i]=date2num(dates[i])
dates=dates.astype(np.int8)
print(dates.dtype)
print(dates)
averages = np.zeros(5) # 定义一个空数组
# 依次获取不同天的索引,然后根据索引求得close(收盘价)数组中所有的对应值,计算平均值,把平均值加入到averages数组中
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) # 计算周每日的收盘价平均值的最大值
np.argmax(averages) # 获取周每日的收盘价平均值的最大值是哪一天
bottom = np.min(averages) # 计算周每日的收盘价平均值的最小值
np.argmin(averages) # 获取周每日的收盘价平均值的最小值是哪一天