《NumPy Beginner's Guide》笔记Chapter3_Part2

时间:2023-02-09 14:38:52
# -*- coding: utf-8 -*-

__author__ = 'ZengDong'
#日期 = 15:46
import numpy as np

""" 1. Average true range: a technical indicator that measures volatility of stock prices maximun function: maximum(x1, x2[, out]) Element-wise maximum of array elements. Compare two arrays and returns a new array containing the element-wise maxima minimun function: """
import sys
h, l, c = np.loadtxt("data.csv", delimiter=",", usecols=(4, 5, 6), unpack=True)

#The ATR is based on the low and high price of N days
N = 10
h = h[-N:]
l = l[-N:]
print("len(h)", len(h), "len(l)", len(l))    #输出:('len(h)', 10, 'len(l)', 10)

#we also need to know the close price of the previous day
print("Close", c)
previousclose = c[-N - 1: -1]
print("len(previousclose", len(previousclose))
print("Previous close", previousclose)
""" 输出: ('len(previousclose', 10) ('Previous close', array([ 348.16, 353.21, 349.31, 352.12, 359.56, 360. , 355.36, 355.76, 352.47, 346.67])) """

#for each day, we calculate the following:
#h-l: The difference between high and low price
#h - previousclose: the difference between high price and previous close
#previousclose - l : the difference between the previous close and the low price
#挨个比较h-l,h-previousclose的最大值,第三个参数与truerange作用一样..
truerange = np.maximum(h - l, h - previousclose, previousclose - l)
print("True range", truerange)  #输出:('True range', array([ 6.89, 8.04, 5.95, 7.67, 2.54, 10.36, 5.15, 4.16, 4.87, 7.32]))

#create an atr array of size N and initialize its value to 0
atr = np.zeros(N)

#the first value of the array is just the average of the truerange array
atr[0] = np.mean(truerange)

#calculate the other values with the folllwing formula:
for i in range(1, N):
    atr[i] = (N - 1) * atr[i - 1] + truerange[i]
    atr[i] /= N
print("ATR", atr)



print("222222222222222222222222222222222222222222222222222222222222222222222")
""" 2. Simple moving average: commonly used to analyze time-series data ones function: convolve function:【a mathematical operation on two functions defined as the integral of the product of the two functions after one of the functions is reversed and shifted.】 Vs 相关...(旋转) """
# use the ones function to create a weight array
N = 10
weight = np.ones(N) / N
print("weight = ", weight)    #输出:('weight = ', array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]))

#now call the convolve function with the following weights:
c = np.loadtxt("data.csv", delimiter=",", usecols=(6,), unpack=True)
print("len(c) = ", len(c))    #输出 ('len(c) = ', 30)

temp = np.convolve(weight, c)
print("len(temp)", len(temp))
print("temp = ", temp)
sma = np.convolve(weight, c)[N - 1 : -N + 1]
print("C = ", c)
print("sma ", sma)
print("len(sma)", len(sma))


#plot
import matplotlib.pyplot as plt
t = np.arange(N - 1, len(c))
#plt.plot(t, c[N -1 :], lw = 1.0)
#plt.plot(t, sma, lw = 2.0)
#plt.show()



print("333333333333333333333333333333333333333333333333333333333333333333333333333")
""" 3. Exponential moving average : This method uses exponentially decreasing weights exp function: linspace function: """
# the exp function calculate the exponential of each array element
x = np.arange(5)
print("Exp", np.exp(x))   #输出:('Exp', array([ 1. , 2.71828183, 7.3890561 , 20.08553692, 54.59815003]))

# the linspace function : It returns an array of evenly spaced numbers
print("Linspace", np.linspace(-1, 0, 5))  #输出:('Linspace', array([-1. , -0.75, -0.5 , -0.25, 0. ]))


#calculate the weight
N = 5;
weight = np.exp(np.linspace(-1., 0., N))

#normalize the weights
weight /= weight.sum()
print("weight=", weight) #输出:('weight=', array([ 0.05767151, 0.06444901, 0.072023 , 0.08048708, 0.08994584, 0.10051619, 0.11232876, 0.12552954, 0.14028165, 0.15676742]))


#convolve function
c = np.loadtxt("data.csv", delimiter=",", usecols=(6,), unpack=True)
ema = np.convolve(weight, c)[N - 1 : -N + 1]
t = np.arange(N - 1, len(c))
#plt.plot(t, c[N-1:], lw = 1.0)
#plt.plot(t, ema, lw = 2.)
#plt.show()




print("4444444444444444444444444444444444444444444444444444444444444444444444444444444444")
""" 4. Bollinger bands(没看懂这东西干嘛的) fill function: The fill function sets the value of an array to a scalar value. The function should be faster than array.flat = scalar or you have to set the values of the array one by one in a loop. """
N = int(5)
weights = np.ones(N) / N
print "Weights", weights
c = np.loadtxt('data.csv', delimiter=',', usecols=(6,), unpack=True)
sma = np.convolve(weights, c)[N-1:-N+1]

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

    averages = np.zeros(N)
    averages.fill(sma[i - N - 1])
    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 = c[N - 1:]
betweent_bands = np.where((c_slice < upperBB) & (c_slice > lowerBB))
print(lowerBB[betweent_bands])
print(c[betweent_bands])
print(upperBB[betweent_bands])

betweent_bands = len(np.ravel(betweent_bands))
print("Ratio between bands", float(betweent_bands) / len(c_slice))

t = np.arange(N - 1, C)
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 = 4.0)
#plt.show()




print("5555555555555555555555555555555555555555555555555555555555555")
""" 5. Linear model linalg package: deals with linear algebra computations 矩阵求转置,求逆矩阵: np.linalg.inv() np.dot(A.transpose(),A) A * A :是元素点乘法,不是矩阵相乘... """
import sys
N = 5;
c = np.loadtxt("data.csv", delimiter=",", usecols=(6,), unpack=True)
b = c[-N:]
print(b)   #输出:[ 355.36 355.76 352.47 346.67 351.99]
b= b[::-1]
print(b)   #输出:[ 351.99 346.67 352.47 355.76 355.36]
A = np.zeros((N, N), float)


for i in range(N):
    A[i, ] = c[-N - 1 - i : -1 - i]
print("A", A)
""" b逆序. ('A', array([[ 360. , 355.36, 355.76, 352.47, 346.67], [ 359.56, 360. , 355.36, 355.76, 352.47], [ 352.12, 359.56, 360. , 355.36, 355.76], [ 349.31, 352.12, 359.56, 360. , 355.36], [ 353.21, 349.31, 352.12, 359.56, 360. ]])) b = [ 355.36 355.76 352.47 346.67 351.99] 相比A的row1,后挪一个位置 """
(x, residuals, rank, s) = np.linalg.lstsq(A, b)
print(x, residuals, rank, s)
""" (array([ 0.78111069, -1.44411737, 1.63563225, -0.89905126, 0.92009049]), array([], dtype=float64), 5, array([ 1.77736601e+03, 1.49622969e+01, 8.75528492e+00, 5.15099261e+00, 1.75199608e+00])) """
print(np.dot(b, x))   #输出 357.939161015


""" 最小二乘解用矩阵表示为: x = (A.T * A)^(-1) * A.T * b (From: CS209 Ag Wu maching learning lecture notes) 我们用以上公式验算,结果与上面np.linalg.lstsq的到结果相同... """
tmp1 = np.linalg.inv(np.dot(A.transpose(),A))
tmp2 = np.dot(tmp1, A.transpose())
tmp3 = np.dot(tmp2, b)
print(tmp3)  #输出 : [ 0.78111069 -1.44411737 1.63563225 -0.89905126 0.92009049]




print("66666666666666666666666666666666666666666666666666666666666666666")
""" 6. Trend lines """
from matplotlib.pyplot import  plot
from matplotlib.pyplot import show

def fit_line(t, y):

    A = np.vstack([t, np.ones_like(t)]).T    #A变成[[0, 1], [1, 1], [2, 1], ....[29, 1]]
    return np.linalg.lstsq(A, y)[0]     #[0] 返回最小二乘系数..此处有x1, x2两项


#first, determine the pivot points, mean of the high, low and close price
h, l, c = np.loadtxt("data.csv", delimiter=",", usecols=(4, 5, 6), unpack=True)
pivots = (h + l + c) / 3
print("Pivots", pivots)

t = np.arange(len(c))   # t = (0, 1, 2, ------, 29)

#得到最小二乘系数
sa, sb = fit_line(t, pivots - (h - l))
ra, rb = fit_line(t, pivots + (h - l))

support = sa * t + sb
resistance = ra * t + rb
condition = (c > support) & (c < resistance)
print("Condition", condition)
betweent_bands = np.where(condition)
print(support[betweent_bands])
print(c[betweent_bands])
print(resistance[betweent_bands])
betweent_bands = len(np.ravel(betweent_bands))
print("Number points between bands", betweent_bands)
print("Ratio between bands", float(betweent_bands) / len(c))

print("Tomorrows support", sa * (t[-1] + 1) + sb)
print("Tomorrows resistance", ra * (t[-1] + 1) + rb)

a1 = c[c > support]
a2 = c[c < resistance]
print("Number of points between bands 2nd approach", len(np.intersect1d(a1, a2)))

plt.plot(t, c)
plt.plot(t, support)
plt.plot(t, resistance)
#plt.show();


print("77777777777777777777777777777777777777777777777777777777777")
""" 7. Methods of ndarry (1): The NumPy ndarray class has a lot of methods that work on the array (2): You may have noticed that many of the functions that are a part of the NumPy library have a counterpart with the same name and functionality in the ndarray object Numpy Vs ndarry var, sum, std, argmax, argmin, mean clip: returns a clipped array, so that all values above a maximum value are set to the maximum and values below a minimum are set to the minimum value. compress: returns an array based on a condition """

#clip
a = np.arange(5)
print("a = ", a)   #输出: ('a = ', array([0, 1, 2, 3, 4]))
print("Clipped", a.clip(1, 2))   #输出:('Clipped', array([1, 1, 2, 2, 2]))

#compress
a = np.arange(4)
print(a)
print("Compressed", a.compress(a > 2))   #输出: ('Compressed', array([3]))






print("8888888888888888888888888888888888888888888888888888888888888888888888")
""" 8. Factorial prod function: computers the product of the elements in an array cumprod function: computer the cumulative product of an array """
#calculate the factorial of 8
b = np.arange(1, 9)
print("b = ", b)     #输出;('b = ', array([1, 2, 3, 4, 5, 6, 7, 8]))
print("Factorial", b.prod())    #输出 40320

#cumprod
print("Factorials", b.cumprod())   #输出:('Factorials', array([ 1, 2, 6, 24, 120, 720, 5040, 40320]))








""" summary : 归总 This chapter informed us about a great number of common NumPy functions. We read a file with loadtxt and wrote to a file with savetxt. We made an identity matrix with the eye function. We read a CSV file containing stock quotes with the loadtxt function. The NumPy average and mean functions allow one to calculate the weighted average and arithmetic mean of a data set. A few common statistics functions were also mentioned – first, the min and max functions that we used to determine the range of the stock prices; second, the median function that gives the median of a data set; and finally, the std and var functions that return the standard deviation and variance of a set of numbers. We calculated the simple stock returns with the diff function that returns back the differences between sequential elements. The log function computes the natural logarithms of array elements. By default, loadtxt tries to convert all data into floats. The loadtxt function has a special parameter for this purpose. The parameter is called converters and is a dictionary that links columns with the so-called converter functions. We defined a function and passed it as an argument to the apply_along_axis function. We implemented an algorithm with the requirement to find the maximum value across arrays. We learned that the ones function can create an array with ones and the convolve function calculates the convolution of a data set with the specified weights. We computed exponentially decreasing weights with the exp and linspace functions. linspace gave us an array with evenly spaced elements, and then we calculated the exponential for these numbers. We called the ndarray sum method in order to normalize the weights. We got acquainted with the NumPy fill function. This function fills an array with a scalar value, the only parameter of the fill function. After this tour through the common NumPy functions, we will continue covering convenience NumPy functions such as polyfit, sign, and piecewise in the next chapter. """