Deep Learning

Pragmatic Deep Learning Model for Forex Forecasting

Using LSTM and TensorFlow on the GBPUSD Time Series for multi-step prediction

Adam Tibi
Adam Tibi
Oct 7 · 23 min read
Multi-Step Prediction for GBPUSD
Multi-Step Prediction for GBPUSD

Source Code and Following Along

Explaining the environment setup and the steps to run the model

Table of Contents

Forex Trading Primer

The ML Model: Concept and Plan

1 — Data Sourcing

2 — Data Preparation

3 — Model Training

4 — Predictions

Continuing and Expanding the Research

Disclaimer

Conclusion

Part Two: Using the Model from a Trading Platform

More Readings

About Me

References

Forex Trading Primer

Image for post
Image for post
GBPUSD exchange rate buy = 1.28820, sell = 1.28816 captured from cTrader

What is Forex?

Commission, Spread and Pips

Spread = Buy - Sell = 1.28820 - 1.28816 = 0.00004 = 0.4e-4
Spread = 0.4e-4 = 0.4 pips
1.28827 - 1.28816 = 0.00011 = 1.1 pips

Tick Data

Date                  | Sell    | Buy
20200930 00:00:00.220 | 1.28643 | 1.28654
20200930 00:00:00.322 | 1.28643 | 1.28653
20200930 00:00:01.025 | 1.28641 | 1.28655
20200930 00:00:01.754 | 1.28641 | 1.28654
20200930 00:00:03.403 | 1.28642 | 1.28653
20200930 00:00:04.204 | 1.28642 | 1.28655
20200930 00:00:04.255 | 1.28643 | 1.28654
20200930 00:00:04.356 | 1.28644 | 1.28656
20200930 00:00:05.520 | 1.28645 | 1.28657
20200930 00:00:05.853 | 1.28647 | 1.28657

Open High Low Close Data

Date           | Open    | High    | Low     | Close
20200930 00:00 | 1.28643 | 1.28663 | 1.28641 | 1.28659
20200930 00:01 | 1.28663 | 1.28675 | 1.28649 | 1.28649
20200930 00:02 | 1.28649 | 1.28650 | 1.28627 | 1.28630
20200930 00:03 | 1.28630 | 1.28648 | 1.28626 | 1.28638
20200930 00:04 | 1.28639 | 1.28647 | 1.28635 | 1.28640
20200930 00:05 | 1.28641 | 1.28654 | 1.28641 | 1.28651
20200930 00:06 | 1.28650 | 1.28655 | 1.28648 | 1.28653
20200930 00:07 | 1.28653 | 1.28654 | 1.28647 | 1.28649

Candlestick Charts

Image for post
Image for post
Candlestick Bars. Image by author
Image for post
Image for post
Candlestick Chart Starting 2020–09–30 midnight. Image by author

Forex Trading

Algorithmic Trading

Backtesting

The ML Model: Concept and Plan

Image for post
Image for post
Candlestick chart with Bollinger Bands (Green) and EMA (Cyan) indicators. Captured from cTrader

Model Choice

Technical Stack Choice

Hardware Choice

The Plan

Data Sourcing, Data Preparation, Model Training, Predictions
Data Sourcing, Data Preparation, Model Training, Predictions
The Plan. Image by author

1 — Data Sourcing

Date,High,Low
2010-01-01 00:00,1.61673,1.61659
2010-01-01 00:01,1.61670,1.61670
...
2020-10-01 23:58,1.28852,1.28838
2020-10-01 23:59,1.28853,1.28846

2 — Data Preparation

Time Interval and OHLC

df['HLAvg'] = df['High'].add(df['Low']).div(2)

Smoothing

Image for post
Image for post
SMA with 14 periods on GBPUSD 1-minute chart. Chart is from TradeView.com and the data source is FXCM.
df['MA'] = df['HLAvg'].rolling(window=14).mean()

Stationarity

Image for post
Image for post
df['Returns'] = np.log(df['MA']/df['MA'].shift(1))
Image for post
Image for post
df['MA'] = df['MA'].mul(np.exp(df['Returns'].shift(-1))).shift(1)
Image for post
Image for post
Time series with the simple moving average and the log returns

Batch Size

Train, Test Split

df = df[df.shape[0] % batch_size:]
df_train = df[:- val_size - test_size]
df_val = df[- val_size - test_size - window_size:- test_size]
df_test = df[- test_size - window_size:]

Process Summary

Image for post
Image for post
First step summary of the data preparation process

Scaling

Image for post
Image for post
Training Set: Scaled Log Returns between 0 and 1
scaler = MinMaxScaler()
train_values = scaler.fit_transform(train_df[['Returns']].values)
...
test_values = scaler.transform(test_df[['Returns']].values)
df['Returns_Prediction'] = scaler.inverse_transform(df[['Returns_Prediction_Scaled']].values)
joblib.dump(scaler, 'scalers/scaler.bin') # For persisting to file
...
scaler = joblib.load('scalers/scaler.bin') # For loading from file
Image for post
Image for post

LSTM Data Input Overview

Image for post
Image for post
Previous prices and their prediction. Image by author
Image for post
Image for post
A feature and a label. Image by author

Window Size

Image for post
Image for post
An illustration of samples shifted by 1 each. Image by author
batch_size = 32
window_size = 8 * batch_size # 256 minutes, 4.3 hours

Converting Samples

def convert_raw_samples_to_model_samples(scd_log_rtns, window_size):
X, y = [], []
len_log_rtns = len(scd_log_rtns)
for i in range(window_size, len_log_rtns):
X.append(values[i-window_size:i])
y.append(values[i])
X, y = np.asarray(X), np.asarray(y)
X = np.reshape(X, (X.shape[0], X.shape[1], 1))
return X, y
Image for post
Image for post
An illustration of the for-loop. Image by author

3 — Model Training

Image for post
Image for post
The dataflow to the model. Image by author
model = Sequential()
model.add(LSTM(76, input_shape=(X.shape[1], 1), return_sequences = False))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss="mse", optimizer='Adam')

Training Statistics

4 — Predictions

Image for post
Image for post
Dataflow before and after prediction. Image by author
y_pred = model.predict(X)
df['Pred_Scaled'] = np.pad(y_pred.reshape(y_pred.shape[0]), (window_size, 0), mode='constant', constant_values=np.nan)
df['Pred_Returns'] = scaler.inverse_transform(df[['Pred_Scaled']].values)df['Pred_MA'] = df['MA'].mul(np.exp(df['Pred_Returns'].shift(-1))).shift(1)

Single-Step Prediction

Image for post
Image for post
Single Prediction (1 minute). Image by author

Multi-Step Prediction

Image for post
Image for post
Several Multi-Step Predictions. Image by author

Continuing and Expanding the Research

Date Feature Engineering

Minimising the Effect of Outliers

Image for post
Image for post
A highlight of some outliers. Image by author

Very Small Mini Batch Size

Different Smoothing Method

Interval Aggregation

Sequence-to-Sequence Forecasting

1.2752, 1.2751, 1.2754, 1.2756 -> 1.2758, 1.2760, 1.2761

Disclaimer

Conclusion

Part Two: Using the Model from a Trading Platform

Image for post
Image for post
Backtesting of profit simulation of £1000 between 24/08/2020 and 30/08/2020 using this model. To be discussed in part 2. Captured from cTrader

More Readings

About Me

References

Towards AI

The Best of Tech, Science, and Engineering.

Sign up for Towards AI Newsletter

By Towards AI

Towards AI publishes the best of tech, science, and engineering. Subscribe with us to receive our newsletter right on your inbox. For sponsorship opportunities, please email us at pub@towardsai.net Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Adam Tibi

Written by

Adam Tibi

Software Architect from London with a certificate in Quant Finance and a background in Software Engineering. Passionate about machine learning, C# and Python

Towards AI

Towards AI is a world’s leading multidisciplinary science publication. Towards AI publishes the best of tech, science, and engineering. Read by thought-leaders and decision-makers around the world.

Written by

Adam Tibi

Software Architect from London with a certificate in Quant Finance and a background in Software Engineering. Passionate about machine learning, C# and Python

Towards AI is a world’s leading multidisciplinary science publication. Towards AI publishes the best of tech, science, and engineering. Read by thought-leaders and decision-makers around the world.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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