knitr::opts_chunk$set(collapse = T, comment = "#>", warning = F, message = F, eval = T) 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")
The finRes suite is organised around the data-science pipeline where preprocessing, including data collection and wrangling, plays a major role. finRes addresses the issue in two complementary packages that work in conjunction with most of the finRes dataset packages.
On the one hand the pullit package provides tools for data collection from Bloomberg. It returns clean and tidy, ready-to-use, data objects for other packages further down the pipeline to work with. On the other hand the storethat package works in concert with fewISOs and GICS to help store the data retrieved for off-Bloomberg consumption in R.
Both pullit and storethat work in tandem with the BBGsymbols package. The latter plays a central role in finRes where it helps pullit in interacting with Bloomberg via the interface provided by the Rblpapi package [@Armstrong_Rblpapi] and storethat in storing the data retrieved.
pullit together with storethat are the two workhorses for data ETL (extract, transform, load) in the finRes suite. Using carefully selected Bloomberg datafields from BBGsymbols in tandem with @Armstrong_Rblpapi's Bloomberg interface pullit provides the R user with easy access to Bloomberg financial data for a number of financial instruments including, at the time of writing, equity and equity-like securities, funds with the category encompassing any money-managing entity, indexes, as well as futures series & term structure individual contracts.
For each instrument category, pullit allows data retrieval for a corresponding category specific set of data types. An active Bloomberg connection is required to retrieve Bloomberg financial data using pullit.
library(pullit); library(lubridate) # end <- Sys.Date(); start <- end - years(2L) start <- "2016-01-01"; end <- "2017-12-31"
library(pullit); library(lubridate) start <- "2016-01-01"; end <- "2017-12-31"
Equity data comes in three major categories in pullit, market, book and info. For a given corporation market data records stock market activity while book data records business activity in the form of financial statements commonly referred to as 'books' and info refers to qualitative information. See the fields
dataset in BBGsymbols for a detailed list of the Bloomberg datafields available.
Retrieve market data from Bloomberg for British Petroleum (BP/ LN Equity), the Weir group (WEIR LN Equity) and Apple (AAPL US Equity) with:
equity_tickers <- c('BP/ LN Equity', 'WEIR LN Equity', 'AAPL US Equity') equity_market <- pull_equity_market(source = "Bloomberg", equity_tickers, start, end, verbose = F) equity_market
equity_tickers <- c('BP/ LN Equity', 'WEIR LN Equity', 'AAPL US Equity') equity_market <- pull_equity_market(source = "storethat", equity_tickers, start, end, verbose = F, file = path) equity_market
For financial statements data BBGsymbols replicates the Bloomberg 'financial analysis' monitor (FA \<GO>). At the time of writing, this includes balance sheet, cash flow statement, income statement as well as 'key stats' that gathers broad summary figures and 'ratios' that includes popular financial ratios.
Retrieve the corresponding data for the abovementioned corporations with:
equity_BS <- pull_equity_book(source = "Bloomberg", book = "balance sheet", equity_tickers, start, end, verbose = F) equity_BS
equity_BS <- pull_equity_book(source = "storethat", book = "balance sheet", equity_tickers, start, end, verbose = F, file = path) equity_BS
equity_CF <- pull_equity_book(source = "Bloomberg", book = "cash flow statement", equity_tickers, start, end, verbose = F) equity_CF
equity_CF <- pull_equity_book(source = "storethat", book = "cash flow statement", equity_tickers, start, end, verbose = F, file = path) equity_CF
equity_IS <- pull_equity_book(source = "Bloomberg", book = "income statement", equity_tickers, start, end, verbose = F) equity_IS
equity_IS <- pull_equity_book(source = "storethat", book = "income statement", equity_tickers, start, end, verbose = F, file = path) equity_IS
equity_KS <- pull_equity_book(source = "Bloomberg", book = "key stats", equity_tickers, start, end, verbose = F) equity_KS
equity_KS <- pull_equity_book(source = "storethat", book = "key stats", equity_tickers, start, end, verbose = F, file = path) equity_KS
equity_R <- pull_equity_book(source = "Bloomberg", book = "ratios", equity_tickers, start, end, verbose = F) equity_R
equity_R <- pull_equity_book(source = "storethat", book = "ratios", equity_tickers, start, end, verbose = F, file = path) equity_R
'info' encompasses a range of contemporaneous qualitative information including, but not limited to, firm's name, security type, exchange where the security trades, country of incorporation, etc.
Retrieve the corresponding data for the abovementioned corporations with:
equity_info <- pull_equity_info(source = "Bloomberg", equity_tickers, verbose = F) equity_info
equity_info <- pull_equity_info(source = "storethat", equity_tickers, verbose = F, file = path) equity_info
Fund data only comes in two categories, market and info. For a given fund market data records stock market activity while info data records contemporaneous qualitative information that includes a wide range of fund characteristics. Market historical data as well as contemporaneous qualitative data not only allow for an in depth assessment of the fund performance but potentially also the mapping of the performance to fund characteristics. See the fields
dataset in BBGsymbols for a detailed list of the Bloomberg datafields available.
Retrieve market data from Bloomberg for the SPDR S&P 500 ETF Trust (SPY US Equity):
fund_tickers <- "SPY US Equity" fund_market <- pull_fund_market(source = "Bloomberg", fund_tickers, start, end, verbose = F) fund_market
fund_tickers <- "SPY US Equity" fund_market <- pull_fund_market(source = "storethat", tickers = fund_tickers, start = start, end = end, verbose = F, file = path) fund_market
Retrieve the corresponding qualitative data for the abovementioned funds with:
fund_info <- pull_fund_info(source = "Bloomberg", fund_tickers, verbose = F) fund_info
fund_info <- pull_fund_info(source = "storethat", fund_tickers, verbose = F, file = path) fund_info
Futures data comes in three major categories in pullit, market, CFTC and info. For a given futures series market data records futures market price activity while CFTC data records market positions. 'info' on the other hand records contemporaneous qualitative information for the corresponding futures series, including but not limited to series name, term structure length, contract size, contract unit, etc. See the fields
dataset in BBGsymbols for a detailed list of the Bloomberg datafields available.
pullit helps to retrieve both term structure as well as aggregated market data for futures. Term structure data records market activity for individual futures term structure contracts while aggregated data records market activity measures that are aggregated over the whole term structure for a particular futures series.
Retrieve market data from Bloomberg for the five first term structure contracts on the Corn - #2-yellow (C A Comdty) and silver (SIA Comdty) futures series where the futures chains are constructed by rolling on liquidity (with active contract: "A") with no roll adjustment (none: "N") using:
futures_tickers <- c("C A Comdty", "SIA Comdty") futures_TS <- pull_futures_market(source = "Bloomberg", type = "term structure", active_contract_tickers = futures_tickers, start, end, TS_positions = 1L:5L, roll_type = "A", roll_days = 0L, roll_months = 0L, roll_adjustment = "N", verbose = F) futures_TS
futures_tickers <- c("C A Comdty", "SIA Comdty") futures_TS <- pull_futures_market(source = "storethat", type = "term structure", active_contract_tickers = futures_tickers, start, end, TS_positions = 1L:5L, roll_type = "A", roll_days = 0L, roll_months = 0L, roll_adjustment = "N", verbose = F, file = path) futures_TS
Retrieve the corresponding aggregated futures market data with:
futures_agg <- pull_futures_market(source = "Bloomberg", type = "aggregate", active_contract_tickers = futures_tickers, start, end, verbose = F) futures_agg
futures_agg <- pull_futures_market(source = "storethat", type = "aggregate", active_contract_tickers = futures_tickers, start, end, verbose = F, file = path) futures_agg
pullit helps to retrieve CFTC futures market position data from Bloomberg. The Commodity Futures Trading Commission (CFTC) publishes the Commitments of Traders (COT) reports to help the public understand market dynamics. Specifically, the COT reports provide a breakdown of each Tuesday's open interest for futures and options on futures markets in which 20 or more traders hold positions equal to or above the reporting levels established by the CFTC. See the fields
and tickers_CFTC
datasets in BBGsymbols for details.
Retrieve the corresponding futures market position data with:
futures_CFTC <- pull_futures_CFTC(source = "Bloomberg", active_contract_tickers = futures_tickers, start, end, verbose = F) futures_CFTC
futures_CFTC <- pull_futures_CFTC(source = "storethat", active_contract_tickers = futures_tickers, start, end, verbose = F, file = path) futures_CFTC
'info' encompasses a range of contemporaneous qualitative information on the underlying futures series including, but not limited to, name for the series' underlying, trading exchange, term structure length, contract size, etc.
Retrieve the corresponding data for the abovementioned futures series with:
futures_info <- pull_futures_info(source = "Bloomberg", futures_tickers, verbose = F) futures_info
futures_info <- pull_futures_info(source = "storethat", futures_tickers, verbose = F, file = path) futures_info
The functions above return objects that not only carry the retrieved financial data but also complementary information that can be accessed using bespoke accessor methods. Historical data functions for example return objects that carry a 'tickers' dataframe that indicates the tickers for which some data have been found, a 'fields' dataframe that indicates the data fields for which data has been found, a 'data' dataframe that hosts the retrieved data as well as a character vector hosting the original call to the function. A get_periods()
method complements the accessor methods by indicating the start and end dates between which data have been found for each ticker and data field:
futures_TS
Access each slot using the appropriate accessor:
tickers <- get_active_contract_tickers(futures_TS) tickers
fields <- get_fields(futures_TS) head(fields)
data <- pullit::get_data(futures_TS) data
call <- pullit::get_call(futures_TS) call
All the objects above can be stored in a bespoke database for later off-Bloomberg consumption. The storethat package makes the process seamless by providing befitted storing methods:
library(storethat) db_create() path = "~/storethat.sqlite" db_store(object = futures_TS, file = path, verbose = F) db_store(object = fund_market, file = path, verbose = F)
All the functions described above can be used to query data from a storethat database by setting the source
parameter to "storethat" and providing the path to the database as a string in the file
parameter. If omitted a pop-up window allows to browse the file system and select the database file. The other function parameters are identical for both data sources and the returned objects are identical in all aspects other than the source of the data they contain:
equity_market <- pull_equity_market( source = "storethat", equity_tickers, start, end, verbose = F, file = path ) equity_market
Updating a storethat database is equally straightforward with pullit. Update the equity content of the database with:
storethat_update(instrument = "equity", file = path)
The function pulls data from Bloomberg for all the equity tickers and fields already present in the storethat database and updates the corresponding series up to the current system date.
Narrower updates are also allowed:
storethat_update(instrument = "equity", book = "market")
The plotit package, also part of the finRes suite, provides plot methods for some pullit data objects including, at the time of writing, futures term structure (FuturesTS) and fund market (FundMarket) objects.
Plot a futures series term structure dynamics with:
library(plotit) plot(object = futures_TS, ticker = "C A Comdty")
Plot historical fund performance with:
plot(object = fund_market, ticker = "SPY US Equity")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.