예제 #1
0
def main(ticker1, ticker2):
	df = pd.read_csv("./Data/close.csv", dtype={"date": str})

	df2 = np.log(df.loc[:, [ticker1, ticker2]]).diff().dropna()
	x = df2[ticker1].values
	y = df2[ticker2].values
	A = np.vstack((np.ones_like(x), x)).T

	b = np.linalg.inv(A.T.dot(A)).dot(A.T).dot(y)
	resid = y - A.dot(b)

	resid_se = pd.Series(resid)
	std2_se = resid_se.rolling(
	    window=100,
	).apply(lambda x: sqrt(sum(np.diff(x)**2) / (len(x) - 1)))
	mean_se = resid_se.rolling(
	    window=100,
	).mean()

	'''
	s_score = (pd.Series(resid_se) - mean_se) / std2_se
	'''
	ar = ARX(resid_se, volatility=EGARCH(2, 0, 2))
	ar.distribution = SkewStudent()
	res = ar.fit()
	s_score = pd.Series(resid)

	arg_lst = [
		(s_score, resid_se, i / 100.0, j / 100.0, k / 100.0, l / 100.0, m / 100.0, n / 100.0) for i in xrange(15, 35, 5) for j in xrange(i + 1, 49, 5) for k in xrange(j + 1, 50, 5) for l in xrange(85, 65, -5) for m in xrange(l - 1, 51, -5) for n in xrange(m - 1, 50, -5)
		]

	pool = mp.Pool(6)
	result = pool.map(back_test_sharp, arg_lst)
	pool.close()
	pool.join()

	with open("./pkl/EG_result_lst_{}_{}_sharp".format(ticker1, ticker2), "wb") as fp:
		cp.dump(result, fp)
	
	x_mean = x.mean()
	y_mean = y.mean()
	pearson = (x - x_mean).dot(y - y_mean) / sqrt(sum((x - x_mean)**2)) / sqrt(sum((y - y_mean)**2))

	result.sort(key=lambda x: x[0], reverse=True)
	best = result[0]
	res = back_test((s_score, resid_se, best[1], best[2], best[3], best[4], best[5], best[6]))
	fig = plt.figure(figsize=(20, 10))
	plt.plot(res[0])
	plt.savefig("./Pics/net_value/EG_{}_{}.png".format(ticker1, ticker2))
	del fig
	return pd.Series(res[0]).to_csv("./xlsx/EG_{}_{}_{}_{}_{}_{}_{}_{}_{}.csv".format(ticker1, ticker2, pearson, best[1], best[2], best[3], best[4], best[5], best[6]))
from arch.univariate import ARX
ar = ARX(Y, lags=30)
print(ar.fit().summary())

# In[270]:

from arch.univariate import ARCH, GARCH
ar.volatility = GARCH(p=3, o=0, q=3)
res = ar.fit(update_freq=0, disp='off')
p(res.summary())

# In[265]:

from arch.univariate import StudentsT
ar.distribution = StudentsT()
res = ar.fit(update_freq=0, disp='off')
p(res.summary())

# In[266]:

arf = ar.forecast(horizon=forecast_steps,
                  start=Y.index[-1],
                  params=res.params,
                  method='simulation')

# In[267]:

plt.plot(idx, arf.simulations.values[-1].T, color='blue', alpha=0.1)
plt.show()