__author__ = 'ZengDong'
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)
N = 10
h = h[-N:]
l = l[-N:]
print("len(h)", len(h), "len(l)", len(l))
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])) """
truerange = np.maximum(h - l, h - previousclose, previousclose - l)
print("True range", truerange)
atr = np.zeros(N)
atr[0] = np.mean(truerange)
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 相关...(旋转) """
N = 10
weight = np.ones(N) / N
print("weight = ", weight)
c = np.loadtxt("data.csv", delimiter=",", usecols=(6,), unpack=True)
print("len(c) = ", len(c))
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))
import matplotlib.pyplot as plt
t = np.arange(N - 1, len(c))
print("333333333333333333333333333333333333333333333333333333333333333333333333333")
""" 3. Exponential moving average : This method uses exponentially decreasing weights exp function: linspace function: """
x = np.arange(5)
print("Exp", np.exp(x))
print("Linspace", np.linspace(-1, 0, 5))
N = 5;
weight = np.exp(np.linspace(-1., 0., N))
weight /= weight.sum()
print("weight=", weight)
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))
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)
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)
b= b[::-1]
print(b)
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))
""" 最小二乘解用矩阵表示为: 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)
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
return np.linalg.lstsq(A, y)[0]
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))
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)
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 """
a = np.arange(5)
print("a = ", a)
print("Clipped", a.clip(1, 2))
a = np.arange(4)
print(a)
print("Compressed", a.compress(a > 2))
print("8888888888888888888888888888888888888888888888888888888888888888888888")
""" 8. Factorial prod function: computers the product of the elements in an array cumprod function: computer the cumulative product of an array """
b = np.arange(1, 9)
print("b = ", b)
print("Factorial", b.prod())
print("Factorials", b.cumprod())
""" 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. """