Abstract

Artificial intelligence methods are important tools for mining information for forecasting in the stock market. Most of the literature related to short-term stock price prediction focuses on the technical data, but in the real market, many individual investors make investment decisions more from stock price shape characteristics rather than specific stock price values. Compared with traditional measurement methods, deep neural networks perform better in processing high-dimensional complex data such as images. This paper proposes a model that combines CAE (convolutional autoencoder) and LSTM (long short-term memory) neural network, uses CAE to extract stock price image feature data, and combines technical data to predict short-term stock prices. The results show that the CAE-LSTM model, based on stock price image morphological feature data and technical data, performs well in short-term stock price prediction and has good generalization ability. The root mean square error of the CAE-LSTM model decreased by about 4% from that of LSTM. CAE-LSTM models have better predictive power than LSTM models that only use technical indicator data as valid inputs.

1. Introduction

Stocks are an important part of modern financial markets. Shares are popular among Chinese investors, benefiting from relatively high returns. However, the factors that affect the stock price are very complex. The stock price is often affected by various factors such as investor behavior, market sentiment, dividends, market policies, economic development conditions, economic policies, credit conditions, interest rates, exchange rate levels, and international capital flows. A good stock price prediction method can not only help investors to judge the market form but also help regulatory authorities in collecting market information, so it has clear research significance.

From the perspective of forecasting methods, stock price forecasting models are mainly divided into two categories. One of the categories is the traditional financial time series forecasting method based on statistical econometric theory. This type of method mostly starts from the perspective of model setting and often has strong assumptions (such as normal distribution, stationarity, and Brownian motion). It may not be possible to obtain better forecasting ability when conditions do not match reality or when the market changes. Another category is the stock price prediction method based on artificial intelligence algorithms. This type of method is driven by data and discovers the law of stock price changes from the perspective of data. Under the condition of excellent data quality and large data scale, the fitting ability and out-of-sample performance of such methods can be higher than those of traditional statistical models.

From the perspective of influencing factors, long-term stock price changes are more affected by economic fundamentals, such as cyclical company stocks perform better during economic expansions and defensive company stocks perform better during recessions; the expected rise in the exchange rate may bring international capital inflows, which will make the market index rise; and the easing of credit may help companies expand production, which will be good for stock prices in the long run. In the short term, stock prices are more affected by technical data such as opening high and closing low, as well as existing technical analysis methods and morphological analysis methods in the market.

Some scholars have studied the use of technical indicators to predict stock prices. Kim input technical analysis indicators into SVM (support vector machine) for training to predict the direction of changes in the valuation of the Korean stock market and compared the SVM with the feedforward neural network and case-based reasoning method, which verifies the feasibility of using SVM for financial forecasting [1]. Li used a BP neural network to predict the time series data of closing prices in the stock market and showed the effectiveness of neural networks in predicting stock prices [2]. Huang and Tsai used feature selection methods to optimize features, used self-organizing feature maps to filter input data, and used SVM to predict stock prices; the results showed that the stock index prediction based on SOFM-SVR was better than the traditional single SVR in terms of average prediction accuracy and training time [3]. Chang et al. labeled data into asset buy and sell points and combined dynamic time windows, case-based reasoning (CBR), and neural network methods to predict stock prices [4]. Peng used stock price time series data and compared the performance of SVM and a BP neural network on stock price prediction [5]. Patel et al. compared an artificial neural network (ANN), SVM, random forest, and the naive Bayes method and found that random forest performed better overall when technical indicators were input into the model as continuous values [6]. Qi et al. introduced Principal Component Analysis (PCA) and the Improved Fruit Fly Algorithm (IFOA) based on the BP neural network method and simulated the stock price data of the Shanghai Stock Exchange; the experimental results showed that the combined model method was better than the single method [7]. Peng used the wavelet noise reduction method to remove the noise in the stock price data before building the LSTM neural network, which improved the prediction accuracy by 30% [8]. Huang used stacked denoising autoscoring to extract features from the basic market data and technical indicators of financial time series, using 11 technical indicators, such as the price and trading volume of the Hong Kong Hang Seng Index, as inputs and used the LSTM model to predict the closing price of the stock index [9]. Sun used stock index time series data as model inputs to build a BP-LSTM model. Experiments showed that the prediction accuracy of the built model was improved compared with the traditional machine learning model [10]. Zhao proposed a stock prediction hybrid model (LSTM-CNN-CBAM) that introduced an attention mechanism based on combining a long short-term memory network (LSTM) and a convolutional neural network (CNN), and the experimental results showed that the introduction of the attention mechanism effectively improved the feature extraction ability of the network [11]. From previous research, most of the literature related to short-term stock price forecasts has been based on technical data, but in the actual market, many individual investors make investment decisions based on the shape characteristics of stock prices rather than specific stock price values.

Autoencoder (AE) is an effective method for dimensionality reduction, and it is widely used in image feature extraction and image compression. The encoder maps the input data from the high-dimensional space to the encoding of the low-dimensional space for the purpose of dimensionality reduction, and the decoder reconstructs the input data from the corresponding encoding to restore the original high-dimensional input [12]. Masci et al. combined the advantages of convolutional filtering in convolutional neural networks with unsupervised pretraining of autoencoders and proposed convolutional autoencoders (CAE) [13]. It has been well applied in terms of [1416].

This paper proposes a model that combines a CAE and an LSTM (long short-term memory) neural network, using CAE to extract stock price image feature data and combining technical data to predict short-term stock prices. This paper compares the results of the new model with the LSTM model using only technical data as valid inputs to illustrate the advantages of introducing image feature data for stock price prediction. The CAE-LSTM model used in this paper takes full advantage of the convolution operation of neural networks in image processing. This paper uses the LSTM model to solve the shortcomings of the traditional linear model in processing nonstandard high-dimensional data and uses the compressed dimensionality-reduced stock price image features as variable input to predict the stock price by analyzing the stock price shape.

2. Model

In addition to technical and fundamental factors, stock price image morphological characteristics may also have an impact on stock price volatility. Most traditional financial theories believe that all investors have sufficient information; that is, they assume that all investors will use all available technical and fundamental data in the market and effective forecasting models to make investment decisions. However, in practice, many individual investors who are unfamiliar with financial theory may only make investment behaviors based on the characteristics of stock price patterns; that is, if investors find that the current stock price pattern is similar to historical stock price patterns or other stock price patterns, they may make decisions on the current stock buying and selling behavior based on the trend of stock price changes in similar historical stages, thereby affecting the current stock price movement. At the same time, the reason for the phenomenon that the technical indicators and fundamental indicators are similar in the current and historical stages but the stock price changes are different may also be because the characteristics of the stock price images in the two stages are different, which causes investors to make different decisions.

This paper assumes that, in the short term, most individual investors trade stocks according to the shape of the stock price candlestick chart, while institutional investors mostly buy and sell stocks by analyzing technical data. Therefore, from a data-driven perspective, short-term stock price changes will be affected by historical stock price image patterns and specific technical data, as shown in formula (1). The stock price at the current moment is a function of the stock price image feature and the historical stock price at each moment of the historical moment .

Due to the extremely high dimensions and complex structure of image data, traditional linear statistical tools are difficult to use to analyze and process the data. Many traditional dimensionality reduction methods, such as PCA, are also ineffective. Therefore, this paper uses an unsupervised learning method based on CAE to extract and compress the stock price image feature data and input the stock price data into the LSTM long-term and short-term neural network for data-driven predictive analysis.

2.1. CAE (Convolutional Autoencoder)

An autoencoder is an unsupervised learning neural network, consisting of an encoder (Encoder) and a decoder (Decoder). The encoder inputs the original data and generates intermediate operation results, while the decoder restores the intermediate operation results to the original data. The similar characteristics of the input and output of the autoencoder make it suitable for data noise reduction, outlier detection, and other fields. If the output dimension of the encoder is set to be smaller than the original data dimension, the autoencoder can also be used for feature extraction and data compression. The simple usable formula is expressed as where and represent the encoder and decoder, respectively, both of which are multilayer neural networks, where each layer is shown in where , , , , and are the excitation function, input, weight matrix, bias term, and output of the -th layer of neural network, respectively. CAE replaces the fully connected layer operations in traditional autoencoders with convolution, pooling, and deconvolution operations. Benefiting from the advantages of a convolution kernel and a pooling layer in image edge extraction and image compression, respectively, CAE performs well in image feature extraction and image compression. This paper used 2-dimensional image data and adopted the average pooling method. The corresponding convolution and pooling operation formulas are shown in where represents 2D convolution, is the number of input data channels, and represent the width and length of the pooling unit, the number of input channels, and the pooling operation interval, respectively. Deconvolution is the backpropagation process of the convolution calculation, which is not described here.

2.2. LSTM Network

An LSTM (long short-term memory) neural network is a variant of a recurrent neural network RNN. It retains the excellent predictive ability of an RNN network for a time series. At the same time, it sets the input gate, forgetting gate, and output gate, which can deal with the problems of gradient disappearance and gradient explosion of an RNN neural network by memorizing and forgetting historical information. Its mathematical formula is shown in where , , and , respectively, represent the input data, hidden layer data, and cell state at time and , , , and , respectively, represent the calculation process of the input gate, forgetting gate, cell gate, and output gate.

3. Empirical Analysis

3.1. Data

The data in this article come from the opening price, closing price, highest price, and lowest price of the Shanghai Stock Exchange Index from 4 January 2000 to 27 May 2021. This research normalized the data and used the closing price as the price for the current trading day. In terms of image data, a candlestick chart of the Shanghai Composite Index was generated every 60 trading days, and each picture was 72 pixels long and wide. In order to eliminate the influence of external factors on the model and speed up the model training, the picture only retained some pixels of the candlestick chart and set the pixel value of the blank area to 0. In order to highlight the influence of stock price image morphology on stock price, this paper converted each image into a grayscale image and conducted model training. The total number of samples was 5184 of which 80% were used for testing and 20% were used for validation.

3.2. Model Training and Result Analysis

In order to keep as many image features as possible while compressing the dimension of image data, CAE adopts the structure of “wide at both ends and narrow in the middle,” that is, set more channels in the convolutional layers near the two ends to retain more information, set fewer channels in the middle part of the network, and combine the average pooling layer to compress image features as much as possible. The structure of the CAE is shown in Figure 1.

It can be seen from the learning curve (Figure 2) that after about 40 rounds of training, the convolutional autoencoder converged, the MSE of the training set was only about 0.07 when it converged, and the error curve of the test set also converged synchronously. The model performed well out-of-sample without overfitting.

Comparing the stock price images before and after the input CAE model (Figure 3), the stock price image output by the model was very similar to the stock price image generated by the input original data, so the convolutional autoencoder in the model well preserved the stock price image features.

In order to prove that the low-dimensional feature data extracted and compressed by CAE can reflect the image morphological characteristics of stock prices, this paper selected four stock price images at different times and calculated the cosine similarity, as shown in Figure 4. The stock prices of pic1, pic2, and pic3 were similar in shape, and the cosine similarity of their CAE-encoded feature data was also very close. The CAE-encoded feature cosine similarity of pic1 and pic4 was relatively low (see Table 1), and the difference in stock image shape was also high.

Since the data compressed by CAE still had 81 dimensions, in order to further compress the CAE edge feature data and avoid the adverse impact on the next LSTM model training due to the high dimension of the image feature data, this paper used the PCA algorithm to further compress the stock image feature data. The image feature data was further compressed to 29 dimensions, while retaining 90% of the variance contribution rate (as shown in Figure 5).

The LSTM network consists of an input layer, an output layer, and two LSTM layers. The loss function uses the mean square error MSE, and the optimization algorithm adopts the Adam method. In order to avoid overfitting caused by too high data dimensions, L2 regularization was used in the optimization process. From the learning curve (Figure 6), the LSTM network reached the convergence faster, and the mean square error MSE was around 0.004 when it converged. The performance in the sample was excellent, and it better simulated the price of the Shanghai Composite Index.

The RMSE of the test set was calculated to be about 0.003, which was smaller than the RMSE of the training set. The model had no overfitting phenomenon, and the out-of-sample performance was good. From the stock price prediction curve of the test set (after normalization, as shown in Figure 7), the model fit the Shanghai Composite Index well, did not overfit the short-term fluctuation of the index, and had good predictive ability.

Comparing the root mean square error (RMSE) of the LSTM model prediction using only technical data as valid input data (in order to eliminate the influence of variable dimension changes on the analysis, the original stock price image feature data were replaced with random numbers that did not contribute to the prediction), the RMSE of the CAE-LSTM model combined with image feature data decreased by about 4% (as shown in Table 2), and the prediction performance was slightly improved.

The smaller margin of error may be due to the fact that in most cases individual investors make short-term trades through stock price image features, but on average, individual investors may have difficulty forming a unified trading view. In addition, from the perspective of model setting, the image feature data compressed by CAE were morphological features calculated from pixel points, but their feature value size represented more category information rather than numerical information. Therefore, converting image feature data into category data through algorithms such as clustering may be further helpful for stock price prediction.

The application of CAE-LSTM provides the following economic significance. First, as an important research field of graphic analysis in stock market technical analysis, the candlestick chart has accumulated a series of analysis and application rules through the application summary of investors over the years. However, the candlestick chart relies heavily on subjective factors such as the analyst’s personal experience, and different people have different perceptions. Therefore, in terms of a candlestick chart representing historical information, whether it really contains information that can predict future price changes has become a research direction worthy of attention. With the extensive research on deep learning in recent years, the application of deep learning models to stock market forecasting has also become a hot research direction, and the LSTM in this paper is a good application. Second, image data processing is key to applying deep learning to the candlestick chart. This paper used the advantages of CAE to perform feature extraction and dimensionality reduction processing on the image data, which further enhanced the application of deep learning in the candlestick chart analysis. In general, CAE-LSTM provides a useful intelligent analysis tool for stock market price and volatility prediction.

4. Conclusion

This paper argued that the short-term stock market price is affected by the morphological characteristics of historical stock price images and specific technical data, because individual investors often make decisions on the current stock trading behavior based on the morphological characteristics of historical stock price images, while institutional investors use more specific technical data for analysis. According to this assumption, this paper proposed a short-term stock price prediction model based on historical stock price image feature data and technical data. The model used CAE to compress and extract stock price image features and input the historical technical data into the LSTM model to predict short-term stock prices. The results showed that the CAE-LSTM model based on stock price image morphological feature data and technical data performed well in short-term stock price prediction and had good generalization ability. There was a small increase in predictive power compared to the LSTM models that only used technical data as valid input data. The model proposed in this paper provided a better analytical tool for stock price forecasting.

In this paper, the CAE-LSTM model that used stock image morphological feature data and technical data as variable input had a small improvement in predictive ability compared to the LSTM model that only used technical data as effective inputs. While optimizing the traditional stock price prediction method, this paper partially proved the positive effect of the CAE autoencoder in extracting stock price morphological features, providing guidance for subsequent researchers to conduct related research on stock price morphologies.

At the same time, this paper may have the following shortcomings. (1) The image feature data extracted and compressed by CAE was still high-dimensional data, and its similarity could only be measured by indicators such as cosine similarity. However, it is difficult to analyze the specific logic of its effect on stock price changes through traditional linear statistical methods. (2) Through the image feature extraction methods of CAE and PCA, although the compressed image data were effectively extracted (compressed from 5184 dimensions to 29 dimensions in this paper), the dimensions of the image feature data were still higher than those of stock price data, which may have a negative impact on model predictions. The above issues can be the direction of future research.

Data Availability

The data used to support the findings of this study are available from the corresponding author upon request.

Conflicts of Interest

The authors declare that they have no conflicts of interest.