Image Recognition vs Other Techniques in Predicting the Financial Market
By: Chuan Bai
Tags: Neural Networks, Time Series Analysis, Convolutional Network, Financial Markets, Time Series Forecasting

Image Recognition vs Other Techniques in Predicting the Financial Market

This work was done in collaboration with @Vesko Karadjov who always sparkles with brilliant ideas.

The financial market deals with time series mostly, and there are tons of techniques and tools around time series prediction. What we going to do is to perform regression-based prediction using CNN and compare it with some traditional methods and see how it performs.

In the world of finance, financial product price or return prediction has been the common goal of any quantitative analysis. This has been studied over and over again for decades. What we are focusing here is the volatility of the market, to be specific, the volatility before and after the stock market open.

Problem

What we have suspected is that the volatility (vol) in the pre-market session might be a good indication of what might happen immediately after the market is open. If we can predict the vol accurately, we may be able to take advantage of it. As an example, we are taking price data on FTSE 100 CFD from OANDA.

Preparing Data

We have asked pulled the aggregated price data on a minute basis between Jan 2016 and Mar 2020, which has a format below.

With a time series that looks like this over the whole period.

The data that we are interested in is the volatility of price, which is essentially the standard deviation on price. Since we are using candlestick data, which has high and low data, I am approximating the volatility by using the high price minus the low price within the minute bar and apply a rolling mean window of size n.

In principle, the vol should be high when there is a large price movement, as a sanity check we took a random trading date and look at its vol against its price in the day to confirm this.

To compare the vol across all the trading days, I have plotted the vols based on time. The vol is calculated based on a time window of 5 minutes. Each point represents the vol in the last 5 minutes, it was calculated every minute across over 1070 days.

We can see that the peaks seem to have duplicated with a leg of an hour, e.g. peaks at 0000 and 0100, 0700, and 0800. Let’s plot out the heat map to check the values.

We can see the effect of daylight saving, so we need to normalise the effect by shifting the summertime to GMT, as below.

There are some periods where vol is the biggest. The solid rows across the heatmap indicate consistent high volatility at certain times, which all of them are reasonable.

0100: Start of CFD trading

0700: EU pre-market open

0800: UK market open

1200: Mid-day

1430: US market open

1630: UK market close

We can also see the high volatility throughout the days in March 2020.

If we want to take advantage of volatility, the higher the better, hence the time with the highest interest is the market opening time, i.e. 0800. If we can use the vol an hour before market open to predict the vol 5 mins after open which normally has the highest vol during the day, we can make trades around this.

The chart above shows the combined vol across time for all the trading days between 2016 and 2020. We can see that the vol tends to pick up right after market open at 0800 and cools down a few minutes after.

We assume that there is a correlation between the vol before and after the market open. Hence, if we plot out the mean of vol an hour before market open and 5 mins mean vol after the market open, we should have a general upward trend, which it does.

The x-axis is the mean vol 1 hour before market open, the y-axis is the mean vol 5 minutes after the market open. There is a correlation between the two, but there is a lot of variances hence it is not a reliable predictor. For example, base on this chart, if we have a mean vol of 7.5 before market open, it is reasonable to assume that we will get a vol between 5 and 25 after the market open, which is not very helpful.

The Interesting Bit

We have essentially already done a linear regression analysis on the vol before and after the market open. However, to be fair to the more complex techniques, and demonstrate that the techniques are indeed predictive. We are splitting the 1070 samples into three groups, train, validation, and test.

Train: 0–699 (Ending 2018–10–09)

Validation: 700–899 (Ending 2019–07–25)

Test: 900–1070 (Ending 2020–03–25)

Classical Method 1 — Linear Regression

Since there is now a split, the stats for linear regression will slightly differ from the stats that generated using the full set of data.

Base on the charts above, we can see that the strong correlation still exists, but the coefficient of determination is very low, we can’t explain the variation in the vol after market open using this technique.

To improve the prediction, one of the ways is to increase the number of features, which leads us to multi-variant regression. In this case, we can use each minute in the hour before the market open to predict the vol after market open. This should give us a better result than the simple mean of the volatility before market open.

The stats improve slightly across the board, however, it is still not very useful as it is. All of the linear regression here are using OLS in statsmodel library.

Classical Method 2 — Gradient Boosting Method

When performing predictions especially classifications, gradient boosting is the standard approach which will give you a very good baseline for further improvements. Although regression results based on gradient boosting can be choppy when predicting a continuous surface. It generally gives a reasonable performance with minimum effort.

Here we have used LightGBM as the library of choice. The features used to feed into the model are essentially the same as the multi-variant regression.

With the 5 different cross-validation evaluations, the model is not very stable across the different cuts of our data sample, suggesting there is some sort of imbalance of outlier samples in different segments of our data. Although this could be a problem when we dive deeper into the prediction process, for the scope of this article, as long as we have the same set of training, validation, and test set, we don’t need to worry that much, since this is essentially a first pass prediction method selection process.

As stated before, we need to keep the samples consistent across the different techniques, hence we only train on the first 700 samples, and predict the samples 900+.

Here is the result if we plot out the training and test set.

The training result is encouraging with an r-squared score of 0.94, but the predictions in test set fall apart. Although it is not doing a great job in the test set, this is still outperforming the predictions we made using the pure linear regression data.

Classical Method 3 — MLP (Neural Network)

When we have such high variance in prediction, a neural network may be able to describe the complex relationship between the input features and discover any hidden relationship between them.

We are using FastAI as the deep learning library to construct the underlying network, as it stands, FastAI is built on top of PyTorch. You can describe custom PyTorch models and pass it into FastAI to gain the training gadgets that FastAI provides.

We are using the same set of inputs and outputs as the LightGBM and feed the data into an MLP network of 2 hidden layers, each has 300 neurons.

After training for a few iterations, we can get a prediction result that will be similar to this.

We can see that the volatility is highest in the test set because of the recent volatility in the market, hence our validation data set is not very representative to our test data. However, it still did a pretty good job in terms of stats compared to the previous techniques.

Image Recognition based Regression

Before we move on to the regression-based on images, our target parameter will need to change a bit, because we will lose the numeric values during the conversion process. Since during the conversion process, the values are normalised within each time window before converting into images.

To compensate it, we have used the ratio between the mean vol before and after market open as our target instead. This way we are asking the neural net a slightly different question. How volatile will the market be after market open, in relation to the vol in pre-market? For example, if the mean vol before market open is 10, and the vol after market open is 50, the target we are aiming to predict will 5 instead of the raw 50.

To prevent comparing an apple to an orange, I have trained an MLP network, which has the same structure before, using the same data.

Note: The vol has been capped at 30 because of huge spikes after the division calculation, as shown below.

Raw vol_after / vol_before

Capped@30 vol_after / vol_before

The result from the indirect prediction made by MLP is slightly worse stats compared with the predictions on raw values, but not hugely different. Now we have a baseline for CNN network to compare with.

Image conversion

With the help of Gramian Angular Field and pyts library, we can now convert any time series into an image based on their polar coordinate relationship between the points. [original paper]

In this code, it creates one image per day, and each image describes the vol 60 minutes before the market open. We also export the target vol ratio to a CSV to facilitate label importing in FastAI.

For the CNN network, it is essentially using ResNET34 as the bottom layers, and bolt on a [1024, 512] dense layers on top, and perform the final regression using a simple linear activation node.

After several iterations of training, we are getting results like this.

We can see that the image-based regression performs much better than the MLP counterpart even predicting the same target.

Here is a summary of stats for all the different techniques for comparison.

We can see that that MLP is outperforming all the other methods when predicting the absolute vol value, and CNN is outperforming the same network in every aspect when predicting the relative vol ratio.

As a result, when making time-series predictions, CNN technique is an outstanding candidate of choice, although it does require drastically more computational power for image conversion and training.

If anyone is interested, we can potentially feed the raw volatility result into the final layers of the CNN, but it is not the scope of this article. Let me know if you have any other ideas or thoughts and hope you enjoyed it.