stock market trend prediction using hidden markov … market trend prediction using hidden markov...
TRANSCRIPT
Stock Market Trend Prediction Using
Hidden Markov Model
By
Abd El-Moniem Mohamed Ibrahim El-Sayed Abd El-Moniem Bayoumi
A Thesis Submitted to the
Faculty of Engineering at Cairo University
In Partial Fulfillment of the
Requirements for the Degree of
MASTER OF SCIENCE
In
COMPUTER ENGINEERING
FACULTY OF ENGINEERING, CAIRO UNIVERSITY
Giza, Egypt
October 2012
2
Stock Market Trend Prediction Using
Hidden Markov Model
By
Abd El-Moniem Mohamed Ibrahim El-Sayed Abd El-Moniem Bayoumi
A Thesis Submitted to the
Faculty of Engineering at Cairo University
In Partial Fulfillment of the
Requirements for the Degree of
MASTER OF SCIENCE
In
COMPUTER ENGINEERING
Under the Supervision of
Prof. Dr. Nevin M. Darwish
Prof. Dr. Amir F. Atiya
Cairo University
FACULTY OF ENGINEERING, CAIRO UNIVERSITY
Giza, Egypt
October 2012
Stock Market Trend Prediction Using Hidden
Markov Model By Abd El-Moniem Mohamed Ibrahim El-Sayed Abd El-Moniem Bayoumi
A Thesis Submitted to the
Faculty of Engineering at Cairo University
In Partial Fulfillment of the
Requirements for the Degree of
MASTER OF SCIENCE
in
COMPUTER ENGINEERING
Approved by the Examining Committee:
Prof. Dr. Nevin Mahmoud Darwish (Main Advisor)
Prof. Dr. Mohamed Zaki Abd El-Majeed (Member)
Prof. Dr. Osman Hegazi Mohamed (Member)
Prof. Dr. Amir Fouad Soryal (Advisor)
FACULTY OF ENGINEERING CAIRO UNIVERSITY
GIZA, EGYPT
October 2012
Engineer: Abd El-Moniem Mohamed Ibrahim El-Sayed Abd El-Moniem Bayoumi
Date of Birth: 12 / 05 / 1987
Nationality: Egyptian
E-mail: [email protected]
Phone: 01111215582
Address: 4A Refaa St., Dokki, Giza, 4th
floor, apartment no. 18
Registration Date: 01 / 10 / 2009
Awarding Date: / /
Degree: Masters of Science
Department: Computer Engineering Department
Supervisors: Prof. Dr. Nevin Mahmoud Darwish
Prof. Dr. Amir Fouad Sourial Atiya
Examiners: Prof. Dr. Osman Hegazi Mohamed (Faculty of Computers and
Information – Cairo University)
Prof. Dr. Mohamed Zaki Abd El-Majeed (Faculty of Engineering – Al-
Azhar University)
Title of Thesis: Stock Market Trend Prediction Using Hidden Markov Model
Key Words: Stock market forecasting; Hidden Markov models; Baum-Welch
algorithm; Technical indicators.
Summary:
Stock market forecasting has been considered an extremely challenging problem, and its predictability
caused a debate that lasted for years. In this thesis, we present a hidden Markov model that models the
up-trend behavior of stocks, to make use of the pull-back effect. Although stock market forecasting
has been widely addressed in the literature using different machine learning techniques, to our
knowledge there is no work that studied neither the trend behavior nor pull-back effect using hidden
Markov models (HMMs). Furthermore, we designed our proposed model to reflect the time-varying
nature of the stock market. In other words, the transition probabilities of the proposed HMM do not
remain constant with time; however, they are altered in reaction to the stock’s price movement.
Accordingly, the proposed HMM model combines a mixture of dynamic and static probabilities; and
we propose a method for training such a model. In addition, we modified Baum-Welch algorithm to
be suitable for training our proposed model which combines the mixture of static and dynamic
transition probabilities. As a proof of concept, we have used a genetic algorithm to train the hidden
Markov model, and compared its results to those of the modified Baum-Welch algorithm. Moreover,
we develop a modified technical indicator to support the proposed model’s decisions. As a case study,
we test our model using S&P 500 stocks’ list, and compare the profits achieved to the Buy & Hold
benchmark, and we show that we could achieve significant profits.
3
Abstract Stock market forecasting has been considered an extremely challenging problem, and its
predictability caused a debate that lasted for years. In this thesis, we present a hidden
Markov model that models the up-trend behavior of stocks, to make use of the pull-back
effect. Although stock market forecasting has been widely addressed in the literature
using different machine learning techniques, to our knowledge there is no work that
studied neither the trend behavior nor pull-back effect using hidden Markov models
(HMMs). Furthermore, we designed our proposed model to reflect the time-varying
nature of the stock market. In other words, the transition probabilities of the proposed
HMM do not remain constant with time; however, they are altered in reaction to the
stock‟s price movement. Accordingly, the proposed HMM model combines a mixture of
dynamic and static probabilities; and we propose a method for training such a model. In
addition, we modified Baum-Welch algorithm to be suitable for training our proposed
model which combines the mixture of static and dynamic transition probabilities. As a
proof of concept, we have used a genetic algorithm to train the hidden Markov model,
and compared its results to those of the modified Baum-Welch algorithm. Moreover, we
develop a modified technical indicator to support the proposed model‟s decisions. As a
case study, we test our model using S&P 500 stocks‟ list, and compare the profits
achieved to the Buy & Hold benchmark, and we show that we could achieve significant
profits.
Keywords: Stock market forecasting; Hidden Markov models; Baum Welch algorithm;
Technical indicators.
4
Acknowledgements First of all, I would like to thank Allah, The Most Merciful, The Most Forgiving, for
giving me the will and determination to complete this work.
I am totally indebted to the supervisors of this work: Prof. Amir F. Atiya and Prof. Nevin
M. Darwish, for their generous support, guidance, advice and helpful comments. Really, I
have learned a lot of things from them on both the academic and personal sides. Finally, I
would like to thank my family for their patience and encouragement.
5
Contents Abstract .............................................................................................................................. 3
Acknowledgements ........................................................................................................... 4
Index of Figures................................................................................................................. 8
Index of Tables .................................................................................................................. 9
Chapter 1: Introduction
1.1 Stock Market Forecasting........................................................................................ 11
1.2 Current Techniques ................................................................................................. 11
1.3 Contribution ............................................................................................................ 12
1.4 Thesis Organization................................................................................................. 15
Chapter 2: Introduction to Stock Market
2.1 An Overview of the Stock Market .......................................................................... 17
Definition 1: Stock..................................................................................................... 17
Definition 2: Index Share .......................................................................................... 18
Definition 3: Stock Exchange .................................................................................... 18
Definition 4: Open-High-Low-Close (OHLC) Chart ................................................ 18
Definition 5: Earnings ............................................................................................... 18
Definition 6: Dividend ............................................................................................... 19
Definition 7: Price Evolution ..................................................................................... 19
Definition 8: Trend (Bullish & Bearish) .................................................................... 19
Definition9: Sideways Market (Choppy Market) ...................................................... 21
Definition 10: Volatility ............................................................................................ 21
Definition 11: Risk .................................................................................................... 21
2.2 Basic Investment Approaches ................................................................................. 22
2.2.1 Technical Analysis ........................................................................................... 22
6
2.2.1.1 Moving Average Convergence-Divergence (MACD) ...................................... 22
2.2.1.2 Stochastic Oscillator (STO) .............................................................................. 23
2.2.1.3 Relative Strength Index (RSI) ........................................................................... 24
2.2.1.4 Stochastic RSI Oscillator (StochRSI) ............................................................... 25
2.2.2 Fundamental Analysis ...................................................................................... 26
2.3 Efficient Market Hypothesis (EMH) ....................................................................... 31
Chapter 3: Related Work
3.1 Efficient Market Hypothesis (EMH) ....................................................................... 33
3.2 Forecasting Techniques ........................................................................................... 34
3.3 Trends ...................................................................................................................... 41
3.4 Existing Hidden Markov Models Work .................................................................. 43
3.5 Genetic Algorithm (GA) ......................................................................................... 45
3.5.1 Introduction ...................................................................................................... 46
3.5.2 Reproduction .................................................................................................... 46
3.5.3 Crossover .......................................................................................................... 47
3.5.4 Mutation............................................................................................................ 47
3.5.5 Genetic Algorithm Steps .................................................................................. 48
3.6 Research Gaps ......................................................................................................... 49
Chapter 4: Introduction to Hidden Markov Models (HMMs)
4.1 Introduction ............................................................................................................. 51
4.2 Forward & Backward Variables .............................................................................. 52
4.2.1 Forward Variable .............................................................................................. 52
4.2.2 Backward Variable ........................................................................................... 52
4.3 HMM Problems ....................................................................................................... 53
4.3.1 Evaluation Problem .......................................................................................... 53
4.3.2 Decoding Problem ............................................................................................ 53
4.3.3 Learning Problem ............................................................................................. 54
7
4.4 Summary ................................................................................................................. 56
Chapter 5: Proposed Pull-Back Hidden Markov Model
5.1 Proposed Model ...................................................................................................... 58
5.2 Estimation of Model‟s Parameters .......................................................................... 64
5.2.1 Dynamic Baum-Welch Algorithm .................................................................... 64
5.2.2 Genetic Algorithm (GA) ................................................................................... 66
5.3 Modified Technical Indicator: STOCHMACD....................................................... 67
5.4 The Combined Model.............................................................................................. 68
5.5 Stock Evaluation Criteria ........................................................................................ 69
5.6 Implementation Details ........................................................................................... 70
Chapter 6: Case Study
6.1 Experiment Setup .................................................................................................... 72
6.2 Performance Measures ............................................................................................ 73
6.3 Experimental Results............................................................................................... 74
6.3.1 First Test Set ..................................................................................................... 75
6.3.2 Second Test Set ................................................................................................ 77
6.3.3 Third Test Set ................................................................................................... 79
Chapter 7: Conclusion and Future Work
7.1 Conclusion ............................................................................................................... 82
7.2 Future Work ............................................................................................................ 82
Bibliography .................................................................................................................... 83
.Error! Bookmark not defined ........................................................................ ملخص الرسالة
8
Index of Figures Figure 1: Up-Trend Example of Microsoft Stock ............................................................. 14
Figure 2: OHLC Chart Example on IBM Stock ............................................................... 18
Figure 3: Bullish Example of Microsoft Stock ................................................................. 20
Figure 4: Bearish Example of Microsoft Stock ................................................................ 20
Figure 5: Choppy Market Example on AWR Stock ......................................................... 21
Figure 6: MACD Example on IBM Stock ........................................................................ 23
Figure 7: STO Example on IBM Stock............................................................................. 24
Figure 8: RSI Example on IBM Stock .............................................................................. 25
Figure 9: Balance Sheet Example ..................................................................................... 28
Figure 10: Income Sheet Example .................................................................................... 29
Figure 11: Cash Flow Statement Example........................................................................ 30
Figure 12: Crossover Example.......................................................................................... 47
Figure 13: Mutation Example ........................................................................................... 48
Figure 14: Pull-Back Illustration using Microsoft Stock .................................................. 59
Figure 15: Pull-Back HMM Model................................................................................... 60
Figure 16: Proposed HMM's Temporal Diagram ............................................................. 61
Figure 17: P13 Curve Shape Example ............................................................................... 63
Figure 18: Example of the parameters‟ values produced by GA ...................................... 67
Figure 19: Pseudo-code of Trade Orders Generation of STOCHMACD ......................... 68
Figure 20: Pseudo-code of Model Entry Condition .......................................................... 69
Figure 21: Annual Profit Results of First Test Set ............................................................ 76
Figure 22: Annual Profit Results of Second Test Set ....................................................... 78
Figure 23: Annual Profit Results of Third Test Set .......................................................... 80
9
Index of Tables Table 1: Training and Testing Periods .............................................................................. 72
Table 2: Profits Achieved on First Test Set ...................................................................... 75
Table 3: Sharpe Ratio Comparison of First Test Set ........................................................ 76
Table 4: Profits and Sharpe Ratio of Second Test Set ...................................................... 77
Table 5: Sharpe Ratio Comparison of Second Test Set .................................................... 78
Table 6: Profits and Sharpe Ratio of Third Test Set ......................................................... 79
Table 7: Sharpe Ratio Comparison of Third Test Set ....................................................... 80
10
Chapter 1 Introduction
11
In this chapter, we introduce the work done in this thesis. First, the stock market
forecasting problem is discussed in brief. Next, we summarize the current techniques
used in stock market‟s trend forecasting. Then, we present the contribution of this work.
Finally, we demonstrate the organization of the thesis.
1.1 Stock Market Forecasting
Stock market forecasting has received an increasing interest in the last four decades. In
general, it is concerned with predicting the future of the stock prices, through predicting
their exact values, or just their direction of change. Assuming that a trader is able to have
future information, significant profits can be gained.
An increasing amount of empirical evidence points to the existence of pockets of
predictability in the stock market. This predictability is due to methods using
fundamental and technical analysis Fundamental analysis is to study the firm‟s financial
situation and take long-term investment decisions based on it. While, technical analysis is
to study the price history and perform short-term trades based on it [1]. Among the
phenomena that produce some profit opportunities for individual stocks is the existence
of medium term trends of length three to twelve months. This has been documented in the
seminal work of Jegadeesh and Titman [2,3], who show convincing empirical evidence
for the existence of these trends in the stock market, and point to several reasonable
explanations. Subsequently, several other researchers worked on these findings. In fact,
to the common stock market observer trends are quite common. They typically occur
because of the existence of serially correlated positive (or negative) events that are
initially not fully discounted in the stock price. Also insider activity causes the price
behavior to drop down slowly over the course of the trend.
1.2 Current Techniques
Technical indicators are the main techniques used to deal with up-trends; and sometimes,
they are combined with the machine learning techniques. According to our literature
review, there is a gap in studying the trend behavior of the stock market using HMM, and
according to our best knowledge there is no HMM model that considers the trend
prediction problem of stocks. The current HMM models that deal with stock market
12
forecasting targets the prediction of the exact values of stock‟s price, for more details
about these approaches refer to [4,5].
1.3 Contribution
Moving beyond analyzing trends using simple autocorrelation and moving average
approaches, this work focuses on characterizing the trend from start to end as a complete
entity, involving consistent upward movements (or downward movements if it is a
downtrend). Periodically, the stock advance pauses, and a pullback occurs. This is a small
and short temporary down movement (up movement for a downtrend), from which the
stock quickly snaps back, and continues its advance. At the end of the trend the stock
goes into an extended down movement (up-movement), signaling the end of the trend.
We propose a Markov model that characterizes this behavior. It has three states: trend,
pullback, and trend reversal (i.e. reverse movement that signals the end of the trend).
Being in any of these states is hidden, leading to be a Hidden Markov Model. From the
observables which are basically the stock‟s observed price action we estimate the
transition probability values. Once the parameters are estimated, we can predict what
state we are in. This is utilized in a proposed trading strategy that enters a trade if a
pullback state is detected. The proposed trading strategy is tested on the 500 stocks
constituting the S&P 500 index, and is found to produce a trading profit larger than the
benchmark.
Let us consider for illustration an uptrend. Figure 1 shows an example of a trend for the
stock of Microsoft (MSFT) that shows clearly the life cycle of a typical trend. A trend
starts with an initial move that may not look at the time as a definitive start of a trend. It
could possibly be part of the fluctuations or cycles that exist in a directionless market. At
that point it is more likely that a hasty declaration of the existence of a trend would give a
false alarm, as the trend could easily fizzle. On the other hand, if our declaration turns out
to be true, and the trend did materialize, then we stand to gain from the whole extent of
the trend, and large profit can be made. As the trend continues and reaches the “middle
age” part, it becomes more and more evident. At this point a declaration of the existence
of a trend could be more assured, but the profit to be made will be less (because we
initiate the trade relatively late in the life of the trend). There is therefore a compromise
13
between confidence (in a trend decision) and the possible profit achieved in the trade.
The trend is characterized by consistent upward price moves that take the price at higher
levels (the so-called “higher highs”). In the middle of the up-moves in the trend, some
pull-backs occur. These are small opposite price movements that are part of the tug-of-
war between overreaction and under-reaction in price action. A pull-back is generally a
small and shallow price correction that gives a pause to the continuous upward
movements of the price. As the trend progresses and comes closer to its later stages, pull-
backs will generally be more frequent. They become deeper and wider. This is due to the
stock being overpriced. Any negative news event will result in a large downward price
spike. At some point, the trend reaches its demise when a price correction comes, and the
price never recovers and never regains the highest level it once reached during the period
of the trend.
Furthermore, our proposed model takes into account the unstable nature of the stock
market. This unstable nature can be represented as the variation in the market‟s behavior
and reactions with respect to time, based on some changing conditions; i.e. stock
market‟s behavior in similar situations changes with respect to time. This unstable nature
exists in a lot of realistic applications other than the stock market, so this work may be
useful for reuse in other fields. Our proposed model achieves this goal using time-variant
transition probabilities; these dynamic transitions reflect the fact that the stock price‟s
movements differ with respect to time spent in the market. For more illustration, if a
stock encounters an up-trend regime, then the probability that this regime ends will not
remain the same during the whole period of the up-trend. It will be minimal in the
beginning of the up-trend period and will increase with time till it reaches its maximal at
the end of this regime; i.e. as the time spent in the regime increases then most likely the
end of this regime becomes closer. This behavior can be modeled through a dynamic
transition probability that varies according to the time spent by the stock in the up-trend
regime. Similarly, this can be shown clearly when the stock enters a pull-back state. The
probability that the stock recovers from this temporary decrease, increases with respect to
the time spent in this pull-back. This behavior can also be modeled through a dynamic
transition probability that depends on the time spent by the stock in the pull-back.
14
Accordingly, this HMM model combines a mixture of static and dynamic transition
probabilities. Moreover, we train such a HMM model to estimate the parameters of the
equations of the dynamic probabilities beside the static probabilities themselves.
Figure 1: Up-Trend Example of Microsoft Stock
To estimate the model‟s parameters, we modified Baum-Welch algorithm to fit the
training of our time-variant model, i.e. the mixture of static and dynamic transition
probabilities. We call this modified algorithm as “Dynamic Baum-Welch Algorithm”.
This proposed algorithm can be considered for dealing with time-variant HMM models in
any other context other than stock market. There are many environments that require a
mixture of dynamic and static probabilities to model them exactly as in the reality; such
as recognition of real video events [6]. Approximations can be made to model those
dynamic probabilities as just static ones, but this affects reality and accuracy of the
model. This proposed algorithm can enhance the realistic modeling and accuracy of such
complex environments. Concerning evaluating this proposed algorithm in the stocks
context, there are some aspects that must be considered. The stock market is highly
affected by every aspect of our life, even if it may seem irrelevant. The stock market is
affected by political events, terrorism, economic events, rumors … etc. This leads to a lot
of noisy data in the stock market context, which affects the performance of the algorithm
negatively, so judging this proposed algorithm‟s performance in the context of stocks
15
must take into account such aspects. Hence, the performance of this algorithm is expected
to be better in fields other than stock market.
To deal with noise effect, we use another approach to train the model, which is genetic
algorithm (GA). This approach avoids being trapped in a local maximum, unlike the
Baum-Welch algorithm. Then we compare the results of the model using both of the
training approaches. Once we identified a trend, then determining trade entry time is
important. Technical analysis provides some way to determine the time of initiating a
trade. In this thesis, we propose a new technical indicator STOCHMACD, which
combines the concepts of two well know technical indicators: Moving Average
Convergence-Divergence (MACD), and Stochastic Oscillator (STO).
1.4 Thesis Organization
The rest of the thesis is organized as follows. Next chapter introduces the stock markets‟
common terms, basic investment approaches, and efficient market hypothesis. Chapter 3
shows the related work, it discusses the debate of the efficient market hypothesis, besides
exploring the techniques used in stock market forecasting, then it shows the techniques
that are used to deal with trends, furthermore it explores the current hidden Markov
models used. Then, it introduces the conventional genetic algorithm and its main terms.
Chapter 4 presents an introduction about HMMs, and their main issues. It also, discusses
Baum-Welch and Viterbi algorithms that are used in the learning of the HMMs‟
parameters. Chapter 5 presents our proposed HMM. Moreover it discusses our
modifications on the original Baum-Welch algorithm. Then, it presents our proposed
modified technical indicator that supports our HMM, after discussing the original
technical indicators that are used to produce that modified one. Chapter 6 presents our
experiment‟s setup, besides discussing our selection criteria used to choose the stocks on
which the model is applied. We discuss our performance measures. Then we show the
experimental results. Finally, we conclude our work and point out possible future work.
16
Chapter 2 Introduction to Stock Market
17
This chapter introduces the stock market field and the key features of this field. It starts
by giving an overview about the stock market itself, and its common terms and their
definitions. Next, the basic investment approaches are discussed including both technical
and fundamental analysis approaches. Then, the efficient market hypothesis (EMH) is
introduced, which is one of the key aspects that must be considered when studying stock
market predictability.
2.1 An Overview of the Stock Market
The stock market provides traders with an environment that facilitates trading of stock
shares; where it matches the sell and buys orders of the traders. The stock market is
represented by a stock exchange in which the trades take place between the traders. They
trade either in individual stocks‟ shares or in stock indices. The stocks‟ prices change
according to this trades following the demand and supply concept. The stock market
behavior can be classified as either being in up-trend, down-trend or choppy state. These
prices can be visualized using various types of charts; the Open-High-Low-Close
(OHLC) chart representation is considered one of the most effective ways to visualize the
change in stock price. Stock holders can make profits out of the price changes and
dividends that are paid to them from the company that issued that stock.
In the coming subsections, we present a brief description about the stock market basics,
and its common terms such as share, earnings, trend … etc. More definitions, illustrations
and examples about stock market basics can be found in Investopedia website [7].
Definition 1: Stock Stock can be also called a share or equity. It is a certificate that proves a fractional
ownership of a portion of a company. Where owning a share issued by a company in the
stock market means the same as being a partner in that company, and implies the right to
earn a portion of the company profits, which is called the dividend. The percentage of
partnership in the company depends of the number of stock shares that a trader owns
[8,9]. The first time a company issues its shares to the stock market is called the initial
public offering (IPO).
18
Definition 2: Index Share Index share implies an ownership to a group of stocks; it is traded as a single portfolio,
such as the S&P 500 (Standard and Poor 500), and Dow Jones 30 indices [8].
Definition 3: Stock Exchange The stock exchange represents the stock market. It gives the opportunity to match
between sell and buy orders of traders, through meeting in-person or electronically.
Where, it can be a physical place like NYSE (New York Stock Exchange) where traders
deal physically, or virtual where traders invest in a totally electronic manner like
NASDAQ (National Association of Securities Dealers Automated Quotations) [10,8].
Here we discuss the most common definitions of the stock market.
Definition 4: Open-High-Low-Close (OHLC) Chart It is a representation of the daily price data of a stock; it consists of four items as follows:
- Open: It is the stock‟s price at the beginning of the trading day.
- High: It is the highest price reached by the stock during the trading day.
- Low: It is the lowest price reached by the stock during the trading day.
- Close: It is the stock‟s price at the end of the trading day.
Figure 2: OHLC Chart Example on IBM Stock
Definition 5: Earnings Earnings are the profits achieved by company; they are reported four times per year i.e.
once every year quarter. They give an indication whether the company is successful and