knitr::opts_chunk$set(collapse = TRUE, eval = T, comment = "#>") folder <- "literature_files"; dir.create(folder) download.file("https://www.dropbox.com/s/htnd7o9nnkk8ng8/references.bib?dl=1", paste(folder, "references.bib", sep = "/")) path <- here::here("development", "storethat.sqlite")
factorem belongs to the finRes suite where it facilitates asset pricing research and factor investment backtesting. Install the development version with devtools::install_github("bautheac/factorem")
.
The package is organised around a workhorse function and a series of wrappers for asset pricing factors popular in the literature. The latter functions get raw financial data retrieved from Bloomberg using the pullit package and return S4 objects that carry data belonging to the corresponding factor including positions and return time series.
The factorem
function is the workhorse function in factorem. From raw financial data and a set of parameter specifications it constructs a complete backtest of the corresponding asset pricing factor and returns the whole time series for the factor positions and returns. For the record the S4 object returned by the function also contains the original raw financial data and the set of parameters supplied as well as the original call to the function. The raw financial data should be supplied in a format similar to that of historical datasets returned by historical data query functions in pullit:
library(pullit); library(lubridate) tickers_equity <- c("LZB US Equity", "SGA US Equity", "AGCO US Equity", "CLR US Equity", "GHC US Equity", "MAN US Equity", "SITE US Equity", "AJRD US Equity", "COMM US Equity", "GME US Equity", "MEI US Equity", "SMP US Equity") start <- "2016-01-01"; end <- "2017-12-31" equity_data_market <- pull_equity_market(source = "Bloomberg", tickers_equity, start, end, verbose = FALSE) get_data(equity_data_market)
library(pullit); library(lubridate) tickers_equity <- c("LZB US Equity", "SGA US Equity", "AGCO US Equity", "CLR US Equity", "GHC US Equity", "MAN US Equity", "SITE US Equity", "AJRD US Equity", "COMM US Equity", "GME US Equity", "MEI US Equity", "SMP US Equity") # end <- Sys.Date(); start <- end - years(2L) start <- "2016-01-01"; end <- "2017-12-31" equity_data_market <- pull_equity_market( source = "storethat", tickers = tickers_equity, start = start, end = end, verbose = FALSE, file = path ) get_data(equity_data_market)
From there, construct a bespoke asset pricing factor using the factorem function. I.e., an equity equally weighted momentum factor:
library(factorem) ranking_period = 1L factor <- factorem( name = "momentum", data = pullit::get_data(equity_data_market), ranking_period = ranking_period, sort_levels = FALSE, weighted = FALSE ) factor
factorem provides wrapper methods for popular asset pricing factors in the literature. At the time of writing the factors covered in the package include, equity market and momentum as well as futures market, momentum, commercial hedging pressure (CHP), open interest and term structure factors. The author welcomes pull requests that would help expanding the current coverage.
The equity market factor encompasses the entire cross-section of a defined investment opportunity set. It gained popularity in the 1960s for the central role it plays in the Capital Asset Pricing Model [@treynor_market_1961; @treynor_toward_1961; @sharpe_capital_1964; @mosin_equilibrium_1966; @lintner_valuation_1975] and remains the most popular factor to date in the literature when it comes to explaining the cross-section of equity returns:
equity_market <- market_factor(data = equity_data_market) equity_market
The equity momentum factor is another popular factor in the asset pricing literature. After being first introduced in @carhart_persistence_1997 it eventually drew the field leaders' attention in @fama_size_2012. The equity momentum sorts on prior returns over a defined ranking period:
ranking_period = 1L equity_momentum <- momentum_factor(data = equity_data_market, ranking_period = ranking_period) equity_momentum
tickers_futures <- c( 'LNA Comdty', 'LPA Comdty', 'LTA Comdty', 'LXA Comdty', 'NGA Comdty', 'O A Comdty', 'PAA Comdty', 'PLA Comdty', 'S A Comdty', 'SBA Comdty', 'SIA Comdty', 'SMA Comdty', 'W A Comdty', 'XBWA Comdty' ) futures_data_TS <- pull_futures_market(source = "storethat", type = "term structure", tickers_futures, start, end, file = storethat, verbose = FALSE) pullit::get_data(futures_data_TS)
tickers_futures <- c( 'LNA Comdty', 'LPA Comdty', 'LTA Comdty', 'LXA Comdty', 'NGA Comdty', 'O A Comdty', 'PAA Comdty', 'PLA Comdty', 'S A Comdty', 'SBA Comdty', 'SIA Comdty', 'SMA Comdty', 'W A Comdty', 'XBWA Comdty' ) futures_data_TS <- pull_futures_market( source = "storethat", type = "term structure", tickers_futures, start, end, verbose = FALSE, file = path ) pullit::get_data(futures_data_TS)
factorem provides futures equivalent for the factors above including a futures market factor that takes equally weighted positions on the nearby front contract of the commodity futures series provided:
futures_market <- market_factor(data = futures_data_TS) futures_market
The momentum factor is also popular in the futures asset pricing literature in particular in the context of commodity markets [@miffre_momentum_2007]. As does its equity equivalent, it sorts on prior returns:
ranking_period = 1L futures_momentum <- momentum_factor(data = futures_data_TS, ranking_period = ranking_period) futures_momentum
The futures commercial hedging pressure factor is based on the well-known hedging pressure-based theory [@keynes_treatise_1930; @hicks_value_1939; @houthakker_restatement_1957; @cootner_returns_1960] which postulates that futures prices for a given commodity are inversely related to the extent that commercial hedgers are short or long and the mimicking portfolio here aims at capturing the impact of hedging pressure as a systemic factor [@basu_capturing_2013].
futures_data_CFTC <- pull_futures_CFTC(source = "Bloomberg", tickers_futures, start, end, verbose = FALSE) ranking_period = 1L futures_CHP <- CHP_factor( price_data = futures_data_TS, CHP_data = futures_data_CFTC, ranking_period = ranking_period ) futures_CHP
futures_data_CFTC <- pull_futures_CFTC( source = "storethat", tickers_futures, start, end, verbose = FALSE, file = path ) ranking_period = 1L futures_CHP <- CHP_factor( price_data = futures_data_TS, CHP_data = futures_data_CFTC, ranking_period = ranking_period ) futures_CHP
The open interest factor relates to futures market liquidity and is also present in the literature where it is described as having explanatory power on the cross-section of commodity futures returns [@hong_what_2012]. It comes in two flavours in factorem, nearby and aggregate.
The nearby open interest factor is concerned with liquidity at the very front end of the term structure where it sorts on nearby front contract's open interest:
ranking_period = 1L futures_OI_nearby <- OI_nearby_factor(data = futures_data_TS, ranking_period = ranking_period) futures_OI_nearby
In contrast, the aggregate open interest factor is concerned with term structure liquidity as a whole and sorts on open interest summed up over all the contracts of a particular futures series:
futures_data_agg <- pull_futures_market(source = "Bloomberg", type = "aggregate", tickers_futures, start, end, verbose = FALSE) ranking_period = 1L futures_OI_aggregate <- OI_aggregate_factor(price_data = futures_data_market, aggregate_data = futures_data_agg, ranking_period = ranking_period)
futures_data_agg <- pull_futures_market( source = "storethat", type = "aggregate", tickers_futures, start, end, verbose = FALSE, file = path ) ranking_period = 1L futures_OI_aggregate <- OI_aggregate_factor( price_data = futures_data_TS, aggregate_data = futures_data_agg, ranking_period = ranking_period ) futures_OI_aggregate
The futures term structure factor [@szymanowska_anatomy_2014; @fuertes_commodity_2015] is concerned with the shape (steepness) of the futures term structure and sorts on roll yield:
ranking_period = 1L futures_TS <- TS_factor(data = futures_data_TS, ranking_period = ranking_period) futures_TS
Accessor methods allow access to the content of the factor objects returned by the functions described above:
get_name(factor)
get_positions(factor)
get_returns(factor)
factorem::get_data(factor)
get_parameters(factor)
factorem::get_call(factor)
A summary method returns a performance summary of the corresponding factor:
summary(factor)
The plotit package, also part of the finRes suite, provides plot methods for the factor objects returned by the functions above:
The plot function dispatched on a factor object from factorem with the parameter type
set to "performance" displays an overview of the performance of the corresponding factor in the form of equity curves. The equity curve for the factor is plotted along with, for long-short factors only, that of the long and short legs of the factor independently:
library(plotit) plot(factor, type = "performance")
Similarly, with the type
parameter set to "positions", the plot function shows the proportion of time each asset leaves in the factor with a breakdown by leg for long-short factors:
plot(factor, type = "positions")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.