Package 'bbdetection'

Title: Identification of Bull and Bear States of the Market
Description: Implements two algorithms of detecting Bull and Bear markets in stock prices: the algorithm of Pagan and Sossounov (2002, <doi:10.1002/jae.664>) and the algorithm of Lunde and Timmermann (2004, <doi:10.1198/073500104000000136>). The package also contains functions for printing out the dating of the Bull and Bear states of the market, the descriptive statistics of the states, and functions for plotting the results. For the sake of convenience, the package includes the monthly and daily data on the prices (not adjusted for dividends) of the S&P 500 stock market index.
Authors: Valeriy Zakamulin
Maintainer: Valeriy Zakamulin <[email protected]>
License: GPL-3
Version: 1.0
Built: 2024-12-04 07:13:58 UTC
Source: CRAN

Help Index


Prints out the dating of bull-bear states

Description

This function prints out (in console window) the dating of bull-bear states. The outcome of this function is a table in LaTeX format.

Usage

bb.dating.states(price, bull, dates)

Arguments

price

a numeric vector of price values

bull

a logical vector that contains the states of the market. This vector is returned by function run_dating_alg or run_filtering_alg.

dates

a vector of dates

Value

A data frame object that contains the dating of bull-bear states.

Examples

{
library(zoo)
library(xtable)
library(ggplot2)
sp500 <- sp500m # choose the monthly data
dates <- index(sp500) # retrieve dates
dates <- as.yearmon(dates) # convert dates to "yearmon" format if monthly data
price <- as.vector(coredata(sp500)) # retrieve prices
setpar_dating_alg(4, 6, 4, 16, 20) # parameters for monthly data
bull <- run_dating_alg(price) # detect the states
bb.dating.states(price, bull, dates)
}

Plots the log of prices and highlight bear states

Description

This function plots the log of prices and highlights bear states

Usage

bb.plot(price, bull, dates, price.name=NULL, log.scale=TRUE)

Arguments

price

a numeric vector of price values

bull

a logical vector that contains the states of the market. This vector is returned by function run_dating_alg or run_filtering_alg.

dates

a vector of dates in Date format

price.name

the name of the time-series of prices that will appear on the y-axis of the plot

log.scale

a logical variable that specifies whether to use log scale along the y-axis

Value

None

Examples

{
library(zoo)
library(xtable)
library(ggplot2)
price <- as.vector(coredata(sp500m)) # retrieve monthly prices
dates <- index(sp500m) # retrieve dates from zoo-object
setpar_dating_alg(4, 6, 5, 15, 20) # parameters for monthly data
bull <- run_dating_alg(price) # detect bull-bear states
bb.plot(price, bull, dates, "S&P 500") # plot the result
}

Prints out the summary statistics of bull-bear states

Description

This function prints out (in console window) the summary statistics of bull-bear states. The outcome of this function is a table in LaTeX format.

Usage

bb.summary.stat(price, bull)

Arguments

price

a numeric vector of price values

bull

a logical vector that contains the states of the market. This vector is returned by function run_dating_alg or run_filtering_alg.

Value

A data frame that contains the descriptive statistics.

Examples

{
library(zoo)
library(xtable)
library(ggplot2)
sp500 <- sp500m # choose the monthly data
price <- as.vector(coredata(sp500)) # retrieve prices
setpar_dating_alg(4, 6, 4, 16, 20) # parameters for monthly data
bull <- run_dating_alg(price) # detect the states
bb.summary.stat(price, bull)
}

Daily data the Dow Jones Industrial Average index

Description

The daily closing prices of the DJIA index from January 1985

Usage

djiad

Format

An object of class "zoo" containing the daily closing prices of the DJIA index from January 1985

Source

https://finance.yahoo.com/

See Also

Other data sets: djiam, sp500d, sp500m


Monthly data on the Dow Jones Industrial Average index

Description

The monthly closing prices of the DJIA index from January 1985

Usage

djiam

Format

An object of class "zoo" containing the monthly closing prices of the DJIA index from January 1985

Source

https://finance.yahoo.com/

See Also

Other data sets: djiad, sp500d, sp500m


Runs the dating algorithm to identify Bull and Bear states

Description

This function implements the dating algorithm of Bry and Boschan (1971) to identify Bull and Bear states

Usage

run_dating_alg(index)

Arguments

index

vector containing the stock price index

Value

A logical vector that contains TRUE for Bull states and FALSE for Bear states

Note

Be aware that the states in the beginning and in the end of "index" are not properly defined. The users are advised to always visually check the correctness of the result (during Bull states the prices should generally increase, during the Bear states decrease).

References

Bry, G. and Boschan, C. (1971). Cyclical Analysis of Time Series: Selected Procedures and Computer Programs. NBER.

Pagan, A. R. and Sossounov, K. A. (2003). A Simple Framework for Analysing Bull and Bear Markets. Journal of Applied Econometrics, 18 (1), 23-46.

Gonzalez, L., Powell, J. G., Shi, J., and Wilson, A. (2005). Two Centuries of Bull and Bear Market Cycles. International Review of Economics and Finance, 14 (4), 469-486.


Runs the filtering algorithm to identify Bull and Bear states

Description

This function implements the filtering algorithm of Lunde and Timmermann (2004) to identify Bull and Bear states

Usage

run_filtering_alg(index)

Arguments

index

vector containing the stock price index

Value

A logical vector that contains TRUE for Bull states and FALSE for Bear states

Note

Be aware that the states in the beginning and in the end of "index" are not properly defined

References

Lunde, A. and Timmermann, A. (2004). Duration Dependence in Stock Prices: An Analysis of Bull and Bear Markets. Journal of Business and Economic Statistics, 22 (3), 253-273.


Sets the paramters of the dating algorithm

Description

This function sets the paramters of the dating algorithm of Bry and Boschan (1971)

Usage

setpar_dating_alg(t_window, t_censor, t_phase, t_cycle, max_chng)

Arguments

t_window

the half-size of the rolling window to find minima and maxima

t_censor

the size of the left and right margin (for the cencoring operation)

t_phase

the minimum phase (bull or bear) length

t_cycle

the minimum full cycle length

max_chng

the change (in percentages) in the "index" that invalidates the minimum phase length rule

Value

None

Note

All parameters but "max_chng" are given in a number of observations. For example, if data are at the monthly frequency, "t_cycle=16" defines that the minimum cycle length should be 16 months.

References

Bry, G. and Boschan, C. (1971). Cyclical Analysis of Time Series: Selected Procedures and Computer Programs. NBER.

Pagan, A. R. and Sossounov, K. A. (2003). A Simple Framework for Analysing Bull and Bear Markets. Journal of Applied Econometrics, 18 (1), 23-46.

Gonzalez, L., Powell, J. G., Shi, J., and Wilson, A. (2005). Two Centuries of Bull and Bear Market Cycles. International Review of Economics and Finance, 14 (4), 469-486.


Sets the paramters of the filtering algorithm

Description

This function sets the paramters of the filtering algorithm of Lunde and Timmermann (2004)

Usage

setpar_filtering_alg(tr_bull, tr_bear)

Arguments

tr_bull

threshold to idenitfy a Bull state (in percentages)

tr_bear

threshold to idenitfy a Bear state (in percentages)

Value

None

References

Lunde, A. and Timmermann, A. (2004). Duration Dependence in Stock Prices: An Analysis of Bull and Bear Markets. Journal of Business and Economic Statistics, 22 (3), 253-273.


Daily data the S&P 500 index

Description

The daily closing prices of the S&P 500 index

Usage

sp500d

Format

An object of class "zoo" containing daily closing prices of the S&P 500 index starting from January 1950

Source

https://finance.yahoo.com/

See Also

Other data sets: djiad, djiam, sp500m


Monthly data on the S&P 500 index

Description

Monthly closing prices of the S&P 500 index

Usage

sp500m

Format

An object of class "zoo" containing monthly closing prices of the S&P 500 index starting from January 1950

Source

https://finance.yahoo.com/

See Also

Other data sets: djiad, djiam, sp500d