Numpy入门笔记第三天
__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#/
Numpy入门笔记第三天的更多相关文章
- numpy学习笔记(三)
(1)numpy的位操作 序号 操作及描述 1. bitwise_and 对数组元素执行位与操作 2. bitwise_or 对数组元素执行位或操作 3. ...
- Numpy入门笔记第一天
# 导入包 import numpy as np # 创建一维数组 a = np.arange(5) print "一维numpy数组", a print "数组的类型& ...
- bootstrap快速入门笔记(三)响应式,行,列,偏移量,排序
一,响应式列重置 .clearfix <div class="row"> <div class="col-xs-6 col-sm-3"> ...
- 面向矩阵的numpy入门笔记
我先声明我学numpy的目的:在python中使用矩阵(我需要在机器学习中使用矩阵),所以我的目的很明确,矩阵: 矩阵在numpy中叫ndarray(The N-dimensional array), ...
- NumPy 学习笔记(三)
NumPy 数组操作: 1.修改数组形状 a.numpy.reshape(arr, newshape, order='C') 在不改变数据的条件下修改形状 b.numpy.ndarray.flat 是 ...
- Numpy入门笔记第二天
# 数组的组合 import numpy as np arr1 = np.arange(5) arr2 = np.arange(3) print arr1 print arr2 [0 1 2 3 4] ...
- [Java]Java入门笔记(三):类、对象和方法
七.类.对象和方法 类和对象的关系 类定义了对象的本质: 类(class)是对象(object)的模板,而对象(object)是类的一个实例(instance). 使多个对象的指向相同: Studen ...
- electron入门笔记(三)- 引入bootstrap
源码:https://github.com/sueRimn/electron-bootstrap 当引入jQuery和bootstrap文件时,会报错,原因是:electron 的 Renderer ...
- tensorflow入门笔记(三) tf.GraphKeys
tf.GraphKeys类存放了图集用到的标准名称. 该标准库使用各种已知的名称收集和检索图中相关的值.例如,tf.Optimizer子类在没有明确指定待优化变量的情况下默认优化被收集到tf.Grap ...
随机推荐
- php版本低更换php版本-问题以解决
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable ...
- Java中InputStream和String之间的转化
https://blog.csdn.net/lmy86263/article/details/60479350 在Java中InputStream和String之间的转化十分普遍,本文主要是总结一下转 ...
- .net core 实践笔记(三)--封装底层
前言: 有了前面的工作,简单的架子基本搭建起来了,因为条件有限,只能先测试SqlServer的了,源码放出来,也希望有兴趣的伙伴可以一起改善,相信可以成为未来进阶架构师的第一步,自己有小项目的时候可以 ...
- GoogleTest初探(0)
单元测试是一种保证代码质量的手段.程序员可以通过写单元测试来保证自己写的代码的功能正确. 本人所在公司使用GoogleTest测试框架来进行单元测试.虽然现在在公司的工程代码中写单元测试已经驾轻就熟, ...
- redefinition of class解决
垃圾玩意我在这儿翻车了. 编译器:Code::Block(懒得用VS,而且又太大了,CB小,而且也就一个控制台程序) Note to myself: 写完一个class的文件定义,编译,通过之后: 1 ...
- Linux基础-2.目录文件的浏览、管理及维护
1.Linux文件系统的层次结构 1)了解Linux文件系统的树状结构: 在Linux或UNIX操作系统中,所有的文件和目录都被组织成一个以根节点开始的倒置树状结构 2)掌握目录的定义:目录相当于Wi ...
- chrome浏览器页面获取绑定返回顶部动画事件插件
在chrome浏览器下页面加载: var top = $("body").scrollTop() ; console.log(top) ...
- 关于JQuery的异步注册
在采用JQuery进行表单异步提交时,前台传入的是json数据格式,后台controller用map接收,再传回前台进行结果判断时,if-else接收结果()里面,尽量不要出现“=”,不然判断语句失效 ...
- tarjan 强连通分量
一.强连通分量定义 有向图强连通分量在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly c ...
- Hystrix使用
Hystrix是Netflix开源的一款容错系统,能帮助使用者码出具备强大的容错能力和鲁棒性的程序.如果某程序或class要使用Hystrix,只需简单继承HystrixCommand/Hystrix ...