In [2]:
# load the packages needed
import numpy as np
import sys
if "../" not in sys.path:
sys.path.append("../")
from tsap.solver import Solver
from tsap.model import AR, MA
import tsap.data_processor as dp
import tsap.inference as inf
from tsap.ts_gen import ar1_gen
data = np.loadtxt("../data/GOOG.csv", delimiter=',')
In [2]:
X = np.array([data[0:100]])
Y = dp.get_return(X)
In [3]:
lag = 5
sigma = 1.0
intercept = 0.1
phi = np.random.randn(5, 1)
AR_model = AR(lag=lag, phi=phi, sigma=sigma, intercept=intercept)
AR_model.params
Out[3]:
In [4]:
_, grads = AR_model.loss(Y)
solver = Solver(AR_model, Y,
update_rule='sgd_momentum',
optim_config={
'learning_rate': 1e-6,
},
num_epochs=1000, batch_size=1,
print_every=10)
solver.train()
In [5]:
AR_model.params
Out[5]:
In [7]:
a, b = inf.yule_walker(Y, order =5,method='unbiased')
print a
print b
In [31]:
rt = AR_model.predict(Y,20)
print rt
pred_price = dp.get_price(X[0,99],rt)
print pred_price
print data[100:120]
import tsap.trading as tr
tr.signal_generation(pred_price,5)
Out[31]:
In [37]:
import matplotlib.pyplot as plt
# This is a bit of magic to make matplotlib figures appear inline in the notebook
# rather than in a new window.
%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# Some more magic so that the notebook will reload external python modules;
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
import tsap.trading as tr
signal=tr.signal_generation(pred_price,5)
plt.plot(pred_price[0,:],'r')
plt.plot(data[100:120],'b')
plt.hold(True)
plt.show()
plt.figure()
plt.plot(data[100:120],'b')
for i in range(20):
if signal[0,i]==1:
plt.plot(i,data[100+i], 'ro')
if signal[0,i]==-1:
plt.plot(i,data[100+i], 'g^')
In [38]:
import tsap.trading as tr
profit = tr.trade(X,np.array([data[100:120]]),AR_model,20,5,100)
print profit
In [39]:
plt.plot(profit[0,:],'b')
#plt.hold(True)
#plt.show()
Out[39]:
In [ ]: