Pragmatic Deep Learning Model for Forex Forecasting

What is Forex?

Forex, Foreign Exchange, is a currency price relationship between two economies, e.g. British Pound vs US Dollar or GBPUSD. The first three letters in the symbol represent the first economy called “Base Currency” and the last three letters represent the second economy called “Quote Currency”. If the exchange rate of GBPUSD is 1.28818 it means that to buy \$1.28818 you pay £1, plus commission and/or Spread.

If you are exchanging with a friend, then you might use two decimal points and exchange the GBPUSD at 1.29, however, if you are exchanging via a Forex trading platform through a Forex broker, then there are fees.

Commission: It’s a fixed fee that the broker charges per transaction. The commission amount is broker-dependant.

Spread: Is the difference between the buying price and selling price. This is how the broker makes a profit.

Let’s take an example, if you have pounds and you want to buy dollars then the GBPUSD buy is 1.28820, conversely sell price is 1.28816. That makes the spread:

`Spread = Buy - Sell = 1.28820 - 1.28816 = 0.00004 = 0.4e-4`

The change in price in Forex is usually very small unless there is an event affecting the economy, so traders use PIPs to express the change.

PIP: Price Interest Point is currency specific. For most currencies, including GBPUSD it is: Change x 10000

We can consider the spread as a price change, so we can express it as:

`Spread = 0.4e-4 = 0.4 pips`

For example, if the selling price of GBPUSD changed from 1.28816 to 1.28827, we say the price moved up by:

`1.28827 - 1.28816 = 0.00011 = 1.1 pips`

Tick Data

A change in price, also known as “tick”, happens at random times, e.g. multiple changes per second or a single change in 2 minutes. Forex generates tick data rapidly, this is an example of GBPUSD tick data for the first 5 seconds of 2020–09–30:

`Date                  | Sell    | Buy20200930 00:00:00.220 | 1.28643 | 1.2865420200930 00:00:00.322 | 1.28643 | 1.2865320200930 00:00:01.025 | 1.28641 | 1.2865520200930 00:00:01.754 | 1.28641 | 1.2865420200930 00:00:03.403 | 1.28642 | 1.2865320200930 00:00:04.204 | 1.28642 | 1.2865520200930 00:00:04.255 | 1.28643 | 1.2865420200930 00:00:04.356 | 1.28644 | 1.2865620200930 00:00:05.520 | 1.28645 | 1.2865720200930 00:00:05.853 | 1.28647 | 1.28657`

There is another way to look at the data, especially when you want to check the rate price on longer periods (minutes, hours, weeks, etc…).

Open High Low Close Data

OHLC is another way to aggregate the data. OHLC can apply to any time interval such as a minute or an hour. The Open captures the sell price at the beginning of the time interval and Close captures the price directly before the start of the second interval. High captures the max that the price reached during the interval and Low captures the min reached. This is an example of 1 minute OHLC data for the first few minutes of the GBPUSD on 2020–09–30:

`Date           | Open    | High    | Low     | Close20200930 00:00 | 1.28643 | 1.28663 | 1.28641 | 1.2865920200930 00:01 | 1.28663 | 1.28675 | 1.28649 | 1.2864920200930 00:02 | 1.28649 | 1.28650 | 1.28627 | 1.2863020200930 00:03 | 1.28630 | 1.28648 | 1.28626 | 1.2863820200930 00:04 | 1.28639 | 1.28647 | 1.28635 | 1.2864020200930 00:05 | 1.28641 | 1.28654 | 1.28641 | 1.2865120200930 00:06 | 1.28650 | 1.28655 | 1.28648 | 1.2865320200930 00:07 | 1.28653 | 1.28654 | 1.28647 | 1.28649`

Candlestick Charts

Traders usually look at charts with OHLC data, this is why they use “Candlestick Chart” to better represent this type of data:

Note that the candlestick colours are arbitrary, the convention is to use a colour and an inverted colour such as black and grey, I used green and orange across this story. Next is our OHLC table above represented as a candlestick chart:

Essentially, if you believe the price is going to increase, you buy the base currency (GBP in our case) using the quote currency (USD in our case) and if you believe the price is going to decrease, you sell the base currency.

Trading is associated with a strategy, take this over-simplistic strategy as an example “Buy if you believe the price will increase by at least 10 pips and sell if you believe the price will decrease by at least 10 pips.”

Your belief in price change could come from many sources, the sky is the limit, examples:

• You think a political decision would affect one of the pair economies
• You expect an out of the ordinary announcement on GDP
• You use some technical indicators and base your decision on them
• You train an ML model on historic data and ask it to predict future prices

Algo trading is using a bot, a strategy written in code, and executing the trade automatically via an API or other means based on the bot recommendation.

An example is using a bot that will push an input data into an ML model and consult the model about the price change then trade accordingly.

I am writing a follow-up article on this platform to show how the model in this story will be used in algo trading, I will add a link when publishing the story.

Backtesting

When you have built a bot, you want to make sure the bot can make a profit, one way is to run this bot using a backtesting platform, preferably the same one as your production one.

Another level of backtesting is to run it in demo mode (virtual money) on the same production platform for a while, but with live data.

This way you mitigate the risk of loss, but, you still have other risks such as the risk of having a market pattern shift.

In my follow-up article, I will show how to backtest the bot that is based on this model.

The ML Model: Concept and Plan

In trading, if we want to know at a particular time whether to buy, sell or do nothing, we want to forecast if the price will go up or down and by how much.

To make a trade decision, a technical trader uses indicators that analyse a fixed number of previous time steps (price changes). If the indicators match a particular pattern this means a buy or a sell signal. The indicators, in essence, are trying to extract patterns out of the previous prices.

The idea here is to have our model to act as an indicator. We will train our model, using historic data, on price changes and whether they resulted in the price going up or down. If there are recurring patterns in the historical data, then we want our model to recognise them.

In brief, we want our model to recognise price patterns and advise us with the expected price change when encountering a pattern.

Model Choice

To meet our objective, we will need an ML model that would recognise time series patterns and forecasts the next pattern, so we can narrow down our selection to the applicable models.

Regression models, such as GARCH, ARIMA and Facebook Prophet, are good for less sophisticated time series prediction, so I excluded them in favour of deep learning neural network models such as Attention Networks and Long Short-Term Memory (LSTM) because they are more suitable for this prediction.

I favoured LSTM as the model is heavily researched compared to the newer Attention Networks, although I might do another research with the Attention Networks.

Technical Stack Choice

• Development: Python 3.8, Tensorflow 2.3 (with built-in Keras), Visual Studio Code with Jupyter Notebook, Visual Studio, Pandas, NumPy, Scikit-Learn, Matplotlib, Ubuntu 20.04

Hardware Choice

This wasn’t really a choice, this was what I already have.

• Laptop (for development): Dell Precision M4800, 32GB RAM, 8 Logical Cores Intel i7 2.9GHz, 2GB RAM Nvidia Quadro K2100M
• Server (for training): Dell Precision Tower 7910, 24GB RAM, 28 Logical Cores Intel Xeon 2.6GHz, Nvidia GeForce RTX 2080 8GB RAM

The Plan

We will go through the standard ML supervised learning process, we will source the data, prepare it in a structure suitable for the model, train the model and then use the model for predictions.

1 — Data Sourcing

I selected the GBPUSD Forex because there are abundance of free quality data available, down to the tick data and I am familiar with the data itself as I live in the UK (I can blindly pinpoint the 2008 Credit Crunch, Brexit Vote Day and COVID-19 Lockdown).

You can download the GBPUSD data from Python using sources like Quandl or as a CSV, as I have done. I used a Windows desktop software called Quant Data Manager to download the GBPUSD 1 minute data from Dukascopy Swiss online bank. This is a sample data:

`Date,High,Low2010-01-01 00:00,1.61673,1.616592010-01-01 00:01,1.61670,1.61670...2020-10-01 23:58,1.28852,1.288382020-10-01 23:59,1.28853,1.28846`

2 — Data Preparation

The Forex data is usually clean, so I have invested a little on this front. Also, rather than focusing on the code, I will put the effort into highlighting the quantitative finance concepts which will make the linked code self-explanatory.

Time Interval and OHLC

With Forex, you have easy access to tick price. However, tick data is highly volatile and the price change rate is not predictable and can be many changes per second or a single change in two minutes. Also, tick price generates too much data and this would increase ML training time.

I chose the 1 minute OHLC (Open, High, Low, Close) as I think the 1-min is a good balance between having a good amount of samples and a good training time. It is a common practice to use the closing price out of the OHLC. However, I don’t think this is the best representation of the time interval so I took the average price between the high-low and I called it HLAvg across the code:

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

Smoothing

Given the factors affecting Forex rate, I believe that using the smoothed time series instead of the actual change in price will yield a better prediction accuracy. I stuck with the basics of smoothing and used the simple moving average (SMA) with 14 periods. I chose 14 as this is the default period used in most technical analysis tools.