Bull Bear States


Bull and Bear are commonly mentioned market dynamics that assist in deploying certain strategies. In this module, two algorithms for bull and bear market detection have been implemented. Pagan and Sossounov’s and Lunde and Timmermann’s algorithms are commonly used to detect different market regimes.

Note

Underlying Literature

The following sources elaborate extensively on the topic:


Pagan and Sossounov

Influenced by Bry and Boschan’s paper (Cyclical Analysis of Time Series: Selected Procedures and Computer Programs, 1971. ), Pagan and Sossounov developed an algorithm to classify bull and bear states. The classification goes through four different filtering methods. The filtering steps are as followed:

  1. Use a window length for both directions to determine local extrema.

  2. Censor the first and last months to eliminate bias.

  3. Remove cycles with length shorter than the given parameter.

  4. Remove phases with length shorter than the given parameter unless if the change is greater than the threshold.

After each filtering method, the corresponding data undergoes an alternation filter that combines consecutive peaks and troughs.

pagan_sossounov(prices, window=8, censor=6, cycle=16, phase=4, threshold=0.2)

Pagan and Sossounov’s labeling method.

Returns a DataFrame with labels of 1 for Bull and -1 for Bear.

Parameters:
  • prices – (pd.DataFrame) Close prices of all tickers in the market.

  • window – (int) Rolling window length to determine local extrema. Paper suggests 8 months for monthly obs.

  • censor – (int) Number of months to eliminate for start and end. Paper suggests 6 months for monthly obs.

  • cycle – (int) Minimum length for a complete cycle. Paper suggests 16 months for monthly obs.

  • phase – (int) Minimum length for a phase. Paper suggests 4 months for monthly obs.

  • threshold – (double) Minimum threshold for phase change. Paper suggests 0.2.

Returns:

(pd.DataFrame) Labeled pd.DataFrame. 1 for Bull, -1 for Bear.

References:

Pagan, Adrian R., and Kirill A. Sossounov. “A simple framework for analysing bull and bear markets.” Journal of applied econometrics 18.1 (2003): 23-46.

Lunde and Timmermann

Lunde and Timmermann’s algorithm is based on realizing the absolute change from the highest or lowest point. Two parameters are used for Lunde and Timmermann: \(\lambda_1\) and \(\lambda_2\). The parameters are considered as switches that indicate the change of states.

lunde_timmermann(prices, bull_threshold=0.15, bear_threshold=0.15)

Lunde and Timmermann’s labeling method. Returns a DataFrame with labels of 1 for Bull and -1 for Bear.

Parameters:
  • prices – (pd.DataFrame) Close prices of all tickers in the market.

  • bull_threshold – (double) Threshold to identify bull market. Paper suggests 0.15.

  • bear_threshold – (double) Threshold to identify bear market. Paper suggests 0.15.

Returns:

(pd.DataFrame) Labeled pd.DataFrame. 1 for Bull, -1 for Bear.

References:

Lunde, Asger, and Allan Timmermann. “Duration dependence in stock prices: An analysis of bull and bear markets.” Journal of Business & Economic Statistics 22.3 (2004): 253-273.

Tip

In their paper, Lunde and Timmermann consider both symmetric (\(\lambda_1 = \lambda_2 =0.2\)) and asymmetric (\(\lambda_1 = 0.2, \lambda_2 = 0.1\)) parameters.


Example

Below is an example on how to create labels for bull and bear detection.

# Import packages
import yfinance as yf

# Importing MlFinLab tools
from mlfinlab.labeling.bull_bear import pagan_sossounov, lunde_timmermann

# Import price data
tickers = "AAPL MSFT AMZN GOOG"
data = yf.download(tickers, start="2010-01-01", end="2022-01-01")["Adj Close"]

# Get labels for Pagan and Sossounov
bull_bear_pagan = pagan_sossounov(
    data, window=8, censor=6, cycle=16, phase=4, threshold=0.2
)

# Get labels for Lunde and Timmermann
bull_bear_lunde = lunde_timmermann(data, bull_threshold=0.15, bear_threshold=0.15)

Presentation Slides



References