# A Study on Stock Graph Recognition Based on Wavelet Denoising and DTW Algorithm

Authors: Ji Wei Luo
Journal: Mathematical Problems in Engineering
 (2021)

---

## Abstract

The “classical pattern” of stock price formation has long been widely used in the determination of future price trends of stocks, and the identification and analysis of classical price patterns have an important guiding role in investors’ decision-making and trading. The wavelet transform is a useful tool to remove some of the noise of time series because it has the characteristic of multiresolution. In this study, we propose a method for stock price pattern recognition based on the wavelet transform and dynamic time warp (DTW). A pattern recognition method with similar quantified results is developed to obtain accurate pattern recognition results. That is, using the wavelet transform to smooth the original price graph, and then using the DTW algorithm improved in this study to find the graph with the smallest distance from the target graph under the sliding window method, the identification and analysis of the target graph can be realized. In order to improve the recognition rate of the target graph, we preprocessed the raw price sequence using the moving average convergence and divergence (MACD) algorithm based on the control experiments set up in this study. The pattern recognition method used in this study will identify the price patterns of a certain time window as a whole, thus avoiding the problem of how to objectively select the important points that constitute a price pattern and the mathematical definition of different price patterns in the previous traditional methods.


---

## Body

## 1. Introduction

Technical analysis based on trading pattern curves is an important method for quantitative analysis of stocks, and in recent years, there have been a number of scholars who have proved the effectiveness of technical analysis from different perspectives as an effective means of extracting information from market prices [1–3]. With the development of financial engineering, computer technology, and mathematical algorithms, technical analysts using traditional trading model analysis methods now regularly use statistical software packages and other data processing techniques. The use of data analytics can minimize or even completely eliminate human subjective influences and turn trading into a scientific practice. Therefore, it is important to find a method that can quickly and efficiently identify trading pattern curves automatically to help investors grasp the volatility patterns of the stock market.Existing stock pattern recognition methods can be divided into rule-based pattern recognition and template-based pattern recognition, both of these methods are based on the extraction of important points and are subjective in nature. Therefore, this study attempts to use the overall graphical identification, that is, the overall identification of the price pattern of a certain time window the abovementioned approach can avoid the problem of how to objectively select the important points that constitute the price pattern and the mathematical definition of different price patterns. Based on this, this study selects an arbitrary time window from the A-share market as the target graph, which avoids mathematical definition of price patterns and increases the universality of the method. The appropriate wavelet parameters are then selected by empirical analysis and the original price series is smoothed using the wavelet transform. An improved DTW algorithm is used to accurately identify the target pattern by finding the stock among the remaining stocks that is most similar to the target pattern. Scholars also recently used machine learning or other intelligent algorithms for financial time series graph identification, but the basic approach is consistent with the abovementioned two categories [4–6].The main tasks of this study include the following: (1) the entropy weighting method is used to determine the weights of the two indicators of information extraction rate and information extraction error, and then to determine the composite indicators according to their weights, and finally to determine the number of optimal decomposition layers of the wavelet and the selection of the wavelet threshold function based on the value of the composite indicators. (2) The DTW algorithm is improved to overcome the problem that the algorithm itself is far away from similar shapes with certain phase differences. This study uses an improved DTW algorithm with a sliding time window algorithm to find the most similar time window to the target graph. (3) Attempt to compare the average rise and fall of the target graph to its similar graph sixty days later and determine that it is a realistic guide.The overall structure of this article is as follows. Section1 is an introduction that reviews the background of graphic recognition technology and the main work of this study. Section 2 presents work on graph recognition. Section 3 briefly introduces wavelet denoising and the theoretical foundations of MACD metrics and explains the principles of the wavelet threshold function. Section 4 describes technological improvements related to the characteristics of financial time series. Section 5 empirically determines the number of layers of the wavelet optimal decomposition as well as the threshold function and the graphs most similar to the target graphs found by the abovementioned methods. Section 6 compares the optimal graph with the forty-day rise and fall of the target graph. Finally, we conclude this study in Section 7. The specific research ideas are shown in Figure 1.Figure 1
Research idea.


## 2. Related Work

The current research on automatic price pattern recognition has fallen into two main categories as rule-based pattern recognition and template-based pattern recognition.Lo et al. [7] were the first to mathematically define ten price patterns such as head-and-shoulders and double-bottoms using the partial pole method. He proposed a systematic, automated method for technical pattern identification using nonparametric kernel regression and applies the method to a large number of U.S. stocks from 1962 to 1996 to evaluate its effectiveness for technical analysis. The abovementioned method creatively realizes the automatic recognition of price patterns by computer. Suh et al. [8] defined a Bezier curve as consisting of seven significant points and then identifies the pattern of the “cup with handle” by calculating the correlation coefficient between the time window and the Bezier curve. Savin et al. [9] used the pattern recognition algorithm of Andrew W with some modifications to determine whether “head-and-shoulders” (HS) price patterns have predictive power for future stock returns. Zapranis and Tsinaslanidis [10] represented the ideal circular base as an isosceles triangle formed by three key points and then used the two congruent circles on the outer ring of this triangle to control price fluctuations. The final two-dimensional coordinates of price and time were used for automatic pattern recognition.Template-based pattern recognition is the network lattice method represented by Leigh et al. [11]. Leigh represented the flag as a weight-based grid and used it as a template to compare it with a time series window and determined whether the sequence matches the template based on the price percentile and a genetic algorithm. Wang and Chan [12] used the flag template method, which represents the top and bottom of the dome as a network lattice based on price fluctuations, as a template for identifying and examining buy signals. Royo et al. [13] defined flags from a new perspective and modified the grid weights.Directly using an existing stock pattern curve as a template before using either of these methods will likely lead to overfitting and will be inefficient, also the accuracy and reliability of the analysis will be compromised [14, 15]. Therefore, when studying complex time series, scholars will first perform some preprocessing on the raw data. In order to improve the mining efficiency, scholars have proposed an approximate representation of the time series, which is a compressed representation of the time series, by retaining the main information of the time series and ignoring the tiny details. The current time series approximation methods have included discrete Fourier transform [16], discrete wavelet transform [17], piecewise aggregate approximation [18], symbolic aggregate approximation [19], and singular value decomposition [20]. Pattern recognition belongs to a type of time series mining. In conjunction with the above overview of price pattern recognition, time preprocessing before pattern recognition can be broadly divided into two categories. (1) Nonlinear curve description. Commonly used methods are nonparametric kernel regression methods and Gaussian kernel estimation methods. Dong used a Gaussian kernel estimation method to smooth the estimation process on the raw data [21]. (2) Approximate representation based on segmentation. For example, Keogh et al. [22] proposed a bottom-up approach for segmenting time series and extracting their features. Gong reduced the complexity of time series using perceived important points proposed by Chung [23, 24].Wavelet transform has been an important way to preprocess time series, especially for nonstationary, nonlinear, and heteroskedastic signals [25]. Therefore, many scholars have used wavelet transforms to preprocess the raw data before building stock prediction models. The most used predictive model is the neural network algorithm [26–28]. The abovementioned prediction models show that the prediction accuracy of the original data is greatly improved after wavelet transform processing. In addition to neural network algorithms, there are other algorithms such as SVMs that are combined with wavelet transforms. The experimental results also showed that the hybrid model incorporating the wavelet transform is better than the prediction using a single algorithm [29–32]. DTW is a kind of elastic measurement for calculating time series similarity [33–35]. As DTW can extract morphological features of time series and handle time transformations and distortions, it can be used for pattern representation of stock time series. For example, Han developed a portfolio optimization model with strict constraints to obtain a pattern representation of stock time series based on DTW [35].According to the abovementioned research, at present for the price pattern automatic identification has achieved some results, there is still room for improvement: (1) in the study of automatic price pattern recognition, the time series preprocessing method is relatively homogeneous. Due to the needs of pattern recognition, the automatic identification of price patterns uses a single method. (2) The extraction of important points in the perception of important points method is highly subjective. (3) In identifying patterns, most studies have used the sliding window method, which allows for the existence of other points between the extreme points of the pattern, but does not consider the relationship between the extreme points of the pattern and other points, which is prone to misjudgment. For this reason, the study will combine the advantages of DTW and wavelet transform to establish a time series expression based on holistic graphs.


## 3. Theoretical Foundation

### 3.1. Wavelet Denoising Principle

Set a noise-containing sequenceF(t), which can be represented by the following model:(1)Ft=st+et.In the above equation,s(t) is the true sequence and e(t) is the white noise.Suppose that the noisee(t) is white noise with zero mean and variance σ2. Set the wavelet function to φa, Wea,t to be the wavelet transform of e(t), which is obtained from the continuous wavelet transform theory.(2)Wea,t=∫−∞∞euφat−udu.It is known from the hypothesis that(3)Eeuev=σ2δu,v.Substituting the formula in (2) into (3) gives the following reasoning:(4)Ewea,t2=σ2φ2a.The purpose of sequencef(t) denoising is to suppress the noisy part of the sequence e(t) to obtain the true sequence s(t); it follows from the abovementioned reasoning that the noise wavelet transformed wea,t is inversely proportional to the scale a. Therefore, it is possible to denoise the sequence using the different characteristics exhibited at each scale after the noise and sequence wavelet transform.


### 3.2. Wavelet Denoising Principle

The method was proposed by Donoho and Johnstone [36]. The basic principle is that the orthogonal wavelet decomposition has the ability of time-frequency local decomposition. When performing sequence processing, the wavelet component exhibits a large amplitude, in contrast to the uniform variation of the noise in the high-frequency part. The larger amplitude wavelet coefficients are overwhelmingly useful sequences; the smaller amplitude coefficients are generally noise after wavelet decomposition. That is, it can be assumed that the wavelet transformation coefficient of the useful sequence is greater than the wavelet transformation coefficient of the noise. Then, a suitable threshold is selected according to certain rules, wavelet coefficients greater than the threshold are retained while wavelet coefficients smaller than the threshold are processed accordingly, and finally the useful sequence is reduced according to the processed wavelet coefficients. The key to the threshold denoising method is to pick an appropriate threshold value. According to the relationship between wavelet coefficients and the threshold, the threshold function can be divided into hard threshold denoising, soft threshold denoising, and zero-threshold denoising method. In addition, there are other threshold denoising methods, such as suppressing detail denoising (suppressing certain layers to obtain detail coefficients based on actual denoising requirements). These four threshold denoising methods are compared in detail in the experimental section of this study.Suppose that the time series equation is as follows:(5)Ft=st+et.The result of doing the wavelet transform on both sides of the above equation simultaneously is as follows:(6)Wea,t=∫−∞∞euφat−udu.The wavelet transform of the actual sequence is equal to the sum of multiple wavelet transforms. The orthogonal wavelet transform of the original time series is able to remove thef(t) correlation to the greatest extent possible, retaining a few and relatively large amplitudes on the wavelet coefficients to a large extent, while the noise e(t) will be distributed across all time axes at all scales with relatively small amplitude after wavelet transformations. According to the abovementioned principle, the wavelet coefficients of the noise are minimized at each scale of the wavelet transform, and then the processed wavelet coefficients are used for sequence reconstruction to suppress the noise. The specific steps of the abovementioned method are shown in Figure 2.Figure 2
Basic steps of threshold denoising.


### 3.3. MACD Fundamentals

In order to get better experimental results, this study uses the MACD algorithm to process the raw data to form a new MACD curve and then uses the DTW algorithm to compare the distance between the MACD curves. The MACD algorithm is a trend-following momentum indicator that shows the relationship between two moving averages of a security’s price. This preprocessing method can be used to eliminate price disturbances and enhance the trend of the original price graphs, ultimately resulting in a better fit. The mathematical principle of the MACD algorithm is as follows:(7)EMAt=n−1n+1×EMAt−1+2n+1×price.EMA(t) is a moving average of t days, EMA(t − 1) is yesterday’s moving average, n is the period, and price is the original price series. t-day maps t different weights. From the above equation, the weights increase over time, so EMA(t) is a better reflection of the future trend of the price chart line. DIF (the difference between the short-term average price of a stock and its long-term average price) is defined as follows:(8)DIF=EMAt1−EMAt2.EMAt1 is a fast-moving average and EMAt2 is a slow-moving average; the difference between the two can reflect the future upward or downward trend of the price chart line. DEA (difference exponential average) is defined as follows:(9)DEA=2n+1×DIF+n−1n+1×DEA′.A weighted average was applied to the abovementioned DIFs, with the same weight settings as EMA. In the above equation,DEA′ is the previous period DEA. HIST (histogram) is defined as follows:(10)HIST=2×DIF−DEA.From the above formula, the HIST can better reflect the rise and fall of the original price series and eliminate the influence of price on the DTW algorithm. In this study, the closing price of 000009.sz from January 1, 2019, to July 24, 2020, is selected and the relevant graph line is plotted as shown in Figure3.Figure 3
Indicator preprocessing.It is obvious from the abovementioned chart, the original price sequence chart line (signal line in the chart) increase is basically consistent with the MACD column in the chart, so the MACD column line can reflect the increase of the original chart line. In addition to the above advantages, MACD removes price distractions when fitting graphs. Therefore, this study converts the standard graph line to an MACD line as shown in Figure4.Figure 4
Target price series graphs are processed by MACD algorithm.


### 3.4. The Process of Rolling Window with DTW

The article selected the raw price series withm data points as the target graphic. The remaining stock price series with n data points is then divided chronologically into subsegments with m data points. A total of (n/m) subsequence fragments can be obtained, and the subsequence fragments are labeled using Window(i). The window(i) with the smallest distance from the target graph is obtained using the DTW algorithm. The rolling window process with DTW is shown in Figure 5.Figure 5
The process of rolling window with DTW.


## 3.1. Wavelet Denoising Principle

Set a noise-containing sequenceF(t), which can be represented by the following model:(1)Ft=st+et.In the above equation,s(t) is the true sequence and e(t) is the white noise.Suppose that the noisee(t) is white noise with zero mean and variance σ2. Set the wavelet function to φa, Wea,t to be the wavelet transform of e(t), which is obtained from the continuous wavelet transform theory.(2)Wea,t=∫−∞∞euφat−udu.It is known from the hypothesis that(3)Eeuev=σ2δu,v.Substituting the formula in (2) into (3) gives the following reasoning:(4)Ewea,t2=σ2φ2a.The purpose of sequencef(t) denoising is to suppress the noisy part of the sequence e(t) to obtain the true sequence s(t); it follows from the abovementioned reasoning that the noise wavelet transformed wea,t is inversely proportional to the scale a. Therefore, it is possible to denoise the sequence using the different characteristics exhibited at each scale after the noise and sequence wavelet transform.


## 3.2. Wavelet Denoising Principle

The method was proposed by Donoho and Johnstone [36]. The basic principle is that the orthogonal wavelet decomposition has the ability of time-frequency local decomposition. When performing sequence processing, the wavelet component exhibits a large amplitude, in contrast to the uniform variation of the noise in the high-frequency part. The larger amplitude wavelet coefficients are overwhelmingly useful sequences; the smaller amplitude coefficients are generally noise after wavelet decomposition. That is, it can be assumed that the wavelet transformation coefficient of the useful sequence is greater than the wavelet transformation coefficient of the noise. Then, a suitable threshold is selected according to certain rules, wavelet coefficients greater than the threshold are retained while wavelet coefficients smaller than the threshold are processed accordingly, and finally the useful sequence is reduced according to the processed wavelet coefficients. The key to the threshold denoising method is to pick an appropriate threshold value. According to the relationship between wavelet coefficients and the threshold, the threshold function can be divided into hard threshold denoising, soft threshold denoising, and zero-threshold denoising method. In addition, there are other threshold denoising methods, such as suppressing detail denoising (suppressing certain layers to obtain detail coefficients based on actual denoising requirements). These four threshold denoising methods are compared in detail in the experimental section of this study.Suppose that the time series equation is as follows:(5)Ft=st+et.The result of doing the wavelet transform on both sides of the above equation simultaneously is as follows:(6)Wea,t=∫−∞∞euφat−udu.The wavelet transform of the actual sequence is equal to the sum of multiple wavelet transforms. The orthogonal wavelet transform of the original time series is able to remove thef(t) correlation to the greatest extent possible, retaining a few and relatively large amplitudes on the wavelet coefficients to a large extent, while the noise e(t) will be distributed across all time axes at all scales with relatively small amplitude after wavelet transformations. According to the abovementioned principle, the wavelet coefficients of the noise are minimized at each scale of the wavelet transform, and then the processed wavelet coefficients are used for sequence reconstruction to suppress the noise. The specific steps of the abovementioned method are shown in Figure 2.Figure 2
Basic steps of threshold denoising.


## 3.3. MACD Fundamentals

In order to get better experimental results, this study uses the MACD algorithm to process the raw data to form a new MACD curve and then uses the DTW algorithm to compare the distance between the MACD curves. The MACD algorithm is a trend-following momentum indicator that shows the relationship between two moving averages of a security’s price. This preprocessing method can be used to eliminate price disturbances and enhance the trend of the original price graphs, ultimately resulting in a better fit. The mathematical principle of the MACD algorithm is as follows:(7)EMAt=n−1n+1×EMAt−1+2n+1×price.EMA(t) is a moving average of t days, EMA(t − 1) is yesterday’s moving average, n is the period, and price is the original price series. t-day maps t different weights. From the above equation, the weights increase over time, so EMA(t) is a better reflection of the future trend of the price chart line. DIF (the difference between the short-term average price of a stock and its long-term average price) is defined as follows:(8)DIF=EMAt1−EMAt2.EMAt1 is a fast-moving average and EMAt2 is a slow-moving average; the difference between the two can reflect the future upward or downward trend of the price chart line. DEA (difference exponential average) is defined as follows:(9)DEA=2n+1×DIF+n−1n+1×DEA′.A weighted average was applied to the abovementioned DIFs, with the same weight settings as EMA. In the above equation,DEA′ is the previous period DEA. HIST (histogram) is defined as follows:(10)HIST=2×DIF−DEA.From the above formula, the HIST can better reflect the rise and fall of the original price series and eliminate the influence of price on the DTW algorithm. In this study, the closing price of 000009.sz from January 1, 2019, to July 24, 2020, is selected and the relevant graph line is plotted as shown in Figure3.Figure 3
Indicator preprocessing.It is obvious from the abovementioned chart, the original price sequence chart line (signal line in the chart) increase is basically consistent with the MACD column in the chart, so the MACD column line can reflect the increase of the original chart line. In addition to the above advantages, MACD removes price distractions when fitting graphs. Therefore, this study converts the standard graph line to an MACD line as shown in Figure4.Figure 4
Target price series graphs are processed by MACD algorithm.


## 3.4. The Process of Rolling Window with DTW

The article selected the raw price series withm data points as the target graphic. The remaining stock price series with n data points is then divided chronologically into subsegments with m data points. A total of (n/m) subsequence fragments can be obtained, and the subsequence fragments are labeled using Window(i). The window(i) with the smallest distance from the target graph is obtained using the DTW algorithm. The rolling window process with DTW is shown in Figure 5.Figure 5
The process of rolling window with DTW.


## 4. Financial Time Series Wavelet Denoising Analysis and DTW Improvements

The time series can be smoothed using the abovementioned theory, but there is still the problem of parameter determination. According to the flow of financial time series processing by Lan and Ma [37], there are three important parameters that affect the final denoising effect: the choice of the wavelet base function, the number of decomposition layers, and the choice of the threshold function.


### 4.1. Characteristics of Financial Time Series

Stock price data, which is typical of financial data, commonly includes stock trading data and yield data calculated from trading data. However, both types of data exhibit an unsteady nature and the data are more volatile. In particular, financial time series are densely populated with singularities, so it is generally difficult to smooth out the financial time series. In addition to large fluctuations, there are small fluctuations in the data, which are highly stochastic and spread throughout the time window. Generally, large fluctuations contain more useful information and are therefore generally retained as useful information; a large number of small fluctuations are generally chosen to be removed due to their randomness and lack of value for analysis and prediction.


### 4.2. Selection of the Wavelet Function

At present, wavelet functions are more diverse and have different properties, which are suitable for different scenarios. The purpose of this study is to identify the graph of a stock in a given time window. Different wavelet functions have different denoising effects, so it is important to choose the right wavelet function. The following analysis of wavelet function characteristics is closely related to denoising.Because there is often a correlation between wavelet function properties (it has been shown that there is no symmetric tightly-branched orthogonal wavelet base, except for the Harr wavelet), it is impossible to find a wavelet function that has all the optimal properties. The purpose of this study is to smooth the financial time series and different wavelet functions correspond to different denoising effects; it is crucial to choose the appropriate wavelet function. The following analysis focuses on a number of features that are closely related to the noise removal effect.(1)
Orthogonality: the wavelet function with orthogonality can maintain the noncorrelation between the wavelet coefficients to improve the denoising performance of the wavelet function. More importantly, orthogonality is necessary to achieve fast wavelet transforms, and thus orthogonality is necessary to achieve wavelet denoising.(2)
Compact support: it means that the wavelet function is nonzero in the effective interval, so the smaller the support width of the wavelet or scale function, the better the local resolution of the wavelet and the finer the denoising effect.(3)
Vanishing moment: since there are a certain number of singularities in the financial time series, it is necessary to apply a certain vanishing moment so that as many wavelet coefficients as possible are zero or generate as few nonzero wavelet coefficients as possible to increase the denoising effect. The larger the vanishing moment, the more wavelet coefficients are zero, so there is a positive correlation between the vanishing moment and the width of the support. That is why there is a compromise between the vanishing moment and the width of the support. The vanishing moment is proportional to the number of singularities present in the sequence, while the stock price sequence has more singularities, so the vanishing moment can be increased accordingly.(4)
Symmetry: the wavelet function with symmetry will reduce the error after wavelet transformation, so this property is conducive to the recovery and reconstruction of the sequence after noise removal. However, this property has less influence on the noise removal effect than other properties. This study finds a number of commonly used wavelet functions to examine, the properties of which are shown in Table1.Table 1
Characteristics of different types of wavelet functions.
Compact supportSupport widthSymmetryOrthogonalityVanishing momentHarrYes1YesYes1MexicanNoinfYesNo1MeyerNoinfYesYesinfDbNYes2N − 1FarYesNSymNYes2N − 1NearYesNCoifNYes6N − 1NearYes2NSinceNoinfYesYesNoBionNr.NdYes2Nr + 1, 2Nd + 1.YesNoNrFor the aforementioned wavelet function, SymN is the best choice for the following reasons: (1) it is orthogonal, enabling wavelet denoising. (2) There are more “spikes” in the stock price sequence, so it is necessary to ensure that the wavelet has a vanishing moment. SymN is the best choice because of the compromise between the vanishing moment and the width of the support. (3) SymN has better symmetry than DbN as well as Conif, which can reduce errors during sequence reconstruction.


### 4.3. Determination of the Number of Decomposition Layers

#### 4.3.1. Wavelet Noise Reduction Evaluation Index

Since wavelet denoising is mostly used inside engineering, the requirements for denoising in engineering are different from the requirements for denoising in finance. In order to measure the effect of sequence denoising, the article defines two variables, information extraction rate (IER) and information extraction error (IEE), drawing on the wavelet threshold method selected by Liu [38] to measure the effect of sequence denoising, considering f as the original time series, f^ as the denoised time series, and IEE and IER defined as follows:(11)IER=f^2f2,IEE=f^−f2.From the above equation, the information extraction rate is positively related to the similarity of the denoised sequence to the original sequence. The larger the information extraction error, the smoother the denoised sequence is compared to the original sequence. The abovementioned two indicators are negatively correlated, and if the information extraction rate is combined with the information extraction error to form a composite evaluation indicator, the process of increasing the number of decomposition layers will inevitably result in a maximum value. The graphical interpretation of this maximum value is the optimal ratio of the sequence in retaining detail information to its approximation information, which corresponds to the number of decomposition layers. The above demonstrates the feasibility of using a combination of information extraction rates and information extraction errors to form new composite evaluation indicators through theoretical analysis and also based on the numerical characteristics of the indicator changes.


#### 4.3.2. Establishment of Composite Evaluation Indicators

Different evaluation indicators have different scales and units, and in order to eliminate the incommensurability this brings, the study treats each evaluation indicator as nonquantitative. In order to preserve the difference between the information extraction rate and the information extraction error, this study chooses to use the averaging method in the no-quantization method. If there arem indicators in the composite evaluation, each with n units, the indicators are xij (i = 1, 2, ..., n; j = 1, 2, ..., m), which represents the jth indicator value of the i-th unit, and yij represents the jth index value of the i-th unit after dimensionless homogenization. The formula is as follows:(12)yij=xijxj¯.The mean value of each indicator after the mean method is 1 and its variance is as follows:(13)varyj=Eyj−12=Exj−xj¯2xj2¯=varxjxj2¯=σjxj¯2.The variance of each indicator after averaging is the square of the coefficient of variationxj¯ of each indicator, which retains information on the degree of variation of each indicator. After averaging, the indicators become the same order of magnitude, and if they are to be combined linearly, weights need to be assigned to each indicator. Since the averaging method preserves the variability among the indicators, then weighting requires the elimination of the variability among the indicators, for which entropy weighting is chosen in this study. Entropy is an ideal metric for multiobjective decision-making and evaluation. It relies on the amount of decision-making information to improve the accuracy of decision-making, so it can objectively reflect the implicit information of the indicator data, improve the resolution of the indicators, avoid the selection bias caused by small differences in indicators, and reflect a full range of indicator information. There is an inverse relationship between the entropy of an indicator and its weight. According to the concept of entropy, entropy Q is defined as follows:(14)Qj=1−Hjn−∑j=1nHj.In the above equation,Hj is defined as follows:(15)Hj=−∑i=1mfijlnfijlnm.In the above equation,fij is defined as follows:(16)fij=1+yij∑i=1m1+yij.The above equation satisfies∑j=1nQj=1.The use of entropy weighting allows for the combination of multiple metrics to determine weights, both to account for associations between multiple samples and to weaken the effect of outliers. The choice of entropy weighting and a measureless mean, i.e., a combination of differential and nondifferential, would theoretically be a good one.According to the abovementioned method, wavelet decomposition is first performed on the original time series to calculate theIERj and IEEj for different decomposition scales J. Then, the dimensionless averaging process is performed to get the IERj˜ and IEEj˜. Finally, the information extraction rate weights QIER and the information extraction error weights QIEE are calculated from the entropy weights. Then, the formula for the value of composite indicator S is as follows:(17)Sj=IERj×QIER+IEEj×QIEE,j=1,2.


### 4.4. DTW Algorithm Improvement

The main objective of the study is to fit the graphs formed by stock prices in a particular time window, and the degree of similarity of the fit is achieved by the DTW algorithm. In order to improve the fit to the target graph, this study makes improvements to the traditional DTW algorithm. The article randomly constructs three sequences of length 20, wheres2 is derived from s1 translation and s3 has no relationship with either of the above. The three sequences are shown in Figure 6.Figure 6
s1, s2, and s3 graphs.The distance betweens1 and s2 is 5 and the distance between s1 and s3 is 4.12, calculated using the traditional DTW algorithm. Thus, it can be seen that the traditional DTW algorithm is not very effective for the distance of the postshift sequence. This study proposes an improved DTW algorithm.(1)
Determine the largest common substring of the two sequences seq1 and seq2, the length of which is given bya.(2)
seq1 and seq2 are numerical sequences that set a maximum standard deviation of the offset tolerance in finding the longest public sequence. In other words, two values within this standard deviation are considered to be part of the public sequence as well.(3)
Define the attenuation factorα as follows:(18)α=1−a×alensql1×lensql2.The length of the common subsequence of both sequences is negatively correlated with its attenuation coefficient, thus enabling improvement of the algorithm. The distance betweens1 and s2 is 0.982 and the distance between s1 and s3 is 2.512, as recalculated by the improved algorithm. It follows that the improved DTW algorithm effectively reduces the distance between the periodic sequences after translation.


## 4.1. Characteristics of Financial Time Series

Stock price data, which is typical of financial data, commonly includes stock trading data and yield data calculated from trading data. However, both types of data exhibit an unsteady nature and the data are more volatile. In particular, financial time series are densely populated with singularities, so it is generally difficult to smooth out the financial time series. In addition to large fluctuations, there are small fluctuations in the data, which are highly stochastic and spread throughout the time window. Generally, large fluctuations contain more useful information and are therefore generally retained as useful information; a large number of small fluctuations are generally chosen to be removed due to their randomness and lack of value for analysis and prediction.


## 4.2. Selection of the Wavelet Function

At present, wavelet functions are more diverse and have different properties, which are suitable for different scenarios. The purpose of this study is to identify the graph of a stock in a given time window. Different wavelet functions have different denoising effects, so it is important to choose the right wavelet function. The following analysis of wavelet function characteristics is closely related to denoising.Because there is often a correlation between wavelet function properties (it has been shown that there is no symmetric tightly-branched orthogonal wavelet base, except for the Harr wavelet), it is impossible to find a wavelet function that has all the optimal properties. The purpose of this study is to smooth the financial time series and different wavelet functions correspond to different denoising effects; it is crucial to choose the appropriate wavelet function. The following analysis focuses on a number of features that are closely related to the noise removal effect.(1)
Orthogonality: the wavelet function with orthogonality can maintain the noncorrelation between the wavelet coefficients to improve the denoising performance of the wavelet function. More importantly, orthogonality is necessary to achieve fast wavelet transforms, and thus orthogonality is necessary to achieve wavelet denoising.(2)
Compact support: it means that the wavelet function is nonzero in the effective interval, so the smaller the support width of the wavelet or scale function, the better the local resolution of the wavelet and the finer the denoising effect.(3)
Vanishing moment: since there are a certain number of singularities in the financial time series, it is necessary to apply a certain vanishing moment so that as many wavelet coefficients as possible are zero or generate as few nonzero wavelet coefficients as possible to increase the denoising effect. The larger the vanishing moment, the more wavelet coefficients are zero, so there is a positive correlation between the vanishing moment and the width of the support. That is why there is a compromise between the vanishing moment and the width of the support. The vanishing moment is proportional to the number of singularities present in the sequence, while the stock price sequence has more singularities, so the vanishing moment can be increased accordingly.(4)
Symmetry: the wavelet function with symmetry will reduce the error after wavelet transformation, so this property is conducive to the recovery and reconstruction of the sequence after noise removal. However, this property has less influence on the noise removal effect than other properties. This study finds a number of commonly used wavelet functions to examine, the properties of which are shown in Table1.Table 1
Characteristics of different types of wavelet functions.
Compact supportSupport widthSymmetryOrthogonalityVanishing momentHarrYes1YesYes1MexicanNoinfYesNo1MeyerNoinfYesYesinfDbNYes2N − 1FarYesNSymNYes2N − 1NearYesNCoifNYes6N − 1NearYes2NSinceNoinfYesYesNoBionNr.NdYes2Nr + 1, 2Nd + 1.YesNoNrFor the aforementioned wavelet function, SymN is the best choice for the following reasons: (1) it is orthogonal, enabling wavelet denoising. (2) There are more “spikes” in the stock price sequence, so it is necessary to ensure that the wavelet has a vanishing moment. SymN is the best choice because of the compromise between the vanishing moment and the width of the support. (3) SymN has better symmetry than DbN as well as Conif, which can reduce errors during sequence reconstruction.


## 4.3. Determination of the Number of Decomposition Layers

### 4.3.1. Wavelet Noise Reduction Evaluation Index

Since wavelet denoising is mostly used inside engineering, the requirements for denoising in engineering are different from the requirements for denoising in finance. In order to measure the effect of sequence denoising, the article defines two variables, information extraction rate (IER) and information extraction error (IEE), drawing on the wavelet threshold method selected by Liu [38] to measure the effect of sequence denoising, considering f as the original time series, f^ as the denoised time series, and IEE and IER defined as follows:(11)IER=f^2f2,IEE=f^−f2.From the above equation, the information extraction rate is positively related to the similarity of the denoised sequence to the original sequence. The larger the information extraction error, the smoother the denoised sequence is compared to the original sequence. The abovementioned two indicators are negatively correlated, and if the information extraction rate is combined with the information extraction error to form a composite evaluation indicator, the process of increasing the number of decomposition layers will inevitably result in a maximum value. The graphical interpretation of this maximum value is the optimal ratio of the sequence in retaining detail information to its approximation information, which corresponds to the number of decomposition layers. The above demonstrates the feasibility of using a combination of information extraction rates and information extraction errors to form new composite evaluation indicators through theoretical analysis and also based on the numerical characteristics of the indicator changes.


### 4.3.2. Establishment of Composite Evaluation Indicators

Different evaluation indicators have different scales and units, and in order to eliminate the incommensurability this brings, the study treats each evaluation indicator as nonquantitative. In order to preserve the difference between the information extraction rate and the information extraction error, this study chooses to use the averaging method in the no-quantization method. If there arem indicators in the composite evaluation, each with n units, the indicators are xij (i = 1, 2, ..., n; j = 1, 2, ..., m), which represents the jth indicator value of the i-th unit, and yij represents the jth index value of the i-th unit after dimensionless homogenization. The formula is as follows:(12)yij=xijxj¯.The mean value of each indicator after the mean method is 1 and its variance is as follows:(13)varyj=Eyj−12=Exj−xj¯2xj2¯=varxjxj2¯=σjxj¯2.The variance of each indicator after averaging is the square of the coefficient of variationxj¯ of each indicator, which retains information on the degree of variation of each indicator. After averaging, the indicators become the same order of magnitude, and if they are to be combined linearly, weights need to be assigned to each indicator. Since the averaging method preserves the variability among the indicators, then weighting requires the elimination of the variability among the indicators, for which entropy weighting is chosen in this study. Entropy is an ideal metric for multiobjective decision-making and evaluation. It relies on the amount of decision-making information to improve the accuracy of decision-making, so it can objectively reflect the implicit information of the indicator data, improve the resolution of the indicators, avoid the selection bias caused by small differences in indicators, and reflect a full range of indicator information. There is an inverse relationship between the entropy of an indicator and its weight. According to the concept of entropy, entropy Q is defined as follows:(14)Qj=1−Hjn−∑j=1nHj.In the above equation,Hj is defined as follows:(15)Hj=−∑i=1mfijlnfijlnm.In the above equation,fij is defined as follows:(16)fij=1+yij∑i=1m1+yij.The above equation satisfies∑j=1nQj=1.The use of entropy weighting allows for the combination of multiple metrics to determine weights, both to account for associations between multiple samples and to weaken the effect of outliers. The choice of entropy weighting and a measureless mean, i.e., a combination of differential and nondifferential, would theoretically be a good one.According to the abovementioned method, wavelet decomposition is first performed on the original time series to calculate theIERj and IEEj for different decomposition scales J. Then, the dimensionless averaging process is performed to get the IERj˜ and IEEj˜. Finally, the information extraction rate weights QIER and the information extraction error weights QIEE are calculated from the entropy weights. Then, the formula for the value of composite indicator S is as follows:(17)Sj=IERj×QIER+IEEj×QIEE,j=1,2.


## 4.3.1. Wavelet Noise Reduction Evaluation Index

Since wavelet denoising is mostly used inside engineering, the requirements for denoising in engineering are different from the requirements for denoising in finance. In order to measure the effect of sequence denoising, the article defines two variables, information extraction rate (IER) and information extraction error (IEE), drawing on the wavelet threshold method selected by Liu [38] to measure the effect of sequence denoising, considering f as the original time series, f^ as the denoised time series, and IEE and IER defined as follows:(11)IER=f^2f2,IEE=f^−f2.From the above equation, the information extraction rate is positively related to the similarity of the denoised sequence to the original sequence. The larger the information extraction error, the smoother the denoised sequence is compared to the original sequence. The abovementioned two indicators are negatively correlated, and if the information extraction rate is combined with the information extraction error to form a composite evaluation indicator, the process of increasing the number of decomposition layers will inevitably result in a maximum value. The graphical interpretation of this maximum value is the optimal ratio of the sequence in retaining detail information to its approximation information, which corresponds to the number of decomposition layers. The above demonstrates the feasibility of using a combination of information extraction rates and information extraction errors to form new composite evaluation indicators through theoretical analysis and also based on the numerical characteristics of the indicator changes.


## 4.3.2. Establishment of Composite Evaluation Indicators

Different evaluation indicators have different scales and units, and in order to eliminate the incommensurability this brings, the study treats each evaluation indicator as nonquantitative. In order to preserve the difference between the information extraction rate and the information extraction error, this study chooses to use the averaging method in the no-quantization method. If there arem indicators in the composite evaluation, each with n units, the indicators are xij (i = 1, 2, ..., n; j = 1, 2, ..., m), which represents the jth indicator value of the i-th unit, and yij represents the jth index value of the i-th unit after dimensionless homogenization. The formula is as follows:(12)yij=xijxj¯.The mean value of each indicator after the mean method is 1 and its variance is as follows:(13)varyj=Eyj−12=Exj−xj¯2xj2¯=varxjxj2¯=σjxj¯2.The variance of each indicator after averaging is the square of the coefficient of variationxj¯ of each indicator, which retains information on the degree of variation of each indicator. After averaging, the indicators become the same order of magnitude, and if they are to be combined linearly, weights need to be assigned to each indicator. Since the averaging method preserves the variability among the indicators, then weighting requires the elimination of the variability among the indicators, for which entropy weighting is chosen in this study. Entropy is an ideal metric for multiobjective decision-making and evaluation. It relies on the amount of decision-making information to improve the accuracy of decision-making, so it can objectively reflect the implicit information of the indicator data, improve the resolution of the indicators, avoid the selection bias caused by small differences in indicators, and reflect a full range of indicator information. There is an inverse relationship between the entropy of an indicator and its weight. According to the concept of entropy, entropy Q is defined as follows:(14)Qj=1−Hjn−∑j=1nHj.In the above equation,Hj is defined as follows:(15)Hj=−∑i=1mfijlnfijlnm.In the above equation,fij is defined as follows:(16)fij=1+yij∑i=1m1+yij.The above equation satisfies∑j=1nQj=1.The use of entropy weighting allows for the combination of multiple metrics to determine weights, both to account for associations between multiple samples and to weaken the effect of outliers. The choice of entropy weighting and a measureless mean, i.e., a combination of differential and nondifferential, would theoretically be a good one.According to the abovementioned method, wavelet decomposition is first performed on the original time series to calculate theIERj and IEEj for different decomposition scales J. Then, the dimensionless averaging process is performed to get the IERj˜ and IEEj˜. Finally, the information extraction rate weights QIER and the information extraction error weights QIEE are calculated from the entropy weights. Then, the formula for the value of composite indicator S is as follows:(17)Sj=IERj×QIER+IEEj×QIEE,j=1,2.


## 4.4. DTW Algorithm Improvement

The main objective of the study is to fit the graphs formed by stock prices in a particular time window, and the degree of similarity of the fit is achieved by the DTW algorithm. In order to improve the fit to the target graph, this study makes improvements to the traditional DTW algorithm. The article randomly constructs three sequences of length 20, wheres2 is derived from s1 translation and s3 has no relationship with either of the above. The three sequences are shown in Figure 6.Figure 6
s1, s2, and s3 graphs.The distance betweens1 and s2 is 5 and the distance between s1 and s3 is 4.12, calculated using the traditional DTW algorithm. Thus, it can be seen that the traditional DTW algorithm is not very effective for the distance of the postshift sequence. This study proposes an improved DTW algorithm.(1)
Determine the largest common substring of the two sequences seq1 and seq2, the length of which is given bya.(2)
seq1 and seq2 are numerical sequences that set a maximum standard deviation of the offset tolerance in finding the longest public sequence. In other words, two values within this standard deviation are considered to be part of the public sequence as well.(3)
Define the attenuation factorα as follows:(18)α=1−a×alensql1×lensql2.The length of the common subsequence of both sequences is negatively correlated with its attenuation coefficient, thus enabling improvement of the algorithm. The distance betweens1 and s2 is 0.982 and the distance between s1 and s3 is 2.512, as recalculated by the improved algorithm. It follows that the improved DTW algorithm effectively reduces the distance between the periodic sequences after translation.


## 5. Experimental Demonstration and Analysis

### 5.1. Methodology

This study arbitrarily selects five stocks in the A-share market as the target graphics and determines the wavelet denoising parameters by the abovementioned entropy weighting method. An improved DTW algorithm is then used to find other stock time windows from the stock market that are the least distant from it. In order to achieve better experimental results, this study introduces MACD algorithm to convert the original price series into “MACD graph line” on the basis of wavelet denoising. Then, the distance between the calculated original price series is transformed into the distance between “MACD lines,” which improves the accuracy of the result.


### 5.2. Source of Experimental Data

All stock data in this study are from the Tushare website (https://tushare.pro) and include trading date, opening price, high price, low price, closing price before compounding, and uncompounded upside and downside. Stock data are daily data and the time frame studied is from the listing of each stock to July 24, 2020.


### 5.3. Wavelet Denoising Preprocessing

#### 5.3.1. Stock Price Sequence Wavelet Denoising Process

(1)
Choose the appropriate wavelet base function and decomposition layer N for the time seriesf.(2)
Threshold quantification of the decomposed high-frequency coefficients is performed, and for each layer from 1 toN, the appropriate threshold and the appropriate threshold function are selected to obtain the estimated wavelet coefficients.(3)
The original time series is reconstructed (performing wavelet inversion) based on the low-frequency coefficients (scale coefficients) of theNth layer after wavelet decomposition and the high-frequency coefficients (wavelet coefficients) of each layer after threshold quantization. The wavelet reconstruction is then performed using the reconstruction algorithm to obtain the denoised postdecomposition sequence.Because the daily stock closing price sequence is essentially a one-dimensional discrete sequence, this focuses on the wavelet decomposition of the one-dimensional discrete sequence. Experiments show that when a white noise sequence is wavelet decomposed, it can be found that the decay rate of the high-frequency coefficient after wavelet decomposition is proportional to the number of layers of wavelet decomposition, and the variance of the same coefficient is inversely proportional to the number of layers of wavelet decomposition. Thus, for the general case, there is a positive relationship between the number of decomposed layers and the smoothness of the denoised sequence. Therefore, in this study, we have to find the most suitable wavelet denoising parameters based on the characteristics of the stock price sequence.


#### 5.3.2. Wavelet Denoising Parameter Investigation

This study selects five stocks in the China A-share market (ticker symbols: 300327.SZ, 300332.SZ, 300336.SZ, 601567.SH, and 601616.SH) as target patterns I–V, respectively. The results of denoising the abovementioned five target graphics using hard threshold, soft threshold, zero threshold, and inhibited detail factor threshold functions combined with the wavelet decomposition layer number method are shown in Tables2–5.Table 2
Suppression of detail factor.
Layers1234567Figure1IER1.0002701.0001981.0000350.9999530.9994750.9985580.998343IEE0.2124690.2899140.3270150.3551670.3709090.3746980.378330s12.5958125.6957149.6128354.7275451.9465426.1783414.00664Figure2IER1.0000291.0001670.9999440.9997750.9992810.9982380.997986IEE0.2361780.3171510.3605680.3772660.3895810.3925510.396433s15.3551121.5418747.1154356.4567950.9883724.7965315.51747Figure3IER0.9999701.0001100.9999840.9997350.9995070.9987200.998531IEE0.2834560.3520920.3925850.4109390.4200730.4247260.427760s15.0958922.7585650.7358656.5978748.4203023.9260118.50077Figure4IER1.0000531.0001361.0000070.9998980.9996180.9996040.999455IEE0.2024960.2830910.3282150.3506920.3638710.3764800.375991s22.4783946.7272148.4098959.9272947.4758026.8606121.57036Figure5IER1.0000051.0002871.0000150.9997490.9990710.9974420.997055IEE0.1397540.2023700.2462940.2717350.2877970.2917350.297407s19.9081528.8595652.1260452.4220847.6068622.5300116.47265Table 3
Hard threshold.
Layers1234567Figure1IER1.0000531.0001361.0000070.9999980.9998180.9998040.999855IEE0.2024960.2830910.4282150.4506920.4638710.4764800.485991s13.5379122.8347139.6128344.9564838.7136128.7243516.53761Figure2IER1.0000341.0000470.9999310.9999230.9997140.9997380.998976IEE0.2284160.3054990.3437740.3897390.3954590.3970320.423563s14.1950421.5970137.9026246.3777440.6392424.0793615.33218Figure3IER0.9999901.0000090.9999930.9999350.9999040.9997180.999476IEE0.3497850.3681390.4054430.4089980.4202110.4249520.436943s15.2035522.2352440.2139946.1138738.4454123.0747318.85263Figure4IER1.0000421.0000360.9999970.9999470.9998570.9998440.999837IEE0.2082190.2437260.3044720.3206920.3486960.3772300.381473s22.3044635.9390137.2873748.3322539.0933626.4424124.57120Figure5IER1.0001421.0000071.0000050.9999940.9999710.9995220.999473IEE0.2396480.2384620.2410030.3781030.3804700.3920620.394378s18.4405828.1149832.0963742.0478837.4900722.3221317.15010Table 4
Soft threshold.
Layers1234567Figure1IER1.0000121.0000081.0000020.9999620.9996340.9996230.998252IEE0.1238510.1740440.2370150.2664670.2704350.2797490.301757s12.7883415.6899529.0064434.8539321.8532916.439208.92942Figure2IER1.0000231.0000010.9999900.9998230.9993470.9993240.998235IEE0.1402760.2032490.2578590.2670060.2706190.2851350.282012s7.0380511.8671927.6102336.4567920.1595514.773458.99417Figure3IER0.9999970.999850.9998740.9997480.9996360.9995240.998648IEE0.1756270.2292260.2404940.2809390.3054620.3221430.330210s5.8070712.8207230.0236426.6001728.5293813.370457.23444Figure4IER1.0000231.0000150.9999450.9998370.9997130.9996380.999564IEE0.1005420.1889800.2295530.2531150.2654220.2701010.274818s12.8066516.5563425.5783838.9200236.1134125.3674219.54325Figure5IER1.0002211.0001040.9999520.9994240.9993290.9978940.993652IEE0.1391090.1443950.1508980.1887500.1975330.2088050.228945s9.7032928.8131032.3488032.8055127.3038212.8400410.47287Table 5
Zero threshold.
Layers1234567Figure1IER0.9992450.9982510.9963240.9958730.9953440.9951060.994857IEE0.3173010.3853970.4226730.4570690.4737940.4779790.505537s8.1498610.9205115.5085720.8440316.7638814.761279.90114Figure2IER0.9993560.9992350.9985440.9977750.9962810.9952380.994567IEE0.3236650.4131660.4628670.4740330.4802470.4973870.525427s5.6412211.8306817.1562323.0645918.9189514.161276.12647Figure3IER0.9999320.9998650.9983640.9974590.9969640.9954870.994786IEE0.3981560.4615970.4875810.5061230.5228240.5355300.621169s5.3601212.1735516.3269124.2420418.1034313.757929.55168Figure4IER0.9999260.9984260.9974520.9972650.9963240.9956870.994568IEE0.2912940.3969250.4319960.4675710.4777620.4977900.56190s2.6358411.6105318.7778923.9855317.6900810.803625.23897Figure5IER0.9993260.9985640.9974560.9972540.9963250.9952680.993125IEE0.2374830.2981720.3210090.3684050.3968610.4179140.451320s7.9544510.8589017.6648222.8859619.6882213.820838.41443From Table5, it can be seen that the integrated index score of wavelet decomposition layers and threshold function shows a trend of increasing and then decreasing, and when the number of wavelet decomposition layers is 4 and the suppression detail factor threshold method is selected, the integrated evaluation index value is the largest, and this experimental result is consistent with the results of the previous mathematical derivation. The data of the abovementioned five stocks are obtained by random sampling, but they all show the maximum value on the same parameters as above, which fully demonstrates the generalizability of the method.The abovementioned five graphics are compared with the original graphics after the abovementioned wavelet denoising method which is shown in Figure7.Figure 7
Comparison of the smoothing process with the original graphics.
(a)(b)(c)(d)(e)


### 5.4. Finding Time Windows Similar to the Target Graph Using the DTW Algorithm

This study selects five stocks in the China A-share market (ticker symbols: 300327.SZ, 300332.SZ, 300336.SZ, 601567.SH, and 601616.SH) as target patterns I–V, respectively. The optimal DTW distances and similarities found using the abovementioned methods, respectively, are shown in Table6. The time complexity of all the abovementioned algorithms is On2.Table 6
Comparison of various methods for optimal DTW distance analysis.
Zero-step aheadOne-step aheadTwo-step aheadPreprocessing without wavelet denoisingPreprocessing with wavelet denoisingPreprocessing with MACDTarget graph I2.236 (0.3090)5.02 (0.1661)0.1197 (0.8930)Target graph II2.637 (0.2749)0.7637 (0.5669)0.1682 (0.8560)Target graph III3.592 (0.2177)0.9091 (0.5238)0.1171 (0.8951)Target graph IV1.946 (0.3394)0.6371 (0.6108)0.1172 (0.8950)Target graph V4.897 (0.1695)0.4918 (0.6703)0.076 (0.9293)The above results show that there is a significant improvement in the final similarity of the original data smoothed using wavelet denoising compared to using the original data directly (the average similarity of the five graphs of the three methods is 0.3612, 0.5075, and 0.8936). This result verifies the need for wavelet denoising. The final similarity of using MACD data compared to direct use of raw data is also significantly improved. Therefore, the final graph recognition method used in this study is to first transform the original data into MACD graphs, then use wavelet denoising to smooth the MACD graphs, and finally use the improved DTW algorithm to achieve effective recognition of the target graphs. The most similar graphs of the five abovementioned SDs after wavelet preprocessing and direct search using the DTW algorithm are shown in Figure8. The most similar graphs found using the DTW algorithm after the abovementioned five target graphs were processed by MACD and then denoised using wavelets, as shown in Figure 9.Figure 8
The graph most similar to the five target graphs.
(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)Figure 9
Graphs most similar to the five target MACD graphs.
(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)


## 5.1. Methodology

This study arbitrarily selects five stocks in the A-share market as the target graphics and determines the wavelet denoising parameters by the abovementioned entropy weighting method. An improved DTW algorithm is then used to find other stock time windows from the stock market that are the least distant from it. In order to achieve better experimental results, this study introduces MACD algorithm to convert the original price series into “MACD graph line” on the basis of wavelet denoising. Then, the distance between the calculated original price series is transformed into the distance between “MACD lines,” which improves the accuracy of the result.


## 5.2. Source of Experimental Data

All stock data in this study are from the Tushare website (https://tushare.pro) and include trading date, opening price, high price, low price, closing price before compounding, and uncompounded upside and downside. Stock data are daily data and the time frame studied is from the listing of each stock to July 24, 2020.


## 5.3. Wavelet Denoising Preprocessing

### 5.3.1. Stock Price Sequence Wavelet Denoising Process

(1)
Choose the appropriate wavelet base function and decomposition layer N for the time seriesf.(2)
Threshold quantification of the decomposed high-frequency coefficients is performed, and for each layer from 1 toN, the appropriate threshold and the appropriate threshold function are selected to obtain the estimated wavelet coefficients.(3)
The original time series is reconstructed (performing wavelet inversion) based on the low-frequency coefficients (scale coefficients) of theNth layer after wavelet decomposition and the high-frequency coefficients (wavelet coefficients) of each layer after threshold quantization. The wavelet reconstruction is then performed using the reconstruction algorithm to obtain the denoised postdecomposition sequence.Because the daily stock closing price sequence is essentially a one-dimensional discrete sequence, this focuses on the wavelet decomposition of the one-dimensional discrete sequence. Experiments show that when a white noise sequence is wavelet decomposed, it can be found that the decay rate of the high-frequency coefficient after wavelet decomposition is proportional to the number of layers of wavelet decomposition, and the variance of the same coefficient is inversely proportional to the number of layers of wavelet decomposition. Thus, for the general case, there is a positive relationship between the number of decomposed layers and the smoothness of the denoised sequence. Therefore, in this study, we have to find the most suitable wavelet denoising parameters based on the characteristics of the stock price sequence.


### 5.3.2. Wavelet Denoising Parameter Investigation

This study selects five stocks in the China A-share market (ticker symbols: 300327.SZ, 300332.SZ, 300336.SZ, 601567.SH, and 601616.SH) as target patterns I–V, respectively. The results of denoising the abovementioned five target graphics using hard threshold, soft threshold, zero threshold, and inhibited detail factor threshold functions combined with the wavelet decomposition layer number method are shown in Tables2–5.Table 2
Suppression of detail factor.
Layers1234567Figure1IER1.0002701.0001981.0000350.9999530.9994750.9985580.998343IEE0.2124690.2899140.3270150.3551670.3709090.3746980.378330s12.5958125.6957149.6128354.7275451.9465426.1783414.00664Figure2IER1.0000291.0001670.9999440.9997750.9992810.9982380.997986IEE0.2361780.3171510.3605680.3772660.3895810.3925510.396433s15.3551121.5418747.1154356.4567950.9883724.7965315.51747Figure3IER0.9999701.0001100.9999840.9997350.9995070.9987200.998531IEE0.2834560.3520920.3925850.4109390.4200730.4247260.427760s15.0958922.7585650.7358656.5978748.4203023.9260118.50077Figure4IER1.0000531.0001361.0000070.9998980.9996180.9996040.999455IEE0.2024960.2830910.3282150.3506920.3638710.3764800.375991s22.4783946.7272148.4098959.9272947.4758026.8606121.57036Figure5IER1.0000051.0002871.0000150.9997490.9990710.9974420.997055IEE0.1397540.2023700.2462940.2717350.2877970.2917350.297407s19.9081528.8595652.1260452.4220847.6068622.5300116.47265Table 3
Hard threshold.
Layers1234567Figure1IER1.0000531.0001361.0000070.9999980.9998180.9998040.999855IEE0.2024960.2830910.4282150.4506920.4638710.4764800.485991s13.5379122.8347139.6128344.9564838.7136128.7243516.53761Figure2IER1.0000341.0000470.9999310.9999230.9997140.9997380.998976IEE0.2284160.3054990.3437740.3897390.3954590.3970320.423563s14.1950421.5970137.9026246.3777440.6392424.0793615.33218Figure3IER0.9999901.0000090.9999930.9999350.9999040.9997180.999476IEE0.3497850.3681390.4054430.4089980.4202110.4249520.436943s15.2035522.2352440.2139946.1138738.4454123.0747318.85263Figure4IER1.0000421.0000360.9999970.9999470.9998570.9998440.999837IEE0.2082190.2437260.3044720.3206920.3486960.3772300.381473s22.3044635.9390137.2873748.3322539.0933626.4424124.57120Figure5IER1.0001421.0000071.0000050.9999940.9999710.9995220.999473IEE0.2396480.2384620.2410030.3781030.3804700.3920620.394378s18.4405828.1149832.0963742.0478837.4900722.3221317.15010Table 4
Soft threshold.
Layers1234567Figure1IER1.0000121.0000081.0000020.9999620.9996340.9996230.998252IEE0.1238510.1740440.2370150.2664670.2704350.2797490.301757s12.7883415.6899529.0064434.8539321.8532916.439208.92942Figure2IER1.0000231.0000010.9999900.9998230.9993470.9993240.998235IEE0.1402760.2032490.2578590.2670060.2706190.2851350.282012s7.0380511.8671927.6102336.4567920.1595514.773458.99417Figure3IER0.9999970.999850.9998740.9997480.9996360.9995240.998648IEE0.1756270.2292260.2404940.2809390.3054620.3221430.330210s5.8070712.8207230.0236426.6001728.5293813.370457.23444Figure4IER1.0000231.0000150.9999450.9998370.9997130.9996380.999564IEE0.1005420.1889800.2295530.2531150.2654220.2701010.274818s12.8066516.5563425.5783838.9200236.1134125.3674219.54325Figure5IER1.0002211.0001040.9999520.9994240.9993290.9978940.993652IEE0.1391090.1443950.1508980.1887500.1975330.2088050.228945s9.7032928.8131032.3488032.8055127.3038212.8400410.47287Table 5
Zero threshold.
Layers1234567Figure1IER0.9992450.9982510.9963240.9958730.9953440.9951060.994857IEE0.3173010.3853970.4226730.4570690.4737940.4779790.505537s8.1498610.9205115.5085720.8440316.7638814.761279.90114Figure2IER0.9993560.9992350.9985440.9977750.9962810.9952380.994567IEE0.3236650.4131660.4628670.4740330.4802470.4973870.525427s5.6412211.8306817.1562323.0645918.9189514.161276.12647Figure3IER0.9999320.9998650.9983640.9974590.9969640.9954870.994786IEE0.3981560.4615970.4875810.5061230.5228240.5355300.621169s5.3601212.1735516.3269124.2420418.1034313.757929.55168Figure4IER0.9999260.9984260.9974520.9972650.9963240.9956870.994568IEE0.2912940.3969250.4319960.4675710.4777620.4977900.56190s2.6358411.6105318.7778923.9855317.6900810.803625.23897Figure5IER0.9993260.9985640.9974560.9972540.9963250.9952680.993125IEE0.2374830.2981720.3210090.3684050.3968610.4179140.451320s7.9544510.8589017.6648222.8859619.6882213.820838.41443From Table5, it can be seen that the integrated index score of wavelet decomposition layers and threshold function shows a trend of increasing and then decreasing, and when the number of wavelet decomposition layers is 4 and the suppression detail factor threshold method is selected, the integrated evaluation index value is the largest, and this experimental result is consistent with the results of the previous mathematical derivation. The data of the abovementioned five stocks are obtained by random sampling, but they all show the maximum value on the same parameters as above, which fully demonstrates the generalizability of the method.The abovementioned five graphics are compared with the original graphics after the abovementioned wavelet denoising method which is shown in Figure7.Figure 7
Comparison of the smoothing process with the original graphics.
(a)(b)(c)(d)(e)


## 5.3.1. Stock Price Sequence Wavelet Denoising Process

(1)
Choose the appropriate wavelet base function and decomposition layer N for the time seriesf.(2)
Threshold quantification of the decomposed high-frequency coefficients is performed, and for each layer from 1 toN, the appropriate threshold and the appropriate threshold function are selected to obtain the estimated wavelet coefficients.(3)
The original time series is reconstructed (performing wavelet inversion) based on the low-frequency coefficients (scale coefficients) of theNth layer after wavelet decomposition and the high-frequency coefficients (wavelet coefficients) of each layer after threshold quantization. The wavelet reconstruction is then performed using the reconstruction algorithm to obtain the denoised postdecomposition sequence.Because the daily stock closing price sequence is essentially a one-dimensional discrete sequence, this focuses on the wavelet decomposition of the one-dimensional discrete sequence. Experiments show that when a white noise sequence is wavelet decomposed, it can be found that the decay rate of the high-frequency coefficient after wavelet decomposition is proportional to the number of layers of wavelet decomposition, and the variance of the same coefficient is inversely proportional to the number of layers of wavelet decomposition. Thus, for the general case, there is a positive relationship between the number of decomposed layers and the smoothness of the denoised sequence. Therefore, in this study, we have to find the most suitable wavelet denoising parameters based on the characteristics of the stock price sequence.


## 5.3.2. Wavelet Denoising Parameter Investigation

This study selects five stocks in the China A-share market (ticker symbols: 300327.SZ, 300332.SZ, 300336.SZ, 601567.SH, and 601616.SH) as target patterns I–V, respectively. The results of denoising the abovementioned five target graphics using hard threshold, soft threshold, zero threshold, and inhibited detail factor threshold functions combined with the wavelet decomposition layer number method are shown in Tables2–5.Table 2
Suppression of detail factor.
Layers1234567Figure1IER1.0002701.0001981.0000350.9999530.9994750.9985580.998343IEE0.2124690.2899140.3270150.3551670.3709090.3746980.378330s12.5958125.6957149.6128354.7275451.9465426.1783414.00664Figure2IER1.0000291.0001670.9999440.9997750.9992810.9982380.997986IEE0.2361780.3171510.3605680.3772660.3895810.3925510.396433s15.3551121.5418747.1154356.4567950.9883724.7965315.51747Figure3IER0.9999701.0001100.9999840.9997350.9995070.9987200.998531IEE0.2834560.3520920.3925850.4109390.4200730.4247260.427760s15.0958922.7585650.7358656.5978748.4203023.9260118.50077Figure4IER1.0000531.0001361.0000070.9998980.9996180.9996040.999455IEE0.2024960.2830910.3282150.3506920.3638710.3764800.375991s22.4783946.7272148.4098959.9272947.4758026.8606121.57036Figure5IER1.0000051.0002871.0000150.9997490.9990710.9974420.997055IEE0.1397540.2023700.2462940.2717350.2877970.2917350.297407s19.9081528.8595652.1260452.4220847.6068622.5300116.47265Table 3
Hard threshold.
Layers1234567Figure1IER1.0000531.0001361.0000070.9999980.9998180.9998040.999855IEE0.2024960.2830910.4282150.4506920.4638710.4764800.485991s13.5379122.8347139.6128344.9564838.7136128.7243516.53761Figure2IER1.0000341.0000470.9999310.9999230.9997140.9997380.998976IEE0.2284160.3054990.3437740.3897390.3954590.3970320.423563s14.1950421.5970137.9026246.3777440.6392424.0793615.33218Figure3IER0.9999901.0000090.9999930.9999350.9999040.9997180.999476IEE0.3497850.3681390.4054430.4089980.4202110.4249520.436943s15.2035522.2352440.2139946.1138738.4454123.0747318.85263Figure4IER1.0000421.0000360.9999970.9999470.9998570.9998440.999837IEE0.2082190.2437260.3044720.3206920.3486960.3772300.381473s22.3044635.9390137.2873748.3322539.0933626.4424124.57120Figure5IER1.0001421.0000071.0000050.9999940.9999710.9995220.999473IEE0.2396480.2384620.2410030.3781030.3804700.3920620.394378s18.4405828.1149832.0963742.0478837.4900722.3221317.15010Table 4
Soft threshold.
Layers1234567Figure1IER1.0000121.0000081.0000020.9999620.9996340.9996230.998252IEE0.1238510.1740440.2370150.2664670.2704350.2797490.301757s12.7883415.6899529.0064434.8539321.8532916.439208.92942Figure2IER1.0000231.0000010.9999900.9998230.9993470.9993240.998235IEE0.1402760.2032490.2578590.2670060.2706190.2851350.282012s7.0380511.8671927.6102336.4567920.1595514.773458.99417Figure3IER0.9999970.999850.9998740.9997480.9996360.9995240.998648IEE0.1756270.2292260.2404940.2809390.3054620.3221430.330210s5.8070712.8207230.0236426.6001728.5293813.370457.23444Figure4IER1.0000231.0000150.9999450.9998370.9997130.9996380.999564IEE0.1005420.1889800.2295530.2531150.2654220.2701010.274818s12.8066516.5563425.5783838.9200236.1134125.3674219.54325Figure5IER1.0002211.0001040.9999520.9994240.9993290.9978940.993652IEE0.1391090.1443950.1508980.1887500.1975330.2088050.228945s9.7032928.8131032.3488032.8055127.3038212.8400410.47287Table 5
Zero threshold.
Layers1234567Figure1IER0.9992450.9982510.9963240.9958730.9953440.9951060.994857IEE0.3173010.3853970.4226730.4570690.4737940.4779790.505537s8.1498610.9205115.5085720.8440316.7638814.761279.90114Figure2IER0.9993560.9992350.9985440.9977750.9962810.9952380.994567IEE0.3236650.4131660.4628670.4740330.4802470.4973870.525427s5.6412211.8306817.1562323.0645918.9189514.161276.12647Figure3IER0.9999320.9998650.9983640.9974590.9969640.9954870.994786IEE0.3981560.4615970.4875810.5061230.5228240.5355300.621169s5.3601212.1735516.3269124.2420418.1034313.757929.55168Figure4IER0.9999260.9984260.9974520.9972650.9963240.9956870.994568IEE0.2912940.3969250.4319960.4675710.4777620.4977900.56190s2.6358411.6105318.7778923.9855317.6900810.803625.23897Figure5IER0.9993260.9985640.9974560.9972540.9963250.9952680.993125IEE0.2374830.2981720.3210090.3684050.3968610.4179140.451320s7.9544510.8589017.6648222.8859619.6882213.820838.41443From Table5, it can be seen that the integrated index score of wavelet decomposition layers and threshold function shows a trend of increasing and then decreasing, and when the number of wavelet decomposition layers is 4 and the suppression detail factor threshold method is selected, the integrated evaluation index value is the largest, and this experimental result is consistent with the results of the previous mathematical derivation. The data of the abovementioned five stocks are obtained by random sampling, but they all show the maximum value on the same parameters as above, which fully demonstrates the generalizability of the method.The abovementioned five graphics are compared with the original graphics after the abovementioned wavelet denoising method which is shown in Figure7.Figure 7
Comparison of the smoothing process with the original graphics.
(a)(b)(c)(d)(e)


## 5.4. Finding Time Windows Similar to the Target Graph Using the DTW Algorithm

This study selects five stocks in the China A-share market (ticker symbols: 300327.SZ, 300332.SZ, 300336.SZ, 601567.SH, and 601616.SH) as target patterns I–V, respectively. The optimal DTW distances and similarities found using the abovementioned methods, respectively, are shown in Table6. The time complexity of all the abovementioned algorithms is On2.Table 6
Comparison of various methods for optimal DTW distance analysis.
Zero-step aheadOne-step aheadTwo-step aheadPreprocessing without wavelet denoisingPreprocessing with wavelet denoisingPreprocessing with MACDTarget graph I2.236 (0.3090)5.02 (0.1661)0.1197 (0.8930)Target graph II2.637 (0.2749)0.7637 (0.5669)0.1682 (0.8560)Target graph III3.592 (0.2177)0.9091 (0.5238)0.1171 (0.8951)Target graph IV1.946 (0.3394)0.6371 (0.6108)0.1172 (0.8950)Target graph V4.897 (0.1695)0.4918 (0.6703)0.076 (0.9293)The above results show that there is a significant improvement in the final similarity of the original data smoothed using wavelet denoising compared to using the original data directly (the average similarity of the five graphs of the three methods is 0.3612, 0.5075, and 0.8936). This result verifies the need for wavelet denoising. The final similarity of using MACD data compared to direct use of raw data is also significantly improved. Therefore, the final graph recognition method used in this study is to first transform the original data into MACD graphs, then use wavelet denoising to smooth the MACD graphs, and finally use the improved DTW algorithm to achieve effective recognition of the target graphs. The most similar graphs of the five abovementioned SDs after wavelet preprocessing and direct search using the DTW algorithm are shown in Figure8. The most similar graphs found using the DTW algorithm after the abovementioned five target graphs were processed by MACD and then denoised using wavelets, as shown in Figure 9.Figure 8
The graph most similar to the five target graphs.
(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)Figure 9
Graphs most similar to the five target MACD graphs.
(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)


## 6. Empirical Analysis of China’s A-Share Market

In order to find out the pattern of the stock market in the short-term and to compare the effectiveness of the abovementioned two methods, the article calculates the average upside and downside of the current time window of the underlying graph and the average upside and downside of the 40-day time window of the optimal graph after the two treatments. The forty-day average increase and decrease in the underlying and optimal graphs are shown in Table7:Table 7
Comparison of 40-day average upside and downside analysis of various methods.
Zero-step aheadOne-step aheadTwo-step aheadPreprocessing without wavelet denoisingPreprocessing with wavelet denoisingPreprocessing with MACDOptimal graphics I0.23540.02350.1546Optimal graphics II0.5773−0.01070.3705Optimal graphics III0.1739−0.00860.0668Optimal graphics IV−0.00940.3866−0.004Optimal graphics V−0.00520.1394−0.0678As can be seen from Table7, the average rise and fall of the optimal graph obtained using the MACD preprocessing method compared to the direct use of the raw data is closer to the average rise and fall of the base graph over the next forty days. This result confirms the lagging nature of the MACD indicator and the effectiveness of the graphical stock selection, fully demonstrating the practicality of the method in practice.


## 7. Conclusions and Discussion

In this study, the wavelet transform is used to denoise the original stock price series, the SymN4 wavelet function is selected according to the characteristics of the stock time series, and the comprehensive scores of different wavelet decomposition layers and wavelet threshold functions are calculated based on the entropy weight method; the results show that the 4-layer decomposition layers and the suppression detail factor threshold method have the highest scores, so the wavelet denoising parameters finally selected in this study are SymN4 wavelet function, 4-layer decomposition layers, and suppression detail factor threshold method. The improved DTW algorithm was then used to calculate the distance between the two sequences to select the optimal graph that is most similar to the target graph. To overcome the effect of price and other factors on the DTW algorithm, the raw sequence data were standardized using the MACD algorithm and then the optimal graph most similar to the target graph was selected using the DTW algorithm. The comparison experimental results show that the similarity between the final graph and the target graph after the MACD algorithm processing is greater than 85%, which fully demonstrates the effectiveness of the graph recognition method. Finally, by comparing the target graphs with the optimal graphs, it is found that the optimal graphs obtained by MACD preprocessing method are closer to the forty-day average increase/decrease of the underlying graphs (the difference between them is within 0.2), indicating that the method has strong practical guidance significance.The graph recognition methods in this study can be nested in stock analysis software, and stock investors can select existing classical patterns or K-line patterns to find stocks that are similar to them for the purpose of graphical stock selection. This provides a practical technical basis for graphical stock selection techniques.The graph recognition methods used in this study are all based on a consistent time window size, so it will result in finding the optimal graph with the same time window size as the target graph, and the DTW algorithm can calculate the similarity of graphs with different size time windows, in addition to the overall recognition of the target graph. Therefore, this study does not fully use all the advantages of the DTW algorithm, and in real-life applications, if the problem of matching different size time window graphics can be solved, it will have more far-reaching implications for stock investors. In future work, this study will try to use genetic algorithm to further find the optimal time window size.



---

*Source: 6641749-2021-01-27.xml*