tidyquant 0.2.0: Added Functionality for Financial Engineers and Business Analysts
tidyquant, version 0.2.0, is now available on CRAN. If your not already familiar,
tidyquant integrates the best quantitative resources for collecting and analyzing quantitative data,
TTR, with the tidy data infrastructure of the
tidyverse allowing for seamless interaction between each. I’ll briefly touch on some of the updates. The package is open source, and you can view the code on the tidyquant github page.
Table of Contents
Who Will Benefit?
tidyquant package was developed with two people in mind:
Financial Engineers: These individuals systematically analyze financial securities (typically stocks) implementing technical trading rules such as MACD, Bollinger Bands, Moving Averages, etc to determine buy and sell signals in an automated way. Charting and implementing modelling algorithms are highly important.
Financial / Business Analysts: These individuals systematically analyze financial securities, financial statements, key ratios such as valuation (e.g. price to earnings multiples), financial health (e.g. current ratio), efficiency (e.g. inventory turnover). Getting financial and key ratio data is highly important along with charting and to a lesser degree modelling.
For the financial engineer, the package is designed to integrate specialty financial functions within the
tidyverse so the user doesn’t need to jump back and forth between tibbles (tidy data frames) / data frames and xts / zoo time-series objects. Methods like
tq_mutate() exist to apply the various xts, zoo, quantmod, and TTR functions to data frames, so you never need to leave the tidyverse. Further, if the user needs to switch object classes, coercion functions exist to easily convert (see
as_tibble() for converting xts to tibbles / data frames, and
as_xts() for converting data frames to xts).
For the financial analyst, the package is designed to enable retrieving key financial data fast and analyzing financial data easy and efficient. The core function,
tq_get(), has the
get argument that can be set to:
Stock Index: Retrieve a list of stock symbols for an entire index such as the S&P500 with
tq_get("SP500", get = "stock.index"). 18 indexes are available.
Stock Prices: Retrieve the stock prices for an individual stock such as Apple with
tq_get("AAPL", get = "stock.prices").
Financial Statements: Retrieve income statements, balance sheets, and cash flow statements for both annual and quarterly periods for an individual stock,
tq_get("AAPL", get = "financials").
Key Ratios: Retrieve 10-years of historical key ratios (89 total available) for an individual stock,
tq_get("AAPL", get = "key.ratios").
tidyquant is designed to work in the
tidyverse. This means users can use
tidyr verbs to slice and dice data and
purrr to map functions at scale. This enables new capabilities for both financial engineers and analysts. Instead of analyzing one stock at a time, you can now analyze as many stocks as you want at the same time and systematically compare each. See the S&P500 and the more advanced Russell 2000 posts for tutorials on mapping functions to stock lists.
Ok, enough about the benefits. You can read more about them in the vignette. Let’s discuss the updates, and I’ll go through some examples of the new functionality.
The major updates are:
Key ratios from Morningstar: Users can now get 89 different key ratios that span 10 years historically. This is great for users that want to know how EPS, P/E, and even financials have changed over time. The source is Morningstar.
zoo integration: The
rollapply functions from the
zoo package are now fully integrated with
tq_mutate. This means you can calculate rolling averages, maximums, medians, and whatever else your heart desires.
Making things more intuitive and hassle-free: These are small tweaks. The transform and mutate function arguments have changed slightly. The
x_fun argument has been replaced with the more intuitive name
ohlc_fun, so users know to enter a OHLC function such as Op to select the open price of stock prices. The
.y are replaced with
y, which make more sense and don’t interfere with mapping functions in
Now, let’s go through some examples.
tidyquant version 0.2.0. Note that you will need the development version 0.2.0.9000 for this post.
I also recommend the open-source RStudio IDE, which makes R Programming easy and efficient.
Example 1: Getting and Visualizing Key Ratios
You will need to download the development version for this example due to an issue with retrieving key ratios from stocks listed on the NYSE exchange. Key ratios are only available for stocks listed on the NASDAQ exchange in 0.2.0. To continue, upgrade to 0.2.0.9000 using
devtools::install_github("mdancho84/tidyquant") to get the latest development version.
Let’s say we want to compare the valuation over time using the price to earnings (P/E) multiple. This is often done when comparing several companies in the same industry to determine those that may be below normal valuation (i.e. the price may be at a discount to historical trends and to peers).
Hypothetically, we’ll select some big banks to visualize the P/E valuation: JP Morgan (JPM), Goldman Sachs (GS), Bank of America (BAC), and Citi Group (C). Before we can visualize all stocks, let’s first get the key ratios for one stock. Use
tq_get(), which gets data, and set the
get argument to “key.ratios”.
Let’s check out the key ratios by unnesting.