# 大数据 - Python数据分析--Numpy常用函数介绍(3)

import numpy as np
from datetime import datetime

def datestr2num(s): #定义一个函数
return datetime.strptime(s.decode('ascii'),"%Y-%m-%d").date().weekday()
#decode('ascii') 将字符串s转化为ascii码

#读取csv文件 ，将日期、开盘价、最低价、最高价、收盘价、成交量等全部读取
dates, opens, high, low, close,vol=np.loadtxt('data.csv',delimiter=',', usecols=(1,2,3,4,5,6),converters={1:datestr2num},unpack=True) #按顺序对应好data.csv与usecols=(1,2,3,4,5,6)中的列
#获取20个交易日的数据 closes = close[0:20] #实际存取下标是0-19 dateslist = dates[0:20] print(closes) #打印出closes数列 print(dateslist)

[37.5  37.58 37.23 36.9  38.45 37.69 37.42 37.2  36.98 36.8  36.79 37.59 37.6  37.7  37.24 37.35 37.9  38.06 37.87 38.99]
[0. 1. 2. 3. 4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4.]

first_monday = np.ravel(np.where(dateslist == 0))[0]
print ("The first Monday index is", first_monday)
#返回最后一个周五的位置
last_friday = np.ravel(np.where(dateslist == 4))[-1]
print ("The last Friday index is", last_friday)
print('\n')

The first Monday index is 0
The last Friday index is 19

weeks_indices = np.arange(first_monday, last_friday+1)
print ("Weeks indices initial", weeks_indices)

weeks_indices = np.split(weeks_indices,4)
print("Weeks indices after split", weeks_indices)
Weeks indices initial [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
Weeks indices after split [array([0, 1, 2, 3, 4], dtype=int64), array([5, 6, 7, 8, 9], dtype=int64), array([10, 11, 12, 13, 14], dtype=int64), array([15, 16, 17, 18, 19], dtype=int64)]

NumPy中，数组的维度也被称作轴。apply_along_axis 函数会调用另外一个由我们给出的函数，作用于每一个数组元素上，数组中有4个元素，分别对应于示例数据中的4个星期，元素中的索引值对应于示例数据中的1天。在调用apply_along_axis 时提供我们自定义的函数名summarize，并指定要作用的轴或维度的编号（如取1）、目标数组以及可变数量的summarize函数的参数，同时进行保存。

# 定义一个函数，该函数将为每一周的数据返回一个元组，包含这一周的开盘价、最高价、最低价和收盘价，类似于每天的盘后数据def summarize(a, o, h, l, c):     monday_open = o[a[0]] #周一开盘价
week_high = np.max( np.take(h, a) ) # 某周最高价
week_low = np.min( np.take(l, a) )  # 某周最低价
friday_close = c[a[-1]]      #某周的收盘价

return("招商银行", monday_open, week_high, week_low, friday_close) #返回某周开盘、最高、低价、收盘价

weeksummary = np.apply_along_axis(summarize, 1, weeks_indices,opens, high, low, close)
print ("Week summary", weeksummary)

np.savetxt("weeksummary.csv", weeksummary, delimiter=",", fmt="%s")

1、波动幅度均值（ATR）ATR（Average True Range，真实波动幅度均值）是一个用来衡量股价波动性的技术指标。ATR是基于N个交易日的最高价和最低价进行计算的，通常取最近20个交易日。

(1) 前一个交易日的收盘价。 previousclose = c[-N -1: -1]对于每一个交易日，计算以下各项。h – l 当日最高价和最低价之差。 　　h – previousclose 当日最高价和前一个交易日收盘价之差。    　　   previousclose – l 前一个交易日收盘价和当日最低价之差。

(2) 用NumPy中的 maximum 函数返回上述三个中的最大值。    truerange = np.maximum(h - l, h - previousclose, previousclose - l)

(3) 创建一个长度为 N 的数组 atr ，并初始化数组元素为0。atr = np.zeros(N)

(4) 这个数组的首个元素就是 truerange 数组元素的平均值。atr[0] = np.mean(truerange)5）计算出每个交易日的波动幅度：

for i in range(1, N):atr[i] = (N - 1) * atr[i - 1] + truerange[i]atr[i] /= N

import numpy as np
from datetime import datetime

def datestr2num(s): #定义一个函数
return datetime.strptime(s.decode('ascii'),"%Y-%m-%d").date().weekday()

dates, opens, high, low, close,vol=np.loadtxt('data.csv',delimiter=',', usecols=(1,2,3,4,5,6),
converters={1:datestr2num},unpack=True)
closes = close[0:20]  #实际存取下标是0-19
dateslist = dates[0:20]
first_monday = np.ravel(np.where(dateslist == 0))[0]
last_friday = np.ravel(np.where(dateslist == 4))[-1]#从最后一个位置开始
weeks_indices = np.split(np.arange(first_monday, last_friday+1),4)

#波动幅度均值（ATR）
N = 20
h = high[-N:]
l = low[-N:]

print ("len(high)", len(h), "len(low)", len(l))
#print ("Close", close)
#前一日的收盘价数列
previousclose = close[-N-1: -1]
print ("len(previousclose)", len(previousclose))
print ("Previous close", previousclose)

#用NumPy中的maximum函数，在 最高-最低，最高-昨日收盘，昨日收盘 三个数据选择最大
truerange = np.maximum(h-l,h-previousclose,previousclose)
print ("True range", truerange)

atr = np.zeros(N)  # 创建一个长度为 N 的数组 atr ，并初始化数组元素为0
atr[0] = np.mean(truerange) # 数组的首个元素设定为truerange数组元素的平均值
for i in range(1, N):  #循环，计算每个交易日的波幅，并保存
atr[i] = (N - 1) * atr[i - 1] + truerange[i]
atr[i] /= N
print ("ATR", atr)

len(high) 20 len(low) 20
len(previousclose) 20
Previous close [42.1  41.1  41.28 42.5  38.83 38.41 38.04 39.62 39.93 39.26 37.91 36.47 36.98 37.21 36.61 37.15 36.89 38.6  38.5  38.03]
True range [1.08 1.5  2.32 2.23 1.56 1.02 2.13 1.49 1.16 0.85 1.67 1.9  0.96 0.63 0.99 0.69 1.74 1.18 0.73 2.15]
ATR [1.399      1.40405    1.4498475  1.48885513 1.49241237 1.46879175 1.50185216 1.50125955 1.48419658 1.45248675 1.46336241 1.48519429 1.45893458 1.41748785 1.39611345 1.36080778 1.37976739 1.36977902 1.33779007 1.37840057]

2、移动均线：股市中最常见的是指标，移动平均线只需要少量的循环和均值函数即可计算得出。简单移动平均线是计算与等权重的指示函数的卷积。

(1) 使用 ones 函数创建一个长度为 N 的元素均初始化为1的数组，然后对整个数组除以 N ，即可得到权重，比如 5日均线，即N=5，则平均每天的权重都为0.2.

N = 5
weights = np.ones(N) / N
print ("Weights", weights)

（2）使用 convolve 函数调用上述的权重值

sma = np.convolve(weights, c)[N-1:-N+1]

N = 5
weights = np.ones(N) / N
print ("Weights", weights)

sma = np.convolve(weights, close)[N-1:-N+1]
print(sma)
print(len(sma))

import matplotlib.pyplot as plt
#省略上述代码

plt.plot(sma, linewidth=5)

3、指数移动平均线

1）先了解numpy中的exp 和 linspace 函数

x = np.arange(5)
y = np.arange(10)
print ("Exp", np.exp(x)) # exp 函数可以计算出每个数组元素的指数
print ("Exp", np.exp(y))

ExpX [ 1.          2.71828183  7.3890561  20.08553692 54.59815003]
ExpY [1.00000000e+00 2.71828183e+00 7.38905610e+00 2.00855369e+01 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03 2.98095799e+03 8.10308393e+03]

print( "Linspace", np.linspace(-1, 0, 5))

Linspace [-1.   -0.75 -0.5  -0.25  0.  ]

linspace中有三个参数，其中前2个是一个范围：一个起始值和一个终止值参数，后一个是生成的数组元素的个数。

2）计算指数移动平均线

import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt

def datestr2num(s): #定义一个函数
return datetime.strptime(s.decode('ascii'),"%Y-%m-%d").date().weekday()

dates, opens, high, low, close,vol=np.loadtxt('data.csv',delimiter=',', usecols=(1,2,3,4,5,6),
converters={1:datestr2num},unpack=True)

N = 5
"""
weights = np.ones(N) / N
print ("Weights", weights)
sma = np.convolve(weights, close)[N-1:-N+1]
print(sma)
print(len(sma))
plt.plot(sma, linewidth=5)
"""
weights = np.exp(np.linspace(-1., 0., N)) #
weights /= weights.sum()  #对权重值做归一化处理
print( "Weights", weights)
ema = np.convolve(weights, close)[N-1:-N+1]
#print(ema)

t = np.arange(N - 1, len(close))
plt.plot (t, close[N-1:], lw=1.0)  #收盘价绘制曲线图
plt.plot (t, ema, lw=2.0)   #按权重计算均线曲线图
plt.show()

4、绘制布林带

import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt

def datestr2num(s): #定义一个函数
return datetime.strptime(s.decode('ascii'),"%Y-%m-%d").date().weekday()

dates, opens, high, low, close,vol=np.loadtxt('data.csv',delimiter=',', usecols=(1,2,3,4,5,6),
converters={1:datestr2num},unpack=True)

N = 5
weights = np.ones(N) / N
sma = np.convolve(weights, close)[N-1:-N+1]
deviation = []

clegth = len(close)
for i in range(N - 1, clegth ):
if i + N < clegth :
dev = close [i: i + N]
else:
dev = close [-N:]

averages = np.zeros(N)
averages.fill(sma[i - N - 1]) #fill()函数可以用一个指定的标量值填充数组，而这个标量值也是 fill 函数唯一的参数。
dev = dev - averages
dev = dev ** 2
dev = np.sqrt(np.mean(dev))
deviation.append(dev)

deviation = 2 * np.array(deviation)
upperBB = sma + deviation
lowerBB = sma - deviation

c_slice = close[N-1:]
between_bands = np.where((c_slice < upperBB) & (c_slice > lowerBB))
between_bands = len(np.ravel(between_bands))
print( "Ratio between bands", float(between_bands)/len(c_slice))

t = np.arange(N-1,clegth)
plt.plot(t, c_slice, lw=1.0) #收盘价
plt.plot(t, sma, lw=2.0)     #移动均线
plt.plot(t, upperBB, lw=3.0) #上轨道
plt.plot(t, lowerBB, lw=1.0) #下轨道
plt.show()