Get 40% Off
⚠ Earnings Alert! Which stocks are poised to surge?
See the stocks on our ProPicks radar. These strategies gained 19.7% year-to-date.
Unlock full list

Identifying Market Conditions Using Machine Learning

Published 10/06/2015, 04:35 PM
Updated 07/09/2023, 06:32 AM

Knowing how different market conditions affect the performance of your strategy can have a huge impact on your returns.

Certain strategies will perform well in highly volatile, choppy markets while others need a strong, smooth trend or they risk long periods of drawdown. Figuring out when you should start or stop trading a strategy, adjusting your risk and money management techniques, and even setting the parameters of your entry and exit conditions are all dependent on the market “regime”, or current conditions.

Being able to identify different market regimes and altering your strategy accordingly can mean the difference between success and failure in the markets. In this article we will explore how to identify different market regimes by using a powerful class of machine-learning algorithms known as “Hidden Markov Models."

Hidden Markov Models

Markov Models are a probabilistic process that look at the current state to predict the next state. A simple example involves looking at the weather. Let’s say we have three weather conditions (also known as “states” or “regimes”): rainy, cloudy, and sunny. If today is raining, a Markov Model looks for the probability of each different weather condition occurring. For example, there might be a higher probability that it will continue to rain tomorrow, a slightly lower probability that it will be cloudy, and a small probability that it will become sunny.

Weather Table

This seems like a very straightforward process, but the complexity lies in not knowing the probability of each regime shift and how to account for these probabilities changing over time. This is where a Hidden Markov Model (HMM) comes into play. They are able to estimate the transition probabilities for each regime and then, based on current conditions, output the most probable regime.

The applications to trading are very clear. Instead of weather conditions, we could define the regimes as being bullish, bearish, or sideways markets, or high or low volatility, or some combination of factors that we know will have a large effect on the performance of our strategy.

Building A Model Over Real Data

With a couple handy packages in R, we can easily build an HMM model that uses two technical indicators to classify the market into three regimes. For this analysis, we'll look at the daily EUR/USD spot exchange rate since January 1st, 2012 and use the daily log returns as well as a 14-period average true range (ATR) indicator to classify the current regime (you can find the raw data as well as the step-by-step R commands to produce the following plots here).

Let's see what the HMM model was able to find:
EUR/USD HMM Charts

We can clearly see in the third, red plot the market regimes, where Regime 1 tended to fall in times of low volatility, Regime 2 in medium volatility, and Regime 3 in times of high volatility. Now let's see the probability of each regime separately:
EUR/USD Regime Probability Chart

Interesting. The market was classified as Regime 3: High Volatility up until the end of 2012 when it moved briefly into Regime 2: Medium Volatility. There was only a very brief period of Regime 1: Low Volatility in middle or 2014.

Applications To Your Trading


Being able to quickly, objectively, and in a fully automated fashion, classify the current market conditions presents a wide range of benefits to your trading. The simplest would be to see if there is a relationship between the regime and performance of your strategy, allowing you to adjust accordingly. You could also adjust the parameters of your strategy based on the regime. For example, larger take profits and stop losses in times of high volatility. Or even create strategies built specifically to perform well on each regime.

We also chose to classify the market regimes based on the daily logarithmic returns and the ATR, but we could use any indicator, from strength of trend to interest rates to the price of another asset, to classify the market. Once you have the tool in your arsenal, the possibilities are limitless.

Latest comments

Risk Disclosure: Trading in financial instruments and/or cryptocurrencies involves high risks including the risk of losing some, or all, of your investment amount, and may not be suitable for all investors. Prices of cryptocurrencies are extremely volatile and may be affected by external factors such as financial, regulatory or political events. Trading on margin increases the financial risks.
Before deciding to trade in financial instrument or cryptocurrencies you should be fully informed of the risks and costs associated with trading the financial markets, carefully consider your investment objectives, level of experience, and risk appetite, and seek professional advice where needed.
Fusion Media would like to remind you that the data contained in this website is not necessarily real-time nor accurate. The data and prices on the website are not necessarily provided by any market or exchange, but may be provided by market makers, and so prices may not be accurate and may differ from the actual price at any given market, meaning prices are indicative and not appropriate for trading purposes. Fusion Media and any provider of the data contained in this website will not accept liability for any loss or damage as a result of your trading, or your reliance on the information contained within this website.
It is prohibited to use, store, reproduce, display, modify, transmit or distribute the data contained in this website without the explicit prior written permission of Fusion Media and/or the data provider. All intellectual property rights are reserved by the providers and/or the exchange providing the data contained in this website.
Fusion Media may be compensated by the advertisers that appear on the website, based on your interaction with the advertisements or advertisers.
© 2007-2024 - Fusion Media Limited. All Rights Reserved.