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绘图,就得在php.ini文件中设置一下 找到这个位置 ;extension=php_gd2.dll,然后把前面的分号去掉,重启下apache就可以了 几乎每行代码我都写了注释,方便看懂 ...
- Ubuntu install 错误 E:Unable to locate package
今天在 Ubuntu 上执行 sudo apt install sl 命令,结果报错:E:Unable to locate package sl 上网查询了一下,先更新一下 apt-get,执行:su ...
- MySQL——安装
1. 下载源: http://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/mysql80-community-release-el7-2.no ...
- iOS 12.0-12.1.2 越狱教程
unc0ver V3.0.0~b29 越狱工具已经开始公测,支持搭载 A8X-A11 处理器的 iOS 12.0-12.1.2 设备完整越狱,Cydia 商店和 Substrate 插件可正常安装并运 ...
- javascript编写的一个完整全方位轮播图效果
1 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- C++中一个0xC0000005访问冲突问题
在冯.诺依曼结构CPU(如i386,ARM A8,A9)的保护模式或者哈佛结构(如8051, ARM M0,.. M3)的CPU下,C++编译器将放置常量的内存设置为只读模式或者放入只读内存中,如果出 ...
- urllib库使用方法 4 create headers
import urllib.requestimport urllib.parse url = "https://www.baidu.com/"#普通请求方法response = u ...
- Go 学习之路:Println 与 Printf 的区别
Println 和Printf 都是fmt包中公共方法:在需要打印信息时常用的函数,那么二函数有什么区别呢? 附上代码 package main import ("time"&qu ...
- 关于Win10 环境下Quartus II 15.0器件列表无法下拉的解决方法
不知大家在Windows 10 64位系统环境下使用Quartus II 15.0在新建工程时遇到过这种问题没,在新建工程的过程是选择器件的列表无法下拉,只能看到一个器件型号,如图1所示. 图1 开始 ...
- 20155204 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
实验一 Java开发环境的熟悉 一.实验内容及步骤 1.使用JDK编译.运行简单的java程序 步骤一:在linux界面下运行终端 步骤二:在终端中打开待编译文件的文件夹 步骤三:使用 javac 文 ...