The use of machine learning in financial trading is gaining traction. One problem of using computers to trade is in the recognition of trading patterns in price data. This paper gives a practical solution in the form of Python code, using existing machine learning libraries. The example includes data smoothing, which is seen as an important step in improving the accuracy of trading systems. The source code is provided to allow the reader to experiment further.

# Introduction

If a trader can make profitable trading decisions simply by looking at trading data, then it is reasonable to expect a computer to be able to match, or better the performance of a human at the same task. The use of computers to automated the trading of stocks, shares and currencies is an ongoing task, with many separate entities competing to produce favourable results.

This guide takes the reader though the process of using the Python programming language and the “sklearn” libraries to build an unsupervised machine learning system which can categorise samples of market data. In doing so, the process isolates the volatility problem seen in trading systems, and uses data smoothing to manage this problem.

## Supervised and unsupervised learning

Machine learning can be categorised into two forms; “supervised” and “unsupervised” learning. In the former, a sample set of data is used with known outcomes. This data is used to “train” the machine learning system. An example of supervised learning might be the use of news feeds, where some might have a positive affect on a stock, while others may have a negative affect. Training data would consist of sample news feeds in a suitable format, as well as the outcomes of these feeds on the stock. The training system inputs and outputs are then used to train the system to identify positive and negative news articles.

Gong, Si, Fong, and Biuk-Aghai (2016) described in detail a system for identifying typical market patterns in data, using a supervised learning approach in their paper; “Financial timeseries pattern matching with extended UCR Suite and Support Vector Machine”.

Unsupervised machine learning, as opposed to supervised machine learning is a method of “training” a system with data that has not been categorised, allowing the system to group the data as best it can. For example, given the data in figure 1, and a request to classify this data into two discrete categories, the system would attempt to find two reasonable data sets within the data, such as A and B shown in figure 2. Once the system has “learned” its categories, the parameters used to define them can be used to group new data into the same two categories.

Financial data categories, used by many traders, such as “head and shoulders”, “double bottom/top” and “saucers”, although commonly referred to, are not defined to the level required for computational systems. Instead of using these categories, we shall use an unsupervised learning algorithm to let the system categorise sample financial data.

## K-means clustering

“k-means” is a relatively simple clustering algorithm, commonly used in unsupervised machine learning.

For a given number of clusters, k, the algorithm tries to minimise the total distance between each data point and their nearest cluster centre (see figure 2). To begin the process, k random points are chosen. Using an iterative process, the system moves the cluster centroids to reduce the total distance.

The nature of the random first choice and the ambiguity in some groups may sometimes lead to different results if the algorithm is re-run.

## Data features

Machine learning data may consist of two dimensional data; rows of discrete sets of “features”, where each feature is a measurable quantity that describes the entity. Each row, or set of features is called a “feature vector”.

In our example we are interested in the movement of a particular trading symbol. Each entity must therefore have features that describe the movement. To achieve this, we simply split the data into time slices of trading, with each feature being a price of the symbol within the time slice.

To further improve the pattern matching, the data is smoothed. The mean is calculated over several trades, and it is these mean values which are time sliced. Figure 3 shows an example where the mean of three trading values are calculated to form each feature. In the example, there are also three features per feature vector.

In the example code shown later, 20 features are used per feature vector, with each feature being a mean of 10 trading prices.

# Clustering program

## Requirements

To run the code, Python 3, and the following Python packages are required:

• numpy
• matplotlib
• sklearn

## Data

A sample of forex trading data is read in by the program. This is a list of 20,000 consecutive GBPUSD asking prices taken from a typical trading time period.

The code takes this training data and splits it into two sections; training data and test data. The training data is used to train the unsupervised learning algorithm and create the parameters necessary to group the data. The training data is then grouped by the system so that the different groupings can be shown.

The test data is then used to prove that new data, unseen by the system can be correctly grouped by the system.

## Program overview

The computer program takes training data samples and groups them into 6 categories, choosing the data grouping criteria by looking for common data shapes. It then uses the same grouping criteria on test data samples to show that it can correctly identify the data shapes.

The program output is a set of graphs showing:

1. Raw training data
2. Training data split into discrete samples and then categorised by the system
3. Test data split into samples and catagorised by the same system

# Data Model

First, the required packages are imported. The sklearn package contains a varied selection of common machine learning tools to vastly reduce the implementation time of common machine learning algorithms in Python.

The program settings can be altered to modify the various settings by changing the following variables:

Data is taken from the dataSource URL into the “data” variable as a numpy array.

The data will be divided into samples, but first it must be resized to ensure that the data exactly divides into the given sample size.

Samples may be of different orders of magnitude, but show a similar trading pattern. To eliminate problems of magnitude in pattern matching each sample is scaled, so that they all have zero mean and unit variance.

Market data can be choppy, especially at a macro scale. To smooth the data, it is averaged over every ten trades (or “ticks”). The smoothing can be modified by changing the value of the “dataPointsPerFeature” variable. The data is them split into samples of 20 smoothed data points (controlled by the “numFeatures” variable). Each sample therefore forms a view of 200 trading ticks, feature reduced to make 20 data points.

The data is then split into samples of training and testing data.

The training data is used to create the k-means clustering parameters.

The clustering indexes for the training data are then predicted, so that we can display the training data samples later to give examples of the data shapes that the algorithm has found.

The testing data is then used to test the k-means clustering. Later this can be visually compared to the clustering of the training data to ensure the clustering algorithm is operating correctly.

# Results Plots

The remainder of the code is dedicated to plotting the results.

## Raw Data

The graph below shows the raw training data; asking price plotted against time. This is the original input data before being chunked into samples, and split into training and test data.

## Training Features By Group

The training data has been used to train the k-means algorithm, which created the data categories based on similarities found in the data.

The training data was then categorized into the groups that the k-means algorithm found. This was to allow us to plot the training data, grouped by category, so we can see the shapes of the various categories.

Group 0


Group 1


Group 2


Group 3


Group 4


Group 5


As can be seen from the training data, the 6 groups do indeed show different trends.

Finally, samples from the test data are shown. These have been run through the k-means fitting algorithm to categorise the data into the groups created during training.

Test data 0 categorized as group 4


Test data 1 categorized as group 3


Test data 2 categorized as group 1


Test data 3 categorized as group 0


Test data 4 categorized as group 0


By looking at the test data plots, a visual inspection of the shapes of the graphs and their grouping, compared to the training data plots shows that the system has correctly grouped this data.

# Conclusion

Python’s sklearn libraries provide a quick and easy way to test out machine learning on market data. The speed at which financial market data flows, far exceeds the ability of a human to fully register all of it. Categorising such data computationally may offer significant savings over manual methods of data recognition, thus providing greater opportunities for trading.

This program isolates a single problem in using machine learning to predict market trends and make successful trades; that of identifying time bound patterns in price data. The test program allows us to explore the use of data smoothing to more accurately predict patterns. In isolating the problem we are better able to understand how smoothing can improve the efficiency of a trading system.

It may be that an efficient trading system could be build of machine learning “layers”; with each layer solving a discrete problem.

# The Complete Code

Shown below is the complete code. The plot output has been modified slightly, just to show the plots in one, rather than many windows.