Package ¡®bbdetection¡¯
September 8, 2017
Type Package
Title Identification of Bull and Bear States of the Market Version 1.0
Author Valeriy Zakamulin
Maintainer Valeriy Zakamulin
Description The package aims to implement two algorithms of detecting Bull and Bear markets in stock prices. 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.
License GPL-3
LazyData TRUE
Depends R (>= 2.10)
Imports Rcpp (>= 0.12.5), zoo, xtable, ggplot2 LinkingTo Rcpp
RoxygenNote 5.0.1 NeedsCompilation yes Archs i386, x64
R topics documented:
Index
8
bb.dating.states ………………………………… 2 bb.plot…………………………………….. 2 bb.summary.stat………………………………… 3 run_dating_alg ………………………………… 4 run_filtering_alg ……………………………….. 5 setpar_dating_alg……………………………….. 5 setpar_filtering_alg ………………………………. 6 sp500d…………………………………….. 7 sp500m……………………………………. 7
1
2 bb.plot
bb.dating.states 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 bull
dates
Value
a numeric vector of price values
a logical vector that contains the states of the market. This vector is returned by
function run_dating_alg or run_filtering_alg. a vector of dates
A data frame object that contains the dating of bull-bear states.
Examples
## Not run:
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)
## End(Not run)
bb.plot 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)
bb.summary.stat
3
Arguments
price bull
dates
price.name
Value
None
Examples
a numeric vector of price values
a logical vector that contains the states of the market. This vector is returned by
function run_dating_alg or run_filtering_alg.
a vector of dates in Date format
the name of the time-series of prices that will appear on the y-axis of the plot
## Not run:
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
## End(Not run)
bb.summary.stat Prints out the summary statistis of bull-bear states
Description
This function prints out (in console window) the summary statistis 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.
4 run_dating_alg Examples
## Not run:
sp500 <- sp500m # choose the monthly data
index <- 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)
## End(Not run)
run_dating_alg 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 Com- puter 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.
Examples
# Assume we have a vector "sp500" that contains the prices of the S&P 500 index
## Not run:
bull <- run_dating_alg(sp500)
## End(Not run)
run_filtering_alg 5
run_filtering_alg 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.
Examples
# Assume we have a vector "sp500" that contains the prices of the S&P 500 index
## Not run:
bull <- run_filtering_alg(sp500)
## End(Not run)
setpar_dating_alg 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)
6
setpar_filtering_alg
Arguments
t_window
t_censor
t_phase
t_cycle
max_chng
Value
None
Note
the half-size of the rolling window to find minima and maxima the size of the left and right margin (for the cencoring operation) the minimum phase (bull or bear) length
the minimum full cycle length
the change (in percentages) in the "index" that invalidates the minimum phase length rule
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 Com- puter 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.
Examples
# Assume we want to use the set of standard parameters to identify the Bull and Bear states when # price data are at monthly frequency
## Not run:
setpar_dating_alg(8, 6, 4, 16, 20)
## End(Not run)
setpar_filtering_alg 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)
sp500d 7 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.
Examples
# Assume we want to use the same threshold of 15% to identify both the Bull and Bear states ## Not run:
setpar_filtering_alg(15, 15)
## End(Not run)
sp500d Daily data included in the package
Description
The daily prices of the S&P 500 index
Usage
sp500d
Format
An object of class "zoo" containing the daily prices of the S&P 500 index from January 1950
sp500m Monthly data included in the package
Description
The monthly prices of the S&P 500 index
Usage
sp500m
Format
An object of class "zoo" containing the monthly prices of the S&P 500 index from January 1950
Index
bb.dating.states, 2 bb.plot, 2 bb.summary.stat, 3
run_dating_alg, 2, 3, 4 run_filtering_alg, 2, 3, 5
setpar_dating_alg, 5 setpar_filtering_alg, 6 sp500d, 7
sp500m, 7
8