Neural networks for algorithmic trading. Correct time series forecasting + backtesting

Not that good, right?

Data preparation

data = pd.read_csv('./data/AAPL.csv')[::-1] 
close_price = data.ix[:, 'Adj Close'].tolist()
plt.plot(close_price)
plt.show()
X = [(np.array(x) — np.mean(x)) / np.std(x) for x in X]
close_price_diffs = close.price.pct_change()
Daily returns of Apple stock over time

Neural network architecture

model = Sequential() 
model.add(Dense(64, input_dim=30))
model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(2))
model.add(Activation('softmax'))
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=5, min_lr=0.000001, verbose=1) model.compile(optimizer=opt,                loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, Y_train, nb_epoch = 50,            batch_size = 128, verbose=1, validation_data=(X_test, Y_test),           shuffle=True, callbacks=[reduce_lr])
plt.figure() 
plt.plot(history.history['loss']) plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='best')

Classification

First network loss
First network accuracy
model = Sequential() 
model.add(Dense(64, input_dim=30))
model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(16))
model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(2)) model.add(Activation('softmax'))
Second network loss
Second network accuracy
model = Sequential() 
model.add(Dense(64, input_dim=30, activity_regularizer=regularizers.l2(0.01))) model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(16, activity_regularizer=regularizers.l2(0.01))) model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(2))
model.add(Activation('softmax'))
Regularized network loss
Regularized network accuracy
model = Sequential()  
model.add(Dense(64, input_dim=30, activity_regularizer=regularizers.l2(0.01))) model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dropout(0.5))
model.add(Dense(16, activity_regularizer=regularizers.l2(0.01))) model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(2))
model.add(Activation('softmax'))
Hardcore regularized network loss
Hardcore regularized network accuracy

Regression

model = Sequential()
model.add(Dense(64, input_dim=30, activity_regularizer=regularizers.l2(0.01))) model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(16, activity_regularizer=regularizers.l2(0.01))) model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(1))
model.add(Activation('linear'))
pred = model.predict(np.array(X_test)) 
original = Y_test
predicted = pred
plt.plot(original, color='black', label = 'Original data') plt.plot(predicted, color='blue', label = 'Predicted data') plt.legend(loc='best')
plt.title('Actual and predicted')
plt.show()
Returns forecast

Backtesting

if np.argmax(pred) == 0 and not self.long_market:
self.long_market = True
signal = SignalEvent(1, sym, dt, 'LONG', 1.0)
self.events.put(signal)
print pred, 'LONG'
if np.argmax(pred) == 1 and self.long_market:
self.long_market = False
signal = SignalEvent(1, sym, dt, 'EXIT', 1.0)
self.events.put(signal)
print pred, 'EXIT'
Trading results

Discussion

Conclusion

Co-founder of consulting firm Neurons Lab and advisor to AI products builders. On Medium, I write about proven strategies for achieving ML technology leadership

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store