__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. Altium 技巧 记录

    1.隐藏全部网络,即隐藏全部的飞线,便于布局,在 PCB 编辑器下,选择工具→连接→显示或隐藏全部网络即可 2.元器件非常多时,模块化布局的小技巧,参考:Altium Designer(DXP)小技巧 ...

  2. Java中的类与对象

    一.类与对象的概念 1.类:类是一组相同属性.方法的对象的集合:对象是类的具体化. 2.对象具有类所有的特征,类拥有的,对象就拥有. 3.类与对象他们的关系是相对的. 类有什么特点 1) 类是对象的类 ...

  3. Oracle高级函数篇之递归查询start with connect by prior简单用法

    路飞:" 把原来CSDN的博客转移到博客园咯!" 前段时间,自己负责的任务中刚好涉及到了组织关系的业务需求,自己用了oracle递归查询.下面简单来举个例子.在工作中我们经常会遇到 ...

  4. functional filter()

    #include "pch.h" #include <iostream> #include <deque> #include <string> ...

  5. nginx+uwsgi+flask+supervisor 项目部署

    环境 - Linux: Ubuntu 16.04 - uWSGI 2.0.18 - Flask 1.0.2 - supervisor 3.2.0 - nginx/1.8.1 首先区分几个概念 WSGI ...

  6. 【Linux】文件、目录权限及归属

    访问权限: 可读(read):允许查看文件内容.显示目录列表 可写(write):允许修改文件内容,允许在目录中新建.移动.删除文件或子目录 可执行(execute):允许运行程序.切换目录 归属: ...

  7. MVC和MVT的区别

    首先,MVC和MVT是框架式不是设计模式. 框架与设计模式虽然相似,但却有着根本的不同.设计模式是对在某种环境中反复出现的问题以及解决该问题的方案的描述,它比框架更抽象:框架可以用代码表示,也能直接执 ...

  8. javascript 之 对象

    可以通过 Object 构造函数或对象字面量的方式创建对象,但是这些方式的缺点是使用同一个接口创建多个对象,会产生大量重复的代码. 1.工厂模式 function createPerson(name, ...

  9. 用户从地址栏输入url,按下enter键后,直到页面加载完成的这个过程都发生了什么?

    流程大概描述一下: 用户将url输入后,服务器接受到请求,然后将这个请求进行处理,然后将处理后的结果返回给浏览器,浏览器将该结果以页面的形式呈现给用户. 详细描述: 1:用户将url(例如www.ba ...

  10. kubernetes常用基础命令

    创建资源对象 创建名为nginx-deploy的控制器资源对象 [root@master ~]# kubectl run nginx-deploy --image=nginx:1.12 --repli ...