In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
eurusd = pd.read_csv("data/EURUSD_daily.csv", index_col='Date')
In [3]:
eurusd.index = pd.to_datetime(eurusd.index)
In [4]:
eurusd['2006-07-01':'2008-05-01'].plot(figsize=(16,9))
Out[4]:
In [5]:
eurusd = eurusd['2006-07-01':'2008-05-01']
In [6]:
split_date = pd.Timestamp('03-01-2008')
In [7]:
train = eurusd.loc[:split_date]
test = eurusd.loc[split_date:]
In [8]:
ax = train.plot()
test.plot(ax=ax)
plt.legend(['train', 'test'])
Out[8]:
In [9]:
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler()
train_sc = sc.fit_transform(train)
test_sc = sc.transform(test)
In [10]:
X_train = train_sc[:-1]
y_train = train_sc[1:]
X_test = test_sc[:-1]
y_test = test_sc[1:]
In [11]:
from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K
from keras.callbacks import EarlyStopping
In [12]:
K.clear_session()
model = Sequential()
model.add(Dense(12, input_dim=1, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
In [13]:
early_stop = EarlyStopping(monitor='loss', patience=1, verbose=1)
In [14]:
model.fit(X_train, y_train, epochs=200, batch_size=2, verbose=1, callbacks=[early_stop])
Out[14]:
In [15]:
y_pred = model.predict(X_test)
In [16]:
ax = plt.plot(y_pred)
plt.plot(y_test)
plt.legend(['predicted', 'real'])
Out[16]:
In [17]:
from keras.layers import LSTM
In [18]:
X_train_t = X_train[:, None]
X_test_t = X_test[:, None]
In [19]:
K.clear_session()
model = Sequential()
model.add(LSTM(6, input_shape=(1,1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
In [20]:
model.fit(X_train_t, y_train,
epochs=100, batch_size=1, verbose=1,
callbacks=[early_stop])
Out[20]:
In [21]:
y_pred = model.predict(X_test_t)
plt.plot(y_test)
plt.plot(y_pred)
Out[21]:
In [22]:
train_sc_df = pd.DataFrame(train_sc, columns=['Scaled'], index = train.index)
test_sc_df = pd.DataFrame(test_sc, columns=['Scaled'], index = test.index)
train_sc_df.head()
Out[22]:
In [23]:
for s in range(1,21):
train_sc_df['shift_{}'.format(s)] = train_sc_df['Scaled'].shift(s)
test_sc_df['shift_{}'.format(s)] = test_sc_df['Scaled'].shift(s)
In [24]:
train_sc_df.head(21)
Out[24]:
In [25]:
X_train = train_sc_df.dropna().drop('Scaled', axis=1)
y_train = train_sc_df.dropna()[['Scaled']]
X_test = test_sc_df.dropna().drop('Scaled', axis=1)
y_test = test_sc_df.dropna()[['Scaled']]
In [26]:
#converts to numpy array
X_train = X_train.values
X_test= X_test.values
y_train = y_train.values
y_test = y_test.values
In [27]:
K.clear_session()
model = Sequential()
model.add(Dense(12, input_dim=20, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
In [28]:
model.fit(X_train, y_train, epochs=200,
batch_size=1, verbose=1, callbacks=[early_stop])
Out[28]:
In [29]:
y_pred = model.predict(X_test)
plt.plot(y_pred)
plt.plot(y_test)
plt.legend(['predicted', 'real'])
Out[29]:
In [30]:
X_train_t = X_train.reshape(X_train.shape[0], 1, 20)
X_test_t = X_test.reshape(X_test.shape[0], 1, 20)
In [31]:
K.clear_session()
model = Sequential()
model.add(LSTM(6, input_shape=(1, 20)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
In [32]:
model.summary()
In [33]:
model.fit(X_train_t, y_train, epochs=100,
batch_size=1, verbose=1, callbacks=[early_stop])
Out[33]:
In [34]:
y_pred = model.predict(X_test_t)
plt.plot(y_pred)
plt.plot(y_test)
plt.legend(['predicted', 'real'])
Out[34]:
In [35]:
X_train_t = X_train.reshape(X_train.shape[0],20,1)
X_test_t = X_test.reshape(X_test.shape[0],20,1)
In [36]:
K.clear_session()
model = Sequential()
model.add(LSTM(8, input_shape=(20, 1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
In [37]:
early_stop = EarlyStopping(monitor='loss', patience=1, verbose=1)
In [38]:
model.fit(X_train_t, y_train, epochs=100, batch_size=1, verbose=1,callbacks=[early_stop])
Out[38]:
In [39]:
y_pred = model.predict(X_test_t)
plt.plot(y_pred)
plt.plot(y_test)
plt.legend(['predicted', 'real'])
Out[39]:
In [ ]: