Original link:tecdat.cn/?p=1557

Original source:Tuo End number according to the tribe public number

 

“Understanding different stock market conditions and changing trading strategies has a big impact on stock market returns. Figuring out when to start or stop losses, adjusting risk and money management techniques, all depend on the current state of the stock market.

Some strategies perform well in a placid stock market, while others may be suited to strong growth or prolonged declines.

In this article, we explore how to identify different stock market conditions by using a class of powerful machine learning algorithms called hidden Markov models (HMM).

Identifying changing Stock Market Conditions with Machine learning — Application of hidden Markov Models (HMM) Part 1

Picture 1

 

Hidden Markov model

 

A Markov model is a probabilistic process that looks at current states to predict the next state. A simple example is watching the weather.

Suppose we have three weather scenarios: rainy, cloudy and sunny. If it rains today, the Markov model looks for the probability of each different weather. Tomorrow, for example, there could be a high chance of persistent rain, a slightly lower chance of cloudy skies and a low chance of sunny skies.

 

Building a Model

 

Based on the above background, we can then use it to find different stock market conditions to optimize our trading strategy. We use the Shanghai Composite Index (000001.SS) from 2004 to present to construct the model.

First, we get the closing price data of Shanghai Composite Index, calculate the yield data, and establish HMM model to compare the prediction results of the model.

 

Plotting the Closing price and yield data of the Shanghai Composite Index, we see the volatility of the stock market between 2004 and 2017.

After fitting the three-state hidden Markov model for the return rate, the posterior probability of each state is plotted:

From 2007 to 2009, due to the subprime crisis, the stock market experienced amazing fluctuations, which rapidly changed the posterior probability of different states. It can be seen that the probability of state 2 and state 3 changed greatly before and after 2008.

The stock market has been quiet since 2010, so the probability of state 2 and state 3 is in equilibrium.

Based on the above judgment, we define three different states. State 1 is a volatile market, state 2 is a falling market, and state 3 is a rising market. Then the prediction results of different states are returned to the real Shanghai Composite Index to observe whether it conforms to objective logic.

Through the real data fitting, we can see the actual situation of the shock market in state 1 (purple), the falling market in state 2 (green), and the rising market in state 3 (red).

Hidden Markov models provide insights into changing stock market conditions. Thus improving the performance of the trading strategy. From our brief exploration, this model should be worth some time to polish. There is much room for improvement. For example, we can introduce multi-factor analysis and establish multivariate model.


reference

1. Machine learning to identify changing stock market conditions — the application of hidden Markov model (HMM)

2. Garch-dcc model and DCC (MVT) modeling estimation in R language

3.R language implementation Copula algorithm modeling dependency case analysis report

4.R language uses ARIMA model to predict stock returns

5. Implementation of LASSO regression, Ridge regression and Elastic Net model in R language

6. Use R language to realize neural network to predict stock cases

7. Realization of R language volatility prediction: ARCH model and HAR-RV model

8.R language how to do Markov switching model