Trader logo

Deep Learning for Forex Trading

An Exploration of Machine Learning Techniques for Predictive Analysis in Forex Trading

By tissue leekPublished about a year ago 6 min read
Like
Deep Learning for Forex Trading
Photo by micheile dot com on Unsplash

Many research papers cover the prediction of financial time series but only a small number of them speak about the application in a real trading strategy. Most of the time the research only gives the performance metrics of the model (accuracy, RMSE, …) but without trying to transform it into a profitable strategy. When we talk about financial time series, we talk about stochastic processes, meaning it deals with a lot of randomness. For this reason, it’s unrealistic to expect getting an accuracy similar to the ones obtained in many other applications of Deep Learning. So, don’t expect getting a 80% accuracy when predicting the market to go up or down for a given time horizon. Predicting Financial Time Series is known to be one of the hardest task in Machine Learning. The purpose is instead of trying to find a model that can give you a small edge when compared to a pure random guess, or to a pure Buy and Hold strategy when we speak about the stock market. There is also a lot of things to take care regarding the data processing when applying Machine Learning to Financial Time Series. If you are a beginner in this field of research or if you get good metrics from your model but poor real trading results, you can read our paper covering this subject, Financial Time Series Data Processing for Machine Learning, it can help you to understand what’s going wrong. For years now, and in collaboration with two French engineer schools, CentralSupelec and Polytechnique, Lusis AI Department is working on Financial Time Series predictions in the perspective of applicable trading strategies. In this article and the future ones, the reader will find some methods and results than can help him a lot with his own research. But because much more than in any other domain, an AI Based Trading Strategy that works can directly be transformed into money, so we will obiously never disclose the most important things enabling to reproduce our models. If you find a reproducible model with very good metrics anywhere in a paper, take care and do your own research and backtest before investing with it.

Predicting GBPUSD intraday trend

In this article we illustrate the application of Deep Learning to build a trading strategy. We first create and evaluate a model predicting intraday trends on GBPUSD. Then we backtest a strategy solely based on the model predictions before to make it run in real time. Dataset : GBPUSD one hour OHLC data between 04/11/2011 and 01/30/2018, so it represents 41,401 one hour OHLC bars, for about 7 years of data Training Set : 2011–2014 Validation Set : 2015 Test Set : 2016–2018 Label : Up/Down closing price N bars later, our predictor is a classifier

Training — Validation — Test Periods on 1H GBPUSD After determining the label, we need to check the proportion of Up and Down classes in the population. Finding unbalanced labels means we would need to rebalance them, or use another metric than the accuracy. Here, we found 50.3% of Up, for 49.7% of Down class, so the Accuracy is a reliable metric for evaluating our model. Here, for confidentialty reason, we will not mention the following : prediction horizon (N bars) size of the time slices used as input for the model features (raw or derivated) we use filters or any other pre-processing applied exact model hyperparameters But we will show that surprisingly, a simple model can perform well.

The Model

In most of the cases when we speak about Time Series predictions we first think about using LSTM or CNN 1D. Here, we wanted to evaluate a MLNN at first to be compared to the two others. But after research we have found a way to make this model accurate enough to build a trading strategy with it. The following diagram illustrates a MLNN similar to the one we use:

MLNN The model output layer has two units, one for the Up and one for the Down class. It uses a softmax activation function. The loss function is a categorical cross entropy. Why do we use such multiple unit output with softmax instead of a single unit with a sigmoid activation ? The reason is that it makes easier to create generic functions for testing various models with labels having more than two classes. The simplest case is Up/Down/Neutral, but we could also have more levels of Up and Down (strong, medium, weak, …). In general we always create a set of productivity functions at first to make the variations of models easier and faster to be built and tested. This accelerates a lot the iterations. Then we can concentrate the efforts on the most important things, the model itself and its performances metrics. About the other point we can disclose, the activation function of the Dense layers is tanh, we use an adam optimizer and dropout layers for regularization. We train the model on 500 epochs with batch size of 64 then get the following results:

Training and Validation Loss and Accuracy The loss function has nothing to do with what we usually get in many other Deep Learning applications. Keep in mind that financial markets is a very noisy context where we can only expect to get a small edge. We can observe the loss improves slightly up to 300 epochs then starts to move up. This indicates some overfitting persists. There are several ways to fight overfitting including: Getting more data : here we only use GBPUSD, we cannot easily apply data augmentation techniques like found in images classifiers. An inverted Cat picture is still a cat, but this is not such simple for financial time series. Any small modification of data can result in very different situations, we can never make sure the label will be the same than for the original sequence. Capturing longer history or adding data from other currency pairs can help Simplifing the Model : this is often the most efficient way to fight overfitting, before to use regularization, here it’s already done Regularization : here we only kept the dropout after testing L1 and L2 regularization Early Stopping : we don’t use it for the moment as we need to capture the best metrics around a given epoch (here somewhere between 250 and 350), an not only a precise epoch like 300. As the method does not natively exists in Keras, we need to implement a custom one So here, we already did two of these approaches, and before to experiment with more data we check the accuracy and see that we make better than a random guess with 0.56 on the validation set. For some epochs the model even did better with an accuracy exceeding 0.6 but before to try getting it with an optimal early stopping, we want to check at first how a trading strategy based on this predictor behave. So we backtest it.

Backtesting

We backtest the model on 3 years of data that were not used for training, so between 2015 and early 2018. The strategy built from the model is very simple. We simply buy when the model predicts an Up trend and sell short when the model predicts a Down trend. In order to evaluate the pure model performance, we don’t set any stop loss or take profit. We simply close the position N bars after the entry, where N is the prediction horizon. We also don’t set any spread for the first backtest in order to get the raw metrics. All the results below are expressed in pips. To get it we use a trade size of 10,000 GBP, so each pip represent 1 USD. We start with a 10,000 USD account. Before speaking about leveraging, every backtest must first start with non-leveraged trades in order to evaluate the risk that can be taken.

investingstockspersonal finance
Like

About the Creator

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.