A Stock Pattern Recognition Algorithm Based On Neural Networks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

A Stock Pattern Recognition Algorithm Based on Neural Networks

Xinyu Guo Xun Liang Xiang Li


Institute of Computer Institute of Computer Institute of Computer
Science & Technology Science & Technology Science & Technology
Peking University Peking University Peking University
100871 Beijing, China 100871 Beijing, China 100871 Beijing, China
[email protected] [email protected] [email protected]

Abstract pattern respectively.

Recent studies show that stock patterns might


implicate useful information for stock price forecasting.
The patterns underlying the price time series can not
be discovered exhaustively by the pure man power in a
limited time, thus the computer algorithm for stock
price pattern recognition becomes more and more (a) (b)
popular. Currently, there are mainly two kinds of stock Figure 1. (a) A typical continuation pattern:
price pattern recognition algorithms: the algorithm ascending symmetrical triangle. (b) A typical
based on rule-matching and the algorithm based on reversal pattern: head and shoulders tops.
template-matching. However, both of the two With the constant enlargement of the securities
algorithms highly require the participation of domain exchange market, it is unrealistic for the finance
experts, as well as their lacks of the learning ability. To practitioners to discover patterns from thousands of
solve these problems, the paper proposes a stock price hundreds of stocks in time without any professional
pattern recognition approach based upon the artificial aids. Scientists begin to design computer-based
neural network. The experiment shows that the neural algorithms for technical pattern recognition [2, 3, 4, 5,
network can effectively learn the characteristics of the 6, and 7]. The technical pattern recognition algorithm
patterns, and accurately recognize the patterns. can mainly be classified into two categories, one is the
rule-based algorithm [2, 5, 6, 7], and the other is
1. Introduction template-based algorithm [3, 4]. Nonetheless, either of
these two categories has to design a specific rule or
As an approach for stock investment, technical template for each pattern, which requires highly
analysis has been widely-scrutinized by research professional skills as well as involves considerable
communities, and the technical pattern analysis is risks thus posing an enormous challenge for technical
regarded as one of the most important technical analysts.
analysis approaches. For the past several years, neural network has been
In the long-term stock analysis experience, stock going through an increment in popularity within stock
analysts summarized many technical patterns beneficial market analysis. One typical illustration is the study
for the investment decision-making, which can be conducted in [8], where a recognition algorithm for
classified into two categorizes: the continuation pattern triangle patterns based upon a recurrent neural network
and the reversal pattern. Continuation pattern indicates was introduced. Nevertheless, it seems computationally
that the stock price is going to keep its current expensive since the method needs to manipulate each
movement trend; while the reversal pattern indicates time point within the series. In this work, we also
that the stock price will move to the opposite trend. 63 propose an approach relying on neural network
important technical patterns are detailed in [1]. In this whereas the most noticeable difference, compared to
paper, we choose 18 typical technical patterns as the the work done in [8], lies in that the inputs of the
research target, including 10 continuation patterns and network do not cover every time point in the series. On
8 reversal patterns. Figure 1(a) and Figure 1(b) show the contrary, a segmentation process is adopted in this
one typical continuation pattern and one reversal work to first transform the original time series into a

Third International Conference on Natural Computation (ICNC 2007)


0-7695-2875-9/07 $25.00 © 2007
sequence of trend segments and corresponding features, distances between the data points within this segment
with each of the features calculated in terms of the and the corresponding line.
price at the last time point within the segment. 3) Merge the two adjacent segments together whose
Eventually, this sequence of features, instead of the merging yields the smallest cost.
whole time series, is designated as part of the inputs of 4) If the current segment number is greater than T/K,
the network, which in comparison with [8], not only is go to 3), otherwise 5).
able to reduce the calculation expense but also enable 5) Connect the neighbor segment in a head-to-tail
the alteration of time granularity for stock patterns by manner, forming a continuous zigzag line.
adjusting the length of the segments. 6) Merge the two adjacent segments together if they
The paper is arranged as follows: Section 2 details share the same trend.
the feature extraction process. The pattern recognition 7) Delete those segments with tiny price oscillation.
algorithm using neural network is discussed in section 8) If the ith segment has an upward trend while the
3. Section 4 covers the experimental results and a (i + 1)th has a downward trend, move the right end of
conclusion is given in section 5. the former (i.e. the left end of the latter) to the
maximum data point within the two segments, and vice
2. Feature extraction versa.
9) If the segmentation result changes during step 6) ,
The stock price time series can be denoted by a 7) or 8), go to 6), otherwise 10).
10) Return the sequences of trend segments.
curve within a certain plane. As given by Figure 2,
The corresponding segmenting results, given that
where the x-coordinate represents the trading days
k=26 and k=5, are visualized in (a), (b) of Figure 3,
while the y-coordinate the closing prices.
respectively.

(a) (b)
Figure 2. The closing price of YanJing Beer Figure 3. The segmentation results. (a) The
on a daily basis from July 25th, 2002 to segmentation result when k = 5 with 111
rd
November 23 , 2005. segments in total; (b) The segmentation
Because stock price time series contains a large result when k = 26 with 19 segments in total.
number of time points, it is a time-consuming job to A technical pattern is composed of several
analyze the patterns from the time series directly. continuous trend segments, based on which several
Therefore, a simplified but efficient method should be samples can be extracted by a certain length of time
used to represent the time series. In this paper, we window. For example, suppose a time series with the
adopt the segmentation process to simplify it, as [2] length T is divided into m trend segments, therefore the
and [9] do. segment series can be denoted as (s1, s2, …, sm), with
There are three methods to realize the trend s i (1 ≤ i ≤ m) representing the ith segment. When
segmentation, including sliding windows, top-down the length of the time window is w, we can extract
and bottom-up [10]. Suppose the stock price time series m-w+1 samples from this sequence of segments,
contains T time points, with the average length for
denoted as (s1, s2, …, sw), (s2, s3, …, sw+1),…,(sm-w+1,
segments as k (1 < k < T), thus the running time for
sm-w+2, …, sm).
both the sliding windows and the bottom-up algorithm
As shown in Figure 4, assuming that the length of
is O (T), while the top-down is O (T2). In this work, we
the time window is 7, we mark the end points of the
choose the bottom-top approach to conduct the time
segments sequentially as 0, 1, 2, …, 7, and make these
series segmentation, with the specific steps as follows.
7 segments into a sample with 8 corresponding price
1) Put the data at both the (2i – 1)th and the 2ith
value denoted as y0, y1, y2, … , y8. Generally, the
time points into one segment, thus the original time
movement range of the price varies according to the
series is divided into T/2 segments.
stock. In order to eliminate the influences caused by
2) Calculate the cost of merging two adjacent
this variance, it is necessary to normalize the price into
segments together, namely the merging cost. Here we
a uniform interval ([0, 1] for instance) [8], or to acquire
define the segment cost as the sum of all the squared

Third International Conference on Natural Computation (ICNC 2007)


0-7695-2875-9/07 $25.00 © 2007
the relative price value by calculating the proportion. the expected output.
The latter approach is adopted here in this paper. Here we try to decide whether a sample belongs to
a certain pattern via the following method. We define
1 7 two positive numbers h1 and h2, which are both very
3
5
close to 0 (e.g. h1=0.005,h2=0.01), and we name them
recognition thresholds. Regarding a certain input
sample, and the corresponding output vector is (a1,
4
6 a2, …, aM), where M denotes the number of neurons in
2
0
the output layer, then this sample should be categorized
into the ith pattern only if |ai – 1| ≤ h1 (1 ≤ i ≤ M) and at
the same time aj ≤ h2 (1 ≤ j ≤ M, j ≠ i), otherwise, it is
Figure 4. Marking the starting and ending considered not belong to any pattern.
points of each segment.
We define 8 features as follows for the sample with 3.2. Metrics for the pattern recognition
a length of 7. experiments

First of all, the most important metric for


, (1) classification is the precision. Second, due to the fact
y i − y i −1 that there are some samples in the testing set that can
pi = , i = 2,3,...,7 , (2) not be categorized into any predefined pattern, the
y i −1 − y i −2 ability of the neural network to distinguish such kind of
y − y1 samples out of the other samples is also considered to
p8 = 7 , (3) be of great significance. In order to evaluate the
y 2 − y1 classification performance of the neural network, we
define seven metrics as follows.
3. Pattern recognition using a classification 1) Classification precision. This term refers to the
proportion of accurately-recognized patterns compared
neural network to all the samples that can be categorized as some
patterns by the network, denoted as prc.
3.1. Algorithm 2) Classification precision for continuation
(reversal) pattern. This term refers to the proportion of
In this paper we investigate the 18 kinds of typical accurately-recognized samples as continuation
technical patterns. Opposed to the aforementioned [8], (reversal) patterns compared to all the samples which
in this work we transform the recognition into a are categorized by the network as continuation
process of classification using a three-layer (reversal) patterns, denoted as cprc (rprc.).
feedforward neural network, whose inputs are the 3) Isolation precision. This term refers to the
sample features defined in section 2. proportion of accurately-recognized samples (either
A three-layer feedforward neural network is belonging to some predefined pattern or not) compared
typically composed of one input layer, one output layer to all the testing samples, denoted as pri.
and one hidden layers. In the input layer, each neuron 4) Recall. This term refers to the proportion of
corresponds to a feature; while in the output layer, each accurately-recognized patterns compared to all the
neuron corresponds to a predefined pattern. samples, contained in the testing set, which are of some
The first step to carry out the classification is to patterns, denoted as rc.
train the neural network with a group of training 5) Recall for continuation (reversal) pattern. This
samples, which can be characterized as having term refers to the proportion of accurately-recognized
expected outputs provided in them. What is worth samples as continuation (reversal) patterns compared to
mentioning is that every training sample belongs to a all the samples, in the testing set, which are of
certain predefined pattern. Then we can use the testing continuation (reversal) patterns, denoted as crc (rrc).
samples to test the performance of the trained network.
The best situation is that once a certain sample is input 4. Experiments
into the network, the output will be a vector with all
elements as zero only except the one corresponding to We chose 2029 samples out of 508 stocks from
the pattern that the sample belongs to. Nonetheless, due Shanghai Stock Exchange, which include 593
to the existence of classification errors and the fact that continuation patterns and 1436 reversal patterns, as
some testing samples don’t belong to any of the 18 training samples. At the mean time, we also extract
predefined patterns; some samples can not get exactly 4937 samples as the testing samples out of 155 stocks

Third International Conference on Natural Computation (ICNC 2007)


0-7695-2875-9/07 $25.00 © 2007
from Shenzhen Stock Exchange within the same network architecture and the testing result for NN_B
time-interval, there are 54 continuation patterns, 270 respectively. Obviously, the enhancement in
reversal patterns and 4613 belong to neither of the two. generalization due to the feature discretization
improves the classification precision and recall to a
4.1. Network training great extent.

In this work, the maximum number of segments in 4.3. Grouping training


one sample is 7 with the feature number as 8. The input
samples have to be first normalized before can be put As shown in Table 2, the recall for continuation
into the network for training, by scaling all the feature pattern is rather low no matter whether the feature
values of each sample into the range of [-1, 1]. Each of discretization is adopted or not. The main reason is that
the 18 neurons in the output layer of the network there exist a large number of reversal patterns
corresponds to a certain pattern, and the network is containing fewer segments in the training set, which
denoted as NN_A here. What’s more, when the disturbs the learning process for continuation patterns.
network is tested by the testing set, it is also necessary Confronting this situation, we first divide the training
to normalize the testing samples. sample into 2 parts, the continuation patterns and the
The second lines of Table 1 and Table 2 show the reversal patterns, and then use the two groups to train
network architecture and the testing result for NN_A two feedforward networks, which are denoted as
respectively. h1 and h2 are the recognition thresholds as NN_C and NN_D. Since the patterns being
defined in the last section. investigated in this work contain 10 continuation
patterns and 8 reversal ones, the numbers of the output
4.2. Feature discretization neurons for NN_C and NN_D are 10 and 8 respectively.
When a testing sample has been put into the networks
Table 2 indicates that the classification precision for testing, both the categorization results of the two
and recall of NN_A is not satisfactory, which is networks should be taken into consideration to produce
probably due to the large range the sample features the final recognition; specifically, if the NN_C
have stretched, while the amount of the training sample categorizes it into continuation pattern while NN_D
available is limited, thus lead to a subsequent categorizes it into reversal pattern, the sample is
overfitting and less generalization of the network. In considered to be classified into continuation pattern,
response to that, we keep on analyzing the essence of whereas if NN_C outputs neither pattern for that
the patterns, and then we discover that the pattern is sample, the output of NN_D is adopted as the final
mainly determined by the relative positions of the recognition.
starting and ending points within each segment. We can The fourth and the fifth lines of Table 1 show the
describe the correlation between points by using a network architectures for NN_C and NN_D
finite number of state indicators. For instance, in the respectively. The fourth line of Table 2 shows the
context of two points, we can use 0 to indicate that the testing result for NN_C and NN_D.
latter is lower than the former, 1 the latter is more or The experimental results illustrate that the
less the same height as the former, and 2 the latter is implementation of grouping training greatly enhances
higher than the former. In this way the discrete feature the recall for continuation pattern though decreases a
values can be realized, namely the feature little bit the classification precision for it, and the other
discretization. With these discrete features, we metrics are no less than the NN_B network. Therefore,
construct a new network using the approach adopted we regard the NN_C combining NN_D network as a
when building the NN_A, denote it as NN_B. better approach.
The third lines of Table 1 and Table 2 show the

Table 1. The network architectures for different networks.


number of number of umber of
network input hidden output h1 h2
neurons neurons neurons
NN_A 8 200 18 0.27 0.8
NN_B 8 200 18 0.08 0.03
NN_C 8 200 10 0.01 0.01
NN_D 6 200 8 0.01 0.01

Third International Conference on Natural Computation (ICNC 2007)


0-7695-2875-9/07 $25.00 © 2007
Table 2. The testing results for different networks.
network prc cprc rprc pri rc crc rrc
NN_A 0.524 0.375 0.528 0.887 0.568 0.058 0.676
NN_B 0.983 1 0.983 0.981 0.840 0.167 0.913
NN_C+NN_D 0.963 0.796 0.993 0.995 0.951 0.722 0.996

5. Conclusion Recognition Approach: A Case Study on Cup with Handle. In


the Artificial Neural Network in Engineering Conference, St.
Louis, Missouri, 2004.
With the fast enlargement of the stock market in [7] S. Anand, W. N.Chin, and S. C. Khoo. Chart Patterns on
their scales, it seems impossible to discover the Price History. Proc. of ACM SIGPLAN Int. Conf. on
underlying technical patterns by pure human power. Functional Programming, 134-145. Florence, Italy. 2001.
The implementation of automatic pattern recognition [8] K. Kamijo, and T. Tanigawa. Stock Price Pattern
using computer technologies serves as an effective Recognition: A Recurrent Neural Network Approach. Proc. of
alternative. the Int. Joint Conf. on Neural Networks, vol. 1, 1990,
Searching for patterns from the original price time 215-221.
series is a time-consuming process and thus is not [9] F. Chung, T. Fu, V. Ng, and R. Luk. An Evolutionary
applicable for real-time pattern recognition. In this Approach to Pattern-based Time Series Segmentation. IEEE
Trans. on Evolutionary Computation. 8(5)(2004), 471-489.
paper, we propose a efficient and effective approach to [10] E. Keogh, S. Chu, D. Hart, and M. Pazzani. An Online
represent the stock price time series, which is adopted Algorithm for Segmenting Time Series. Proc. of IEEE Int.
by patterns theory in technical analysis. The stock price Conf. on Data Mining, 2001, 289-296.
time series is reduced to multiple line segments, from
which we extract the pattern features, and the
feedforward neural network is used for the pattern
recognition. According to the empirical results, to
separate the continuation and reversal patterns, by
using two networks to conduct the recognitions
individually, has observably increased the classification
precision as well as the recall.
Within the current literature of technical analysis,
generally the scale of the pattern, namely the time span,
has always been considered as an important criterion to
identify the pattern. Therefore, we will employ the time
span factor in our classification model based upon
artificial neural networks in our subsequent work, in
order to enhance the accuracy and reliability of the
pattern recognition.

References
[1] N. Bulkowski. Encyclopedia of Chart Patterns, 2nd
Edition. John Wiley and Sons, 2005.
[2] C. L. Osler, and P. H. K. Chang. Head and Shoulders: Not
Just a Flaky Pattern. Staff Report No.4, Federal Reserve Bank
of New York.
[3]W. Leigh, N. Paz, and R. Purvis. Market timing: a test of a
charting heuristic. Economics Letters, 77(1)(2002), 55-63.
[4] W. Leigh, N. Modani, and R. Hightower. A
Computational Implementation of Stock Charting: Abrupt
Volume Increase As Signal for Movement in New York Stock
Exchange Composite Index. Decision Support Systems,
37(4)(2004), 515-530.
[5] A. Lo, H. Mamaysky, and J. Wang. Foundations of
Technical Analysis: Computational Algorithms, Statistical
Inference, and Empirical Implementation. Journal of Finance,
55(4)(2000), 1705-1765.
[6] S. C. Suh , D. Li, and J. Gao. A Novel Chart Pattern

Third International Conference on Natural Computation (ICNC 2007)


0-7695-2875-9/07 $25.00 © 2007

You might also like