Guillaume Thibault
Trading Places: When Technical Analysts Meet Artificial Intelligence
The Efficient Market Hypothesis, developed by Eugene Francis Fama, states that the prices of financial securities represent all available information at all times, making it impossible to buy an undervalued stock or sell an overvalued stock. In this vein, under the least restrictive market efficiency assumption, prices reflect all available past information, making technical analysis useless for predicting future price changes.
Technical analysis is a method of financial analysis that focuses on studying the price history and trading volumes of an asset, such as stocks, currencies, commodities, bonds, etc. This method is based on the idea that market trends, patterns and past behavior can be used to predict future price movements.
Technical analysts use charts to represent historical price patterns and technical indicators to assess market strength or weakness. These indicators include moving averages, oscillators, trading volumes, support and resistance levels, and other measures that help identify trends and patterns in historical data.
In order to test the hypothesis of weak financial market efficiency, let’s try to get out technical analysis signals.
A use case for artificial intelligence?
Knowing that the main principle is signal extraction from these measurements, it may be interesting to try to use artificial intelligence tools to automate the technical analysis, instead of using my weak knowledge in the field. These machine learning algorithms can be used to identify patterns in historical data that are not easily identifiable by humans, or to detect anomalies in trends that might indicate changes in market direction.
The use of artificial intelligence in technical analysis can help automate certain tasks such as finding buy and sell signals, identifying support and resistance levels, monitoring trading volumes, etc. This can help traders and investors make faster and more accurate decisions based on more complete and reliable data.
Disclamer
It is important to note that using artificial intelligence to make buying and selling decisions in the financial markets carries enormous risks and can result in significant losses. Machine learning algorithms cannot predict market fluctuations with certainty and human supervision is necessary to avoid mistakes or misinterpretation of data.
That being said, this article does not follow this recommendation and instead aims to explore the possibility of using artificial intelligence to perform technical analysis in order to execute buy and sell orders in the financial markets.
In other words: Do not reproduce at home 😉
The project
The idea is to try out image analysis models having the translation invariance property on technical graphs, with the aim that machine learning techniques learn technical analysis signals directly. The article is dedicated to a first proof of concept in the subject.
The Data
To get started in a relatively simple way, let’s just use candlestick charts. This type of chart represents the price movements of a financial asset. Each candlestick represents the opening price, closing price, and the highest and lowest prices during the period under consideration. This type of chart allows you to quickly visualize the price movements of an asset and detect trends or patterns in the historical data.
In our case, let’s start by using a 2-minute interval per candlestick and compose charts with 30 candlesticks corresponding to 60-minute intervals per chart. Since we are going to use deep learning techniques on images, it is important to remove any information that could be used by the model to make its prediction that is not a candlestick. The following graph corresponds to an image that the model will have to analyze to extract a signal.
Knowing that these images will be used for training the model, let’s simplify the graphics to reduce the memory and computing power needed for training. To do this, let’s lower the resolution of the image, without losing information, by increasing the size of the bands and by using several colors to distinguish the different parts of the candles.
Create Dataset
The code for the data processing can be found in the full notebook.
Some additional details:
-
Source – yfinance python library
-
Tickers – 186 tickers with the highest transaction volume for February 17th 2023
-
Training dataset: 4464 images
-
Validation dataset: 1116 images
Date range (will automatically adjuste to the last 5 trading day):
- 2023-02-13 10:30:00-05:00 to 2023-02-17 15:30:00-05:00
- Date threshold for the 20% of the validation set: > 2023-02-16 19:18:00-05:00
Important step: Look at the distribution of our data to see if it is not too unbalanced
DataLoader
Let’s create a data loader for the dataset we created
Now that we have our dataset, we will need to split it in a training and validation set and we will need a data loader to be able to have minibatch.
Security check to ensure that resizing the image does not remove any information.
The model
The chosen model is a convolutional neural network (CNN) model which is a deep neural network architecture used in computer vision tasks. Convolution filters are a key component of CNNs. They are filters that scan the image to extract important features.
Convolution filters are applied to every part of the image and are used to extract features such as edges, textures and shapes. Convolution filters are defined by their dimensions (width and height) and depth. During training, the CNN automatically adjusts the values of the convolution filters to optimize the performance of the classification or regression task.
The model used will be Restnet-18, a deep convolutional neural network (CNN) model, developed by Kaiming He and several other researchers at Microsoft Research originally published in the paper “Deep Residual Learning for Image Recognition” released in 2015.
- Reference: https://arxiv.org/abs/1512.03385
This type of architecture is very popular because of the use of residual connections, a method used to solve the problem of degradation of the network accuracy when the network depth increases by adding direct connections that bypass several convolution layers. These residual connections allow the network to retain previously learned information and thus facilitate the learning of new image representations.
MLP
For reference, to demonstrate the potential utility of this type of model, let us establish a benchmark against which to compare the CNN model. We will use a simple dense neural network (MLP) model, in which each neuron is connected to each pixel in the image, as follows:
Now, the training of this model can be done in a few minutes.
Result
Looking at the preliminary results of such a model, we find that it is unable to detect patterns in the training and validation set. The results correspond to a completely random outcome.
Resnet-18
After that, we can try with Resnet-18 We can then create the model with the residual block defined earlier.
Parameter that we will use
Now the training will take about 10 minutes on a Tesla T4 graphics card
Results
Looking at the preliminary results of this experimentation, we can see that the Restnet-18 model succeeds in extracting patterns from the training set. The filters learned by the model also succeeded in detecting patterns in the training set and the predictions achieved an accuracy of about 50%. This result is promising. It is important to mention that the dataset was designed to be relatively realistic, i.e. training the model on a historical dataset and making the prediction on a dataset with a date that is later than the training dataset. The opposite case gives more impressive results of ~60%, but these are not realistic.
Conclusion
The use of deep learning model to learn technical chart signals proves to be a success. The model has proven to make the right decision between buying, doing nothing and selling at a accuracy of 50%, which is higher than random (33%) and always taking the dominant class (40%). The use of the model in practice remains inconceivable without using other analysis tools.
The results of the article impress me personally because I still have difficulty believing that signals can be hidden in historical data because the past is not a guarantee of the future.
In reality, technical analysis is used by many traders and investors to make decisions to buy and sell assets. However, these methods do not take into account the fundamentals of a company or market, such as financial data, economic news, macroeconomic factors, etc. Therefore, technical analysis is often used in combination with fundamental analysis to get a more complete picture of the financial markets.
Some points that may be interesting to investigate further:
- Visualization of the filters learned by the model.
- Increase the volume of data used.
- Adding more fundamental analysis curves on the charts.
- Test different models, because only one was tested.