__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入门笔记第三天的更多相关文章

  1. numpy学习笔记(三)

    (1)numpy的位操作 序号         操作及描述 1.      bitwise_and 对数组元素执行位与操作 2.      bitwise_or 对数组元素执行位或操作 3.      ...

  2. Numpy入门笔记第一天

    # 导入包 import numpy as np # 创建一维数组 a = np.arange(5) print "一维numpy数组", a print "数组的类型& ...

  3. bootstrap快速入门笔记(三)响应式,行,列,偏移量,排序

    一,响应式列重置 .clearfix <div class="row"> <div class="col-xs-6 col-sm-3"> ...

  4. 面向矩阵的numpy入门笔记

    我先声明我学numpy的目的:在python中使用矩阵(我需要在机器学习中使用矩阵),所以我的目的很明确,矩阵: 矩阵在numpy中叫ndarray(The N-dimensional array), ...

  5. NumPy 学习笔记(三)

    NumPy 数组操作: 1.修改数组形状 a.numpy.reshape(arr, newshape, order='C') 在不改变数据的条件下修改形状 b.numpy.ndarray.flat 是 ...

  6. Numpy入门笔记第二天

    # 数组的组合 import numpy as np arr1 = np.arange(5) arr2 = np.arange(3) print arr1 print arr2 [0 1 2 3 4] ...

  7. [Java]Java入门笔记(三):类、对象和方法

    七.类.对象和方法 类和对象的关系 类定义了对象的本质: 类(class)是对象(object)的模板,而对象(object)是类的一个实例(instance). 使多个对象的指向相同: Studen ...

  8. electron入门笔记(三)- 引入bootstrap

    源码:https://github.com/sueRimn/electron-bootstrap 当引入jQuery和bootstrap文件时,会报错,原因是:electron 的 Renderer ...

  9. tensorflow入门笔记(三) tf.GraphKeys

    tf.GraphKeys类存放了图集用到的标准名称. 该标准库使用各种已知的名称收集和检索图中相关的值.例如,tf.Optimizer子类在没有明确指定待优化变量的情况下默认优化被收集到tf.Grap ...

随机推荐

  1. Linux TCP server 只能接受一个 TCP 连接

    #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <str ...

  2. 微服务之配置中心ConfigKeeper

    在微服务架构中,配置中心是必不可少的基础服务.ConfigKeeper已开源,本文将深度分析配置中心的核心内容,错过「Spring Cloud中国社区北京沙龙-2018.10.28 」的同学将从本篇文 ...

  3. [iOS]CIFilter滤镜

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  4. let与var的区别,为什么什么要用let?

    1.var是全局声明,let是块级作用的,只适用于当前代码块 var a = 1: if(true){ let a; a=22: console.log(a);'//22 } if(){}内就是let ...

  5. vue组件中,iview的modal组件爬坑--modal的显示与否应该是使用v-show

    这是我第一次写博客,主要是记录下自己解决问题的过程和知识的总结,如有不对的地方欢迎指出来! 需求:点击btn,弹出modal显示图表(以折现图为例) 这应该是很基本的需求也是很容易实现的,代码和效果如 ...

  6. ionic ios 打包 真机测试常见问题

    1.ionic 项目在windows下正常打包安卓包时  迁移到mac下打包ios时  不需要复制平台目录platforms即可  不用再mac下去安装各种插件信息 2.ionic 下不能访问api信 ...

  7. Iframe 定义内联的子窗口(框架)

    1.Iframe 定义内联的子窗口(框架),用于在网页内显示网页 语法: <iframe src="URL"></iframe>URL 指向隔离页面的位置, ...

  8. Redis笔记 -- 链表和链表节点的API函数(三)

    链表和链表节点API 函数 作用 时间复杂度 listSetDupMethod 将给定的函数设置为链表的节点值复制函数 复制函数可以通过链表的dup属性直接获得,O(1) listGetDupMeth ...

  9. DELPHI一个对付内存汇漏的办法和技巧

    DELPHI是要手动释放内存的,如果客户端程序有泄漏,可能不是很大问题, 但是如果你是用DELPHI做服务端程序,有泄漏的话,时间一长会占用很多内存,直到服务端程序要关闭重启.所以内存泄漏还是有害的. ...

  10. 大数据:Map终结和Spill文件合并

    当Mapper没有数据输入,mapper.run中的while循环会调用context.nextKeyValue就返回false,于是便返回到runNewMapper中,在这里程序会关闭输入通道和输出 ...