Tidyquant: Financial Data Analysis in R

Evan Toth

Introduction to Tidyquant

  • Allows manipulation and analysis of financial data
  • Pulls financial data (stocks, ETFs) into R
  • Computes returns and performance metrics
  • Creates financial-style visualizations
  • Integrates with tidy workflows

Pulling Stock Data

  • tq_get is the main function that calls stock data from online sources (Yahoo Finance, FRED, Oanda)
  • tq_mutate and tq_transmute are the main functions for this financial data manipulation (new columns + data frames)
library(tidyquant)
# pulling in Spotify stock data
SPOT <- tq_get("SPOT", from = "2020-01-01")
head(SPOT)

Pulling Stock Data (Output)

  • SPOT dataset
# A tibble: 6 × 8
  symbol date        open  high   low close  volume adjusted
  <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
1 SPOT   2020-01-02  151   153.  150.  152.  662600     152.
2 SPOT   2020-01-03  150.  154.  150.  152. 1018400     152.
3 SPOT   2020-01-06  151.  157   150.  157. 1311900     157.
4 SPOT   2020-01-07  157.  158.  155.  156.  876700     156.
5 SPOT   2020-01-08  156.  159.  155.  159.  974500     159.
6 SPOT   2020-01-09  158.  160.  157.  158. 1630600     158.

Analyzing Performance

  • tq_performance turns investment returns into performance metrics
  • tq_portfolio can do this for multiple groups of investments
# Calculate daily returns
library(tidyquant)
library(dplyr)
SPOT_returns <- SPOT %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = "daily")

# Compute performance metrics
SPOT_returns %>%
  tq_performance(Ra = daily.returns,
                 performance_fun = table.Stats) %>%
  select(ArithmeticMean, Stdev, Kurtosis, Skewness, Maximum, Minimum)

Performance Results

# A tibble: 1 × 6
  ArithmeticMean Stdev Kurtosis Skewness Maximum Minimum
           <dbl> <dbl>    <dbl>    <dbl>   <dbl>   <dbl>
1         0.0012 0.031     3.32   0.0991   0.148  -0.168

Tidyquant Visualizations

  • Tidyquant enables clear, professional visualizations
  • Here, I compare Spotify stock performance with its main competitors
library(tidyquant)
library(ggplot2)

stocks <- tq_get(c("SPOT", "AAPL", "GOOG"), from = "2020-01-01")

stocks %>%
  ggplot(aes(x = date, y = adjusted, color = symbol)) +
  geom_line() +
  labs(
    title = "Stock Comparisons",
    x = "Date",
    y = "Adjusted Price"
  ) +
  theme_tq()

Tidyquant Visualizations (Output)

  • Comparing SPOT with AAPL and GOOG

SPOT Candlestick Chart

  • You can also use the classic stock candlestick chart
  • Done by replacing geom_line() with geom_barchart
SPOT %>%
    ggplot(aes(x = date, y = close)) +
    geom_barchart(aes(open = open, high = high, low = low, close = close)) +
    labs(title = "SPOT Bar Chart", y = "Closing Price", x = "Date") + 
    theme_tq()

SPOT Candlestick Chart (Output)

Conclusion

  • Tidyquant is specifically made to analyze and visualize financial data
  • Improves ease of access to economic data
  • Simplifies return calculations and performance metrics
  • Spotify stock showed strong performance relative to competitors in recent years, with a maximum daily return of 14.8%, and minimum daily return of -16.8%.

Thank you!