__author__ = 'ZengDong'
import numpy as np
import matplotlib.pyplot as plt
""" 1. Correlation (A high correlation implies a relationship of some sort) function: cov diagonal trace std corrcoef mean """
bhp = np.loadtxt("BHP.csv", delimiter=",", usecols=(6,), unpack=True)
bhp_returans = np.diff(bhp) / bhp[: -1]
print("len(bhp_returans) = ", len(bhp_returans))
vale = np.loadtxt("VALE.csv", delimiter=",", usecols=(6,), unpack=True)
vale_returns = np.diff(vale) / vale[: -1]
covariance = np.cov(bhp_returans, vale_returns)
print("Covariance", covariance)
""" 输出: ('Covariance', array([[ 0.00028179, 0.00019766], [ 0.00019766, 0.00030123]])) """
print("Covariance diagonal", covariance.diagonal())
print("Covariance trace", covariance.trace())
print("Correlation coefficient", np.corrcoef(bhp_returans, vale_returns))
""" 输出; ('Correlation coefficient', array([[ 1. , 0.67841747], [ 0.67841747, 1. ]])) """
difference = bhp - vale
avg = np.mean(difference)
dev = np.std(difference)
print("Out of sync", np.abs(difference[-1] - avg) > 2 * dev)
t = np.arange(len(bhp_returans))
plt.plot(t, bhp_returans, lw = 1.0)
plt.plot(t, vale_returns, lw = 2.0)
print("2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
""" 2. Polynomials function: polyfit: fit a set of data points to a polynomial even if the underlying function is not continuous polyval: computer the values of a polynomial roots: returns the roots of the polynomial polyder: gives back the derivative of a polynomial """
import sys
bhp = np.loadtxt("BHP.csv", delimiter=",", usecols=(6,), unpack=True)
vale = np.loadtxt("VALE.csv", delimiter=",", usecols=(6,), unpack=True)
N_order = 100
t = np.arange(len(bhp))
poly = np.polyfit(t, bhp - vale, N_order)
print("Polynomial fit", poly)
print("Next value", np.polyval(poly, t[-1] + 1))
print("Roots", np.roots(poly))
der = np.polyder(poly)
print("Derivative", der)
print("Extremas", np.roots(der))
vals = np.polyval(poly, t)
print("np.argmax(vals)", np.argmax(vals))
print("np.argmin(vals)", np.argmin(vals))
plt.plot(t, bhp - vale)
plt.plot(t, vals)
print("3333333333333333333333333333333333333333333333333333333333333333333333333333333333333")
""" 3. On-balance volume function: diff: computes the difference between two sequential array elements and returns an array containing these differences: sign: The NumPy sign function returns the signs for each element in an array. -1 is returned for a negative number, 1 for a positive number, and 0, otherwise. piecewise: as its name suggests, evaluates a function piece-by-piece. """
c, v = np.loadtxt("BHP.csv", delimiter=",", usecols=(6, 7), unpack=True)
change = np.diff(c)
print("Change", change, "len(Change)", len(change))
signs = np.sign(change)
print("Signs", signs)
pieces = np.piecewise(change, [change < 0, change > 0], [-1, 1])
print("Pieces", pieces)
print("Arrays equal?", np.array_equal(signs, pieces))
print("On balance volume", v[1:] * signs)
print("44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
""" 4. Simulation function: vectorize: The vectorize function is the NumPy equivalent of the Python map function another way to avoid using loops """
o, h, l, c = np.loadtxt("BHP.csv", delimiter=",", usecols=(3, 4, 5, 6), unpack=True)
N = 1;
def calc_profit(open, high, low, close):
buy = open * float(N)
if low < buy < high:
return (close - buy) / buy
else:
return 0
func = np.vectorize(calc_profit)
profits = func(o, h, l, c)
print("profits", profits)
real_trades = profits[profits != 0]
print("Number of trades", len(real_trades), round(100.0 * len(real_trades) / len(c), 2), "%")
print("Average profit/loss %", round(np.mean(real_trades) * 100, 2))
winning_trades = profits[profits > 0]
print("Number of winning trades", len(winning_trades), round(100.0 * len(winning_trades) / len(c), 2), "%")
print("Average profit %", round(np.mean(winning_trades) * 100, 2))
losing_trads = profits[profits < 0]
print("Number of losing trades", len(losing_trads), round(100.0 * len(losing_trads) / len(c), 2), "%")
print("Average loss %", round(np.mean(losing_trads) * 100, 2))
print("5555555555555555555555555555555555555555555555555555555555555555555555555555555")
""" 5. Smoothing function: hanning: a windowing function formed by a weighted cosine """
N = 8
weights = np.hanning(N)
print("Weights", weights)
print("sum(weights)", np.sum(weights))
""" t = np.arange(len(weights)) plt.clf() plt.plot(t, weights) plt.show() """
bhp = np.loadtxt("BHP.csv", delimiter=",", usecols=(6,), unpack=True)
bhp_returans = np.diff(bhp) / bhp[: -1]
smooth_bhp = np.convolve(weights / weights.sum(), bhp_returans)[N - 1 : -N + 1]
vale = np.loadtxt("VALE.csv", delimiter=",", usecols=(6,), unpack=True)
vale_returns = np.diff(vale) /vale[: -1]
smooth_vale = np.convolve(weights/weights.sum(), vale_returns)[N - 1 : -N + 1]
K = 30;
t = np.arange(N - 1, len(bhp_returans))
poly_bhp = np.polyfit(t, smooth_bhp, K)
poly_vale = np.polyfit(t, smooth_vale, K)
poly_sub = np.polysub(poly_bhp, poly_vale)
xpoints = np.roots(poly_sub)
print("Intersection points", xpoints)
reals = np.isreal(xpoints)
print("Real number?", reals);
xpoints = np.select([reals], [xpoints])
xpoints = xpoints.real
print("Real intersection points", xpoints)
print("Sans 0s", np.trim_zeros(xpoints))
plt.clf()
plt.plot(t, bhp_returans[N-1:], lw = 1.0)
plt.plot(t, smooth_bhp, lw = 2.0)
plt.show()