How to design a machine learning trading bot: Pattern Recognition


Bahman in How to

Dec 29, 2020

Previously on “How to design a machine learning trading bot”

We have started with “Collecting Data”:

We found out what is OHLCV data, and we learned why we need historical data and online data both together.

Then we continued with “Data Analysis

We have seen how cleaning the data including features engineering is important. We learned that to make a stable machine learning model we need to prepare data in the right way and finally, we found out how visualizing data can help us to reach the goal.

So, let’s start the third step considering that we already have valid data in our hands.

This article is the third episode from a series of articles with the title of “How to design a machine learning trading bot”

Step 3: Finding a pattern in analyzed data

Discovering patterns in the data is a very fundamental task that has been played by advanced machine learning methods and your creativity.

Let’s start with this main question in the market:

Do you believe, some patterns in the market repeat frequently? If so, we are on the same track.

Pattern Recognition

Let's start with one of the simplest patterns in technical analysis and that’s “Trend”. So, if you don’t know what is the trend about, just pause here for a minute and search on Google.

We can find two main patterns in the trend: Bullish (Up Trend) and Bearish (Down Trend). (Again if you don’t know about them just pause and search on Google :))

So, let's recap what we have done so far because it seems we haven’t done anything, but actually, we did what we were looking for.

In short, we break the problem into a smaller problem which was finding a trend (pattern) into finding a bullish/bearish trend (pattern). The main key here is we don’t start finding a pattern in raw numeric data blindly.

“Astrology” and “Horoscope”

In the pattern recognition phase, you should be so careful and aware. Finding a pattern could be like “Astrology” and “Horoscope” if you just look for any nonsense pattern. There are a lot of data out there and you can find many patterns, and even you can make many stories between these patterns, Oppositely, on the other side, we have “Astronomy”. Always make sure, you are following the data like an astronomer and a scientist, not an astrologer or a horoscopist.

Astronomer & Scientist way

Okay then. Let’s get back to our sample. We want to find the “Bullish Trend” pattern, and one of the simplest strategies to catch this pattern is using the “Moving Average”. (Pause and google it)

Bullish and Bearish pattern:

Finding a bullish trend by following toward SMA20 (Simple Moving Average 20) is one of the simple and easy ways in the market, I’ve called it the “Hello, World!” in trading courses ;)

In the following chart, you can follow the SMA20 with the black curved line. You can simply say:

Bullish: If Close > SMA20

Bearish: If Close < SMA20

(Gold/USD, 5m, 27 Nov 2020)


Machine Learning

So, when machine learning shows up?

To solve a problem with machine learning, you need to choose a method. For example, there are some problems that you can find a solution with “Unsupervised Learning” methods, and some problems are solved with “Regression” methods.

Again and as always we start with one of the simplest methods which is “Binary Classification”.

Binary Classification

In binary classification, you have a label [0,1] for your records. Plus, the machine is going to predict 0 or 1 for your new record.

Now, How binary classification can help us to find a bullish trend? Practically, we will develop it in our next season (How to develop a machine learning trading bot), but having a conclusion for this episode, we assume:

To find a bullish pattern we set the label to 1 if Close > SMA20 for each record and we set it to 0 if it’s not. In the end, we have a dataset of OHLCV (check out the very first episode, Data Collection) including an indicator SMA20, plus a target column (label) [0,1]. After training with ML methods, if the prediction comes to 1 it means we have a bullish trend, and if it shows 0 means we are not in a bullish pattern.

If you’ve noticed so far, I always start with the simple concept and I believe if you fully understand the idea, then you can develop it into a more complex solution.

This post was originally published on