Movatterモバイル変換


[0]ホーム

URL:


Type:Package
Title:Tidy Quantitative Financial Analysis
Version:1.0.11
Description:Bringing business and financial analysis to the 'tidyverse'. The 'tidyquant' package provides a convenient wrapper to various 'xts', 'zoo', 'quantmod', 'TTR' and 'PerformanceAnalytics' package functions and returns the objects in the tidy 'tibble' format. The main advantage is being able to use quantitative functions with the 'tidyverse' functions including 'purrr', 'dplyr', 'tidyr', 'ggplot2', 'lubridate', etc. See the 'tidyquant' website for more information, documentation and examples.
URL:https://business-science.github.io/tidyquant/,https://github.com/business-science/tidyquant
BugReports:https://github.com/business-science/tidyquant/issues
License:MIT + file LICENSE
Encoding:UTF-8
LazyData:true
Depends:R (≥ 3.5.0),
Imports:dplyr (≥ 1.0.0), ggplot2 (≥ 3.4.0), httr, httr2, curl,lazyeval, lubridate, magrittr, PerformanceAnalytics, RobStatTM,quantmod (≥ 0.4-13), purrr, readr, readxl, stringr, tibble,tidyr (≥ 1.0.0), timetk (≥ 2.4.0), timeDate, TTR, xts, rlang,zoo, cli
Suggests:alphavantager (≥ 0.1.2), Quandl, riingo, tibbletime, broom,knitr, forcats, rmarkdown, testthat (≥ 2.1.0), scales,Rblpapi, janitor
RoxygenNote:7.3.2
VignetteBuilder:knitr
NeedsCompilation:no
Packaged:2025-02-13 01:09:08 UTC; mdancho
Author:Matt Dancho [aut, cre], Davis Vaughan [aut]
Maintainer:Matt Dancho <mdancho@business-science.io>
Repository:CRAN
Date/Publication:2025-02-13 05:30:02 UTC

tidyquant: Integrating quantitative financial analysis tools with the tidyverse

Description

The main advantage oftidyquant is tobridge the gap between the best quantitative resources for collecting andmanipulating quantitative data,xts,quantmod andTTR,and the data modeling workflow and infrastructure of thetidyverse.

Details

In this package,tidyquant functions and supporting data sets areprovided to seamlessly combine tidy tools with existing quantitativeanalytics packages. The main advantage is being able to use tidyfunctions with purrr for mapping and tidyr for nesting to extend modeling tomany stocks. See the tidyquant website for more information, documentationand examples.

Users will probably be interested in the following:

To learn more about tidyquant, start with the vignettes:browseVignettes(package = "tidyquant")

Author(s)

Maintainer: Matt Danchomdancho@business-science.io

Authors:

See Also

Useful links:


Pipe operator

Description

Seemagrittr::%>% for details.

Usage

lhs %>% rhs

Stock prices for the "FANG" stocks.

Description

A dataset containing the daily historical stock prices for the "FANG" tech stocks,"META", "AMZN", "NFLX", and "GOOG", spanning from the beginning of2013 through the end of 2016.

Usage

FANG

Format

A "tibble" ("tidy" data frame) with 4,032 rows and 8 variables:

symbol

stock ticker symbol

date

trade date

open

stock price at the open of trading, in USD

high

stock price at the highest point during trading, in USD

low

stock price at the lowest point during trading, in USD

close

stock price at the close of trading, in USD

volume

number of shares traded

adjusted

stock price at the close of trading adjusted for stock splits, in USD

Source

https://www.investopedia.com/terms/f/fang-stocks-fb-amzn.asp


Set Alpha Vantage API Key

Description

Requires the alphavantager packager to use.

Usage

av_api_key(api_key)

Arguments

api_key

Optionally passed parameter to set Alpha Vantageapi_key.

Details

A wrapper foralphavantager::av_api_key()

Value

Returns invisibly the currently setapi_key

See Also

tq_get()get = "alphavantager"

Examples

## Not run: if (rlang::is_installed("alphavantager")) {av_api_key(api_key = "foobar")}## End(Not run)

Zoom in on plot regions using date ranges or date-time ranges

Description

Zoom in on plot regions using date ranges or date-time ranges

Usage

coord_x_date(xlim = NULL, ylim = NULL, expand = TRUE)coord_x_datetime(xlim = NULL, ylim = NULL, expand = TRUE)

Arguments

xlim

Limits for the x axis, entered as character dates in "YYYY-MM-DD"format for date or "YYYY-MM-DD HH:MM:SS" for date-time.

ylim

Limits for the y axis, entered as values

expand

IfTRUE, the default, adds a small expansion factor tothe limits to ensure that data and axes don't overlap. IfFALSE,limits are taken exactly from the data orxlim/ylim.

Details

Thecoord_ functions prevent loss of data during zooming, which isnecessary when zooming in on plots that calculatestats using dataoutside of the zoom range (e.g. when plotting moving averageswithgeom_ma()). Setting limits usingscale_x_datechanges the underlying data which causes moving averages to fail.

coord_x_date is a wrapper forcoord_cartesianthat enables quickly zooming in on plot regions using a date range.

coord_x_datetime is a wrapper forcoord_cartesianthat enables quickly zooming in on plot regions using a date-time range.

See Also

ggplot2::coord_cartesian()

Examples

# Load librarieslibrary(dplyr)library(ggplot2)# coord_x_dateAAPL <- tq_get("AAPL", from = "2013-01-01", to = "2016-12-31")AAPL %>%    ggplot(aes(x = date, y = adjusted)) +    geom_line() +                         # Plot stock price    geom_ma(n = 50) +                     # Plot 50-day Moving Average    geom_ma(n = 200, color = "red") +     # Plot 200-day Moving Average    # Zoom in    coord_x_date(xlim = c("2016-01-01", "2016-12-31"))# coord_x_datetimetime_index <- seq(from = as.POSIXct("2012-05-15 07:00"),                  to   = as.POSIXct("2012-05-17 18:00"),                  by   = "hour")set.seed(1)value <- rnorm(n = length(time_index))hourly_data <- tibble(time.index = time_index,                      value      = value)hourly_data %>%    ggplot(aes(x = time.index, y = value)) +    geom_point() +    coord_x_datetime(xlim = c("2012-05-15 07:00:00", "2012-05-15 16:00:00"))

Deprecated functions

Description

A record of functions that have been deprecated.

Usage

tq_transform(data, ohlc_fun = OHLCV, mutate_fun, col_rename = NULL, ...)tq_transform_xy(data, x, y = NULL, mutate_fun, col_rename = NULL, ...)

Arguments

data

Atibble (tidy data frame) of data typically fromtq_get().

ohlc_fun

Deprecated. Useselect.

mutate_fun

The mutation function from either thexts,quantmod, orTTR package. Executetq_mutate_fun_options()to see the full list of options by package.

col_rename

A string or character vector containing names that can be usedto quickly rename columns.

...

Additional parameters passed to the appropriate mutatationfunction.

x,y

Parameters used with⁠_xy⁠ that consist of column names of variablesto be passed to the mutatation function (instead of OHLC functions).

Details


Excel Date and Time Functions

Description

50+ date and time functions familiar to users coming from anExcel Background.The main benefits are:

  1. Integration of the amazinglubridate package for handling dates and times

  2. Integration of Holidays fromtimeDate and Business Calendars

  3. New Date Math and Date Sequence Functions that factor in Business Calendars (e.g.EOMONTH(),NET_WORKDAYS())

These functions are designed to help users coming from anExcel background.Most functions replicate the behavior of Excel:

Usage

AS_DATE(x, ...)AS_DATETIME(x, ...)DATE(year, month, day)DATEVALUE(x, ...)YMD(x, ...)MDY(x, ...)DMY(x, ...)YMD_HMS(x, ...)MDY_HMS(x, ...)DMY_HMS(x, ...)YMD_HM(x, ...)MDY_HM(x, ...)DMY_HM(x, ...)YMD_H(x, ...)MDY_H(x, ...)DMY_H(x, ...)WEEKDAY(x, ..., label = FALSE, abbr = TRUE)WDAY(x, ..., label = FALSE, abbr = TRUE)DOW(x, ..., label = FALSE, abbr = TRUE)MONTHDAY(x, ...)MDAY(x, ...)DOM(x, ...)QUARTERDAY(x, ...)QDAY(x, ...)DAY(x, ...)WEEKNUM(x, ...)WEEK(x, ...)WEEKNUM_ISO(x, ...)MONTH(x, ..., label = FALSE, abbr = TRUE)QUARTER(x, ..., include_year = FALSE, fiscal_start = 1)YEAR(x, ...)YEAR_ISO(x, ...)DATE_TO_NUMERIC(x, ...)DATE_TO_DECIMAL(x, ...)SECOND(x, ...)MINUTE(x, ...)HOUR(x, ...)NOW(...)TODAY(...)EOMONTH(start_date, months = 0)EDATE(start_date, months = 0)NET_WORKDAYS(start_date, end_date, remove_weekends = TRUE, holidays = NULL)COUNT_DAYS(start_date, end_date)YEARFRAC(start_date, end_date)DATE_SEQUENCE(start_date, end_date, by = "day")WORKDAY_SEQUENCE(start_date, end_date, remove_weekends = TRUE, holidays = NULL)HOLIDAY_SEQUENCE(  start_date,  end_date,  calendar = c("NYSE", "LONDON", "NERC", "TSX", "ZURICH"))HOLIDAY_TABLE(years, pattern = ".")FLOOR_DATE(x, ..., by = "day")FLOOR_DAY(x, ...)FLOOR_WEEK(x, ...)FLOOR_MONTH(x, ...)FLOOR_QUARTER(x, ...)FLOOR_YEAR(x, ...)CEILING_DATE(x, ..., by = "day")CEILING_DAY(x, ...)CEILING_WEEK(x, ...)CEILING_MONTH(x, ...)CEILING_QUARTER(x, ...)CEILING_YEAR(x, ...)ROUND_DATE(x, ..., by = "day")ROUND_DAY(x, ...)ROUND_WEEK(x, ...)ROUND_MONTH(x, ...)ROUND_QUARTER(x, ...)ROUND_YEAR(x, ...)

Arguments

x

A vector of date or date-time objects

...

Parameters passed to underlyinglubridate functions.

year

Used inDATE()

month

Used inDATE()

day

Used inDATE()

label

A logical used forMONTH() andWEEKDAY() Date Extractors to decide whether or not to return names(as ordered factors) or numeric values.

abbr

A logical used forMONTH() andWEEKDAY(). Iflabel = TRUE, used to determine iffull names (e.g. Wednesday) or abbreviated names (e.g. Wed) should be returned.

include_year

A logical value used inQUARTER(). Determines whether or not to return 2020 Q3 as3 or2020.3.

fiscal_start

A numeric value used inQUARTER(). Determines the fiscal-year starting quarter.

start_date

Used in Date Math and Date Sequence operations. The starting date in the calculation.

months

Used to offset months inEOMONTH() ANDEDATE() Date Math calculations

end_date

Used in Date Math and Date Sequence operations. The ending date in the calculation.

remove_weekends

A logical value used in Date Sequence and Date Math calculations.Indicates whether or not weekends should be removed from the calculation.

holidays

A vector of dates corresponding to holidays that should be removed from the calculation.

by

Used to determine the gap in Date Sequence calculations and value to round to in Date Collapsing operations.Acceptable values are: A character string, containing one of"day","week","month","quarter" or"year".

calendar

The calendar to be used in Date Sequence calculations for Holidays from thetimeDate package.Acceptable values are:"NYSE","LONDON","NERC","TSX","ZURICH"

years

A numeric vector of years to return Holidays for inHOLIDAY_TABLE()

pattern

Used to filter Holidays (e.g.pattern = "Easter"). A "regular expression" filtering pattern.

Details

Converters - Make date and date-time from text (character data)

Extractors - Returns information from a time-stamp.

Current Time - Returns the current date/date-time based on your locale.

Date Math - Perform popular Excel date calculations

Date Sequences - Return a vector of dates or a Holiday Table (tibble).

Date Collapsers - Collapse a date sequence (useful indplyr::group_by() andpivot_table())

Value

Examples

# Librarieslibrary(lubridate)# --- Basic Usage ----# Converters ---AS_DATE("2011 Jan-01") # GeneralYMD("2011 Jan-01")     # Year, Month-Day FormatMDY("01-02-20")        # Month-Day, Year Format (January 2nd, 2020)DMY("01-02-20")        # Day-Month, Year Format (February 1st, 2020)# Extractors ---WEEKDAY("2020-01-01")                                  # Labelled DayWEEKDAY("2020-01-01", label = FALSE)                   # Numeric DayWEEKDAY("2020-01-01", label = FALSE, week_start = 1)   # Start at 1 (Monday) vs 7 (Sunday)MONTH("2020-01-01")QUARTER("2020-01-01")YEAR("2020-01-01")# Current Date-Time ---NOW()TODAY()# Date Math ---EOMONTH("2020-01-01")EOMONTH("2020-01-01", months = 1)NET_WORKDAYS("2020-01-01", "2020-07-01") # 131 Skipping WeekendsNET_WORKDAYS("2020-01-01", "2020-07-01",             holidays = HOLIDAY_SEQUENCE("2020-01-01", "2020-07-01",                                         calendar = "NYSE")) # 126 Skipping 5 NYSE Holidays# Date Sequences ---DATE_SEQUENCE("2020-01-01", "2020-07-01")WORKDAY_SEQUENCE("2020-01-01", "2020-07-01")HOLIDAY_SEQUENCE("2020-01-01", "2020-07-01", calendar = "NYSE")WORKDAY_SEQUENCE("2020-01-01", "2020-07-01",                 holidays = HOLIDAY_SEQUENCE("2020-01-01", "2020-07-01",                                             calendar = "NYSE"))# Date Collapsers ---FLOOR_DATE(AS_DATE("2020-01-15"), by = "month")CEILING_DATE(AS_DATE("2020-01-15"), by = "month")CEILING_DATE(AS_DATE("2020-01-15"), by = "month") - ddays(1) # EOMONTH using lubridate# --- Usage with tidyverse ---# Calculate returns by symbol/year/quarterFANG %>%    pivot_table(        .rows       = c(symbol, ~ QUARTER(date)),        .columns    = ~ YEAR(date),        .values     = ~ PCT_CHANGE_FIRSTLAST(adjusted)    )

Excel Financial Math Functions

Description

Excel financial math functions are designed to easily calculate Net Present Value (NPV()),Future Value of cashflow (FV()), Present Value of future cashflow (PV()), and more.

These functions are designed to help users coming from anExcel background.Most functions replicate the behavior of Excel:

Usage

NPV(cashflow, rate, nper = NULL)IRR(cashflow)FV(rate, nper, pv = 0, pmt = 0, type = 0)PV(rate, nper, fv = 0, pmt = 0, type = 0)PMT(rate, nper, pv, fv = 0, type = 0)RATE(nper, pmt, pv, fv = 0, type = 0)

Arguments

cashflow

Cash flow values. When one value is provided, it's assumed constant cash flow.

rate

One or more rate. When one rate is provided it's assumed constant rate.

nper

Number of periods. When 'nper“ is provided, the cashflow values and rate are assumed constant.

pv

Present value. Initial investments (cash inflows) are typically a negative value.

pmt

Number of payments per period.

type

Should payments (pmt) occur at the beginning (type = 0) orthe end (type = 1) of each period.

fv

Future value. Cash outflows are typically a positive value.

Details

Net Present Value (NPV)Net present value (NPV) is the difference between the present value of cash inflows andthe present value of cash outflows over a period of time. NPV is used in capital budgetingand investment planning to analyze the profitability of a projected investment or project.For more information, seeInvestopedia NPV.

Internal Rate of Return (IRR)The internal rate of return (IRR) is a metric used in capital budgeting to estimate theprofitability of potential investments. The internal rate of return is a discount ratethat makes the net present value (NPV) of all cash flows from a particular project equalto zero. IRR calculations rely on the same formula as NPV does.For more information, seeInvestopedia IRR.

Future Value (FV)Future value (FV) is the value of a current asset at a future date based on an assumedrate of growth. The future value (FV) is important to investors and financial plannersas they use it to estimate how much an investment made today will be worth in the future.Knowing the future value enables investors to make sound investment decisions based ontheir anticipated needs. However, external economic factors, such as inflation, can adverselyaffect the future value of the asset by eroding its value.For more information, seeInvestopedia FV.

Present Value (PV)Present value (PV) is the current value of a future sum of money or stream of cash flows given aspecified rate of return. Future cash flows are discounted at the discount rate, and the higherthe discount rate, the lower the present value of the future cash flows. Determining theappropriate discount rate is the key to properly valuing future cash flows, whether they be earningsor obligations. For more information, seeInvestopedia PV.

Payment (PMT)The PaymentPMT() function calculates the payment for a loan based on constant payments and a constant interest rate.

Rate (RATE)Returns the interest rate per period of a loan or an investment.For example, use 6%/4 for quarterly payments at 6% APR.

Value

Examples

NPV(c(-1000, 250, 350, 450, 450), rate = 0.05)IRR(c(-1000, 250, 350, 450, 450))FV(rate = 0.05, nper = 5, pv = -100, pmt = 0, type = 0)PV(rate = 0.05, nper = 5, fv = -100, pmt = 0, type = 0)PMT(nper = 20, rate = 0.05, pv = -100, fv = 0, type = 0)RATE(nper = 20, pmt = 8, pv = -100, fv = 0, type = 0)

Excel Summarising "If" Functions

Description

"IFS" functions are filtering versions of their summarization counterparts.Simply add "cases" that filter if a condition is true.Multiple cases are evaluated as "AND" filtering operations.A single case with| ("OR") bars can be created to accomplish an "OR".See details below.

These functions are designed to help users coming from anExcel background.Most functions replicate the behavior of Excel:

Usage

SUM_IFS(x, ...)COUNT_IFS(x, ...)AVERAGE_IFS(x, ...)MEDIAN_IFS(x, ...)MIN_IFS(x, ...)MAX_IFS(x, ...)CREATE_IFS(.f, ...)

Arguments

x

A vector. Most functions are designed for numeric data.Some functions likeCOUNT_IFS() handle multiple data types.

...

Add cases to evaluate. See Details.

.f

A function to convert to an "IFS" function.Use... in this case to provide parameters to the.f likena.rm = TRUE.

Details

"AND" Filtering:Multiple cases are evaluated as "AND" filtering operations.

"OR" Filtering:Compound single cases with| ("OR") bars can be created to accomplish an "OR".Simply use a statement likex > 10 | x < -10 to perform an "OR" if-statement.

Creating New "Summarizing IFS" Functions:Users can create new "IFS" functions using theCREATE_IFS() function factory.The only requirement is that the output of your function (.f) must be a singlevalue (scalar). See examples below.

Value

Useful Functions

Summary Functions - Return a single value from a vector

Create your own summary "IFS" function

Examples

library(dplyr)library(timetk, exclude = "FANG")library(stringr)library(lubridate)# --- Basic Usage ---SUM_IFS(x = 1:10, x > 5)COUNT_IFS(x = letters, str_detect(x, "a|b|c"))SUM_IFS(-10:10, x > 8 | x < -5)# Create your own IFS function (Mind blowingly simple)!Q75_IFS <- CREATE_IFS(.f = quantile, probs = 0.75, na.rm = TRUE)Q75_IFS(1:10, x > 5)# --- Usage with tidyverse ---# Using multiple cases IFS cases to count the frequency of days with# high trade volume in a given yearFANG %>%    group_by(symbol) %>%    summarise(        high_volume_in_2015 = COUNT_IFS(volume,                                        year(date) == 2015,                                        volume > quantile(volume, 0.75))    )# Count negative returns by monthFANG %>%    mutate(symbol = forcats::as_factor(symbol)) %>%    group_by(symbol) %>%    # Collapse from daily to FIRST value by month    summarise_by_time(        .date_var  = date,        .by        = "month",        adjusted   = FIRST(adjusted)    ) %>%    # Calculate monthly returns    group_by(symbol) %>%    mutate(        returns = PCT_CHANGE(adjusted, fill_na = 0)    ) %>%    # Find returns less than zero and count the frequency    summarise(        negative_monthly_returns = COUNT_IFS(returns, returns < 0)    )

Excel Pivot Table

Description

The Pivot Table is one of Excel's most powerful features, and now it's available inR!A pivot table is a table of statistics that summarizes the data of a more extensive table(such as from a database, spreadsheet, or business intelligence program).

These functions are designed to help users coming from anExcel background.Most functions replicate the behavior of Excel:

Usage

pivot_table(  .data,  .rows,  .columns,  .values,  .filters = NULL,  .sort = NULL,  fill_na = NA)

Arguments

.data

Adata.frame ortibble that contains data to summarize with a pivot table

.rows

Enter one or more groups to assess as expressions (e.g.~ MONTH(date_column))

.columns

Enter one or more groups to assess expressions (e.g.~ YEAR(date_column))

.values

Numeric only. Enter one or more summarization expression(s) (e.g.~ SUM(value_column))

.filters

This argument is not yet in use

.sort

This argument is not yet in use

fill_na

A value to replace missing values with. Default isNA

Details

This summary might include sums, averages, or other statistics, which the pivot table groups together in a meaningful way.

The key parameters are:

R implementation details.

Value

Returns a tibble that has been pivoted to summarize information by column and row groupings

Examples

# PIVOT TABLE ----# Calculate returns by year/quarterFANG %>%    pivot_table(        .rows       = c(symbol, ~ QUARTER(date)),        .columns    = ~ YEAR(date),        .values     = ~ PCT_CHANGE_FIRSTLAST(adjusted)    )

Excel Reference Functions

Description

Excel reference functions are used to efficiently lookup values from a data source.The most popular lookup function is "VLOOKUP", which has been implemented in R.

These functions are designed to help users coming from anExcel background.Most functions replicate the behavior of Excel:

Usage

VLOOKUP(.lookup_values, .data, .lookup_column, .return_column)

Arguments

.lookup_values

One or more lookup values.

.data

Adata.frame ortibble that contains values to evaluate and return

.lookup_column

The column in.data containing exact matching values of the.lookup_values

.return_column

The column in.data containing the values to return if a match is found

Details

VLOOKUP() Details

Value

Returns a vector the length of the input lookup values

Examples

library(dplyr)lookup_table <- tibble(    stock   = c("META", "AMZN", "NFLX", "GOOG"),    company = c("Facebook", "Amazon", "Netflix", "Google"))# --- Basic Usage ---VLOOKUP("NFLX",        .data = lookup_table,        .lookup_column = stock,        .return_column = company)# --- Usage with tidyverse ---# Add company names to the stock dataFANG %>%    mutate(company = VLOOKUP(symbol, lookup_table, stock, company))

Excel Statistical Mutation Functions

Description

15+ common statistical functions familiar to users of Excel (e.g.ABS(),SQRT())thatmodify / transform a series of values(i.e. a vector of the same length of the input is returned).

These functions are designed to help users coming from anExcel background.Most functions replicate the behavior of Excel:

Usage

ABS(x)SQRT(x)LOG(x)EXP(x)RETURN(x, n = 1, fill_na = NA)PCT_CHANGE(x, n = 1, fill_na = NA)CHANGE(x, n = 1, fill_na = NA)LAG(x, n = 1, fill_na = NA)LEAD(x, n = 1, fill_na = NA)CUMULATIVE_SUM(x)CUMULATIVE_PRODUCT(x)CUMULATIVE_MAX(x)CUMULATIVE_MIN(x)CUMULATIVE_MEAN(x)CUMULATIVE_MEDIAN(x)

Arguments

x

A vector. Most functions are designed for numeric data.

n

Values to offset. Used in functions likeLAG(),LEAD(), andPCT_CHANGE()

fill_na

Fill missing (NA) values with a different value. Used in offsetting functions.

Value

Useful functions

Mutation Functions - Transforms a vector

Examples

# Librarieslibrary(timetk, exclude = "FANG")library(dplyr)# --- Basic Usage ----CUMULATIVE_SUM(1:10)PCT_CHANGE(c(21, 24, 22, 25), fill_na = 0)# --- Usage with tidyverse ---# Go from daily to monthly periodicity,# then calculate returns and growth of $1 USDFANG %>%    mutate(symbol = forcats::as_factor(symbol)) %>%    group_by(symbol) %>%    # Summarization - Collapse from daily to FIRST value by month    summarise_by_time(        .date_var  = date,        .by        = "month",        adjusted   = FIRST(adjusted)    ) %>%    # Mutation - Calculate monthly returns and cumulative growth of $1 USD    group_by(symbol) %>%    mutate(        returns = PCT_CHANGE(adjusted, fill_na = 0),        growth  = CUMULATIVE_SUM(returns) + 1    )

Excel Statistical Summary Functions

Description

15+ common statistical functions familiar to users of Excel (e.g.SUM(),AVERAGE()).These functions return asingle value (i.e. a vector of length 1).

These functions are designed to help users coming from anExcel background.Most functions replicate the behavior of Excel:

Usage

SUM(x)AVERAGE(x)MEDIAN(x)MIN(x)MAX(x)COUNT(x)COUNT_UNIQUE(x)STDEV(x)VAR(x)COR(x, y)COV(x, y)FIRST(x)LAST(x)NTH(x, n = 1)CHANGE_FIRSTLAST(x)PCT_CHANGE_FIRSTLAST(x)

Arguments

x

A vector. Most functions are designed for numeric data.Some functions likeCOUNT() handle multiple data types.

y

A vector. Used in functions requiring 2 inputs.

n

A single value used inNTH() to select a specific element location to return.

Details

Summary Functions

Value

Useful functions

Summary Functions - Return a single value from a vector

Examples

# Librarieslibrary(timetk, exclude = "FANG")library(forcats)library(dplyr)# --- Basic Usage ----SUM(1:10)PCT_CHANGE_FIRSTLAST(c(21, 24, 22, 25))# --- Usage with tidyverse ---# Go from daily to monthly periodicity,# then calculate returns and growth of $1 USDFANG %>%    mutate(symbol = forcats::as_factor(symbol)) %>%    group_by(symbol) %>%    # Summarization - Collapse from daily to FIRST value by month    summarise_by_time(        .date_var  = date,        .by        = "month",        adjusted   = FIRST(adjusted)    )

Plot Bollinger Bands using Moving Averages

Description

Bollinger Bands plot a range around a moving average typically two standard deviations up and down.Thegeom_bbands() function enables plotting Bollinger Bands quickly using various moving average functions.The moving average functions used are specified inTTR::SMA()from the TTR package. Usecoord_x_date() to zoom into specific plot regions.The following moving averages are available:

Usage

geom_bbands(  mapping = NULL,  data = NULL,  position = "identity",  na.rm = TRUE,  show.legend = NA,  inherit.aes = TRUE,  ma_fun = SMA,  n = 20,  sd = 2,  wilder = FALSE,  ratio = NULL,  v = 1,  wts = 1:n,  color_ma = "darkblue",  color_bands = "red",  alpha = 0.15,  fill = "grey20",  ...)geom_bbands_(  mapping = NULL,  data = NULL,  position = "identity",  na.rm = TRUE,  show.legend = NA,  inherit.aes = TRUE,  ma_fun = "SMA",  n = 10,  sd = 2,  wilder = FALSE,  ratio = NULL,  v = 1,  wts = 1:n,  color_ma = "darkblue",  color_bands = "red",  alpha = 0.15,  fill = "grey20",  ...)

Arguments

mapping

Set of aesthetic mappings created byggplot2::aes() orggplot2::aes_(). If specified andinherit.aes = TRUE (thedefault), it is combined with the default mapping at the top level of theplot. You must supplymapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

IfNULL, the default, the data is inherited from the plotdata as specified in the call toggplot2::ggplot().

Adata.frame, or other object, will override the plotdata. All objects will be fortified to produce a data frame. Seeggplot2::fortify() for which variables will be created.

Afunction will be called with a single argument,the plot data. The return value must be adata.frame., andwill be used as the layer data.

position

A position adjustment to use on the data for this layer. Thiscan be used in various ways, including to prevent overplotting andimproving the display. Theposition argument accepts the following:

  • The result of calling a position function, such asposition_jitter().This method allows for passing extra arguments to the position.

  • A string naming the position adjustment. To give the position as astring, strip the function name of theposition_ prefix. For example,to useposition_jitter(), give the position as"jitter".

  • For more information and other ways to specify the position, see thelayer position documentation.

na.rm

IfTRUE, silently removesNA values, whichtypically desired for moving averages.

show.legend

logical. Should this layer be included in the legends?NA, the default, includes if any aesthetics are mapped.FALSE never includes, andTRUE always includes.It can also be a named logical vector to finely select the aesthetics todisplay.

inherit.aes

IfFALSE, overrides the default aesthetics,rather than combining with them. This is most useful for helper functionsthat define both data and aesthetics and shouldn't inherit behavior fromthe default plot specification, e.g.ggplot2::borders().

ma_fun

The function used to calculate the moving average. Seven options areavailable including: SMA, EMA, WMA, DEMA, ZLEMA, VWMA, and EVWMA. The default isSMA. SeeTTR::SMA() for underlying functions.

n

Number of periods to average over. Must be between 1 andnrow(x), inclusive.

sd

The number of standard deviations to use.

wilder

logical; ifTRUE, a Welles Wilder type EMA will becalculated; see notes.

ratio

A smoothing/decay ratio.ratio overrideswilderinEMA.

v

The 'volume factor' (a number in [0,1]). See Notes.

wts

Vector of weights. Length ofwts vector must equal thelength ofx, orn (the default).

color_ma,color_bands

Select the line color to be applied for the movingaverage line and the Bollinger band line.

alpha

Used to adjust the alpha transparency for the BBand ribbon.

fill

Used to adjust the fill color for the BBand ribbon.

...

Other arguments passed on toggplot2::layer(). These areoften aesthetics, used to set an aesthetic to a fixed value, likecolor = "red" orsize = 3. They may also be parametersto the paired geom/stat.

Aesthetics

The following aesthetics are understood (required are in bold):

See Also

See individual modeling functions for underlying parameters:

Examples

library(dplyr)library(ggplot2)library(lubridate)AAPL <- tq_get("AAPL", from = "2013-01-01", to = "2016-12-31")# SMAAAPL %>%    ggplot(aes(x = date, y = close)) +    geom_line() +           # Plot stock price    geom_bbands(aes(high = high, low = low, close = close), ma_fun = SMA, n = 50) +    coord_x_date(xlim = c(as_date("2016-12-31") - dyears(1), as_date("2016-12-31")),                 ylim = c(20, 35))# EMAAAPL %>%   ggplot(aes(x = date, y = close)) +   geom_line() +           # Plot stock price   geom_bbands(aes(high = high, low = low, close = close),                  ma_fun = EMA, wilder = TRUE, ratio = NULL, n = 50) +   coord_x_date(xlim = c(as_date("2016-12-31") - dyears(1), as_date("2016-12-31")),                ylim = c(20, 35))# VWMAAAPL %>%    ggplot(aes(x = date, y = close)) +    geom_line() +           # Plot stock price    geom_bbands(aes(high = high, low = low, close = close, volume = volume),                   ma_fun = VWMA, n = 50) +    coord_x_date(xlim = c(as_date("2016-12-31") - dyears(1), as_date("2016-12-31")),                ylim = c(20, 35))

Plot Financial Charts in ggplot2

Description

Financial charts provide visual cues to open, high, low, and close prices.Usecoord_x_date() to zoom into specific plot regions.The following financial chart geoms are available:

Usage

geom_barchart(  mapping = NULL,  data = NULL,  stat = "identity",  position = "identity",  na.rm = TRUE,  show.legend = NA,  inherit.aes = TRUE,  colour_up = "darkblue",  colour_down = "red",  fill_up = "darkblue",  fill_down = "red",  ...)geom_candlestick(  mapping = NULL,  data = NULL,  stat = "identity",  position = "identity",  na.rm = TRUE,  show.legend = NA,  inherit.aes = TRUE,  colour_up = "darkblue",  colour_down = "red",  fill_up = "darkblue",  fill_down = "red",  ...)

Arguments

mapping

Set of aesthetic mappings created byggplot2::aes() orggplot2::aes_(). If specified andinherit.aes = TRUE (thedefault), it is combined with the default mapping at the top level of theplot. You must supplymapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

IfNULL, the default, the data is inherited from the plotdata as specified in the call toggplot2::ggplot().

Adata.frame, or other object, will override the plotdata. All objects will be fortified to produce a data frame. Seeggplot2::fortify() for which variables will be created.

Afunction will be called with a single argument,the plot data. The return value must be adata.frame., andwill be used as the layer data.

stat

The statistical transformation to use on the data for this layer.When using a⁠geom_*()⁠ function to construct a layer, thestatargument can be used the override the default coupling between geoms andstats. Thestat argument accepts the following:

  • AStat ggproto subclass, for exampleStatCount.

  • A string naming the stat. To give the stat as a string, strip thefunction name of thestat_ prefix. For example, to usestat_count(),give the stat as"count".

  • For more information and other ways to specify the stat, see thelayer stat documentation.

position

A position adjustment to use on the data for this layer. Thiscan be used in various ways, including to prevent overplotting andimproving the display. Theposition argument accepts the following:

  • The result of calling a position function, such asposition_jitter().This method allows for passing extra arguments to the position.

  • A string naming the position adjustment. To give the position as astring, strip the function name of theposition_ prefix. For example,to useposition_jitter(), give the position as"jitter".

  • For more information and other ways to specify the position, see thelayer position documentation.

na.rm

IfTRUE, silently removesNA values, whichtypically desired for moving averages.

show.legend

logical. Should this layer be included in the legends?NA, the default, includes if any aesthetics are mapped.FALSE never includes, andTRUE always includes.It can also be a named logical vector to finely select the aesthetics todisplay.

inherit.aes

IfFALSE, overrides the default aesthetics,rather than combining with them. This is most useful for helper functionsthat define both data and aesthetics and shouldn't inherit behavior fromthe default plot specification, e.g.ggplot2::borders().

colour_up,colour_down

Select colors to be applied based on price movementfrom open to close. Ifclose >= open,colour_up is used. Otherwise,colour_down is used. The default is"darkblue" and"red", respectively.

fill_up,fill_down

Select fills to be applied based on price movementfrom open to close. If close >= open,fill_up is used. Otherwise,fill_down is used. The default is"darkblue" and "red", respectively.Only affectsgeom_candlestick().

...

Other arguments passed on toggplot2::layer(). These areoften aesthetics, used to set an aesthetic to a fixed value, likecolor = "red" orsize = 3. They may also be parametersto the paired geom/stat.

Aesthetics

The following aesthetics are understood (required are in bold):

See Also

See individual modeling functions for underlying parameters:

Examples

library(dplyr)library(ggplot2)library(lubridate)AAPL <- tq_get("AAPL", from = "2013-01-01", to = "2016-12-31")# Bar ChartAAPL %>%    ggplot(aes(x = date, y = close)) +    geom_barchart(aes(open = open, high = high, low = low, close = close)) +    geom_ma(color = "darkgreen") +    coord_x_date(xlim = c("2016-01-01", "2016-12-31"),                 ylim = c(20, 30))# Candlestick ChartAAPL %>%    ggplot(aes(x = date, y = close)) +    geom_candlestick(aes(open = open, high = high, low = low, close = close)) +    geom_ma(color = "darkgreen") +    coord_x_date(xlim = c("2016-01-01", "2016-12-31"),                 ylim = c(20, 30))

Plot moving averages

Description

The underlying moving average functions used are specified inTTR::SMA()from the TTR package. Usecoord_x_date() to zoom into specific plot regions.The following moving averages are available:

Usage

geom_ma(  mapping = NULL,  data = NULL,  position = "identity",  na.rm = TRUE,  show.legend = NA,  inherit.aes = TRUE,  ma_fun = SMA,  n = 20,  wilder = FALSE,  ratio = NULL,  v = 1,  wts = 1:n,  ...)geom_ma_(  mapping = NULL,  data = NULL,  position = "identity",  na.rm = TRUE,  show.legend = NA,  inherit.aes = TRUE,  ma_fun = "SMA",  n = 20,  wilder = FALSE,  ratio = NULL,  v = 1,  wts = 1:n,  ...)

Arguments

mapping

Set of aesthetic mappings created byggplot2::aes() orggplot2::aes_(). If specified andinherit.aes = TRUE (thedefault), it is combined with the default mapping at the top level of theplot. You must supplymapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

IfNULL, the default, the data is inherited from the plotdata as specified in the call toggplot2::ggplot().

Adata.frame, or other object, will override the plotdata. All objects will be fortified to produce a data frame. Seeggplot2::fortify() for which variables will be created.

Afunction will be called with a single argument,the plot data. The return value must be adata.frame., andwill be used as the layer data.

position

A position adjustment to use on the data for this layer. Thiscan be used in various ways, including to prevent overplotting andimproving the display. Theposition argument accepts the following:

  • The result of calling a position function, such asposition_jitter().This method allows for passing extra arguments to the position.

  • A string naming the position adjustment. To give the position as astring, strip the function name of theposition_ prefix. For example,to useposition_jitter(), give the position as"jitter".

  • For more information and other ways to specify the position, see thelayer position documentation.

na.rm

IfTRUE, silently removesNA values, whichtypically desired for moving averages.

show.legend

logical. Should this layer be included in the legends?NA, the default, includes if any aesthetics are mapped.FALSE never includes, andTRUE always includes.It can also be a named logical vector to finely select the aesthetics todisplay.

inherit.aes

IfFALSE, overrides the default aesthetics,rather than combining with them. This is most useful for helper functionsthat define both data and aesthetics and shouldn't inherit behavior fromthe default plot specification, e.g.ggplot2::borders().

ma_fun

The function used to calculate the moving average. Seven options areavailable including: SMA, EMA, WMA, DEMA, ZLEMA, VWMA, and EVWMA. The default isSMA. SeeTTR::SMA() for underlying functions.

n

Number of periods to average over. Must be between 1 andnrow(x), inclusive.

wilder

logical; ifTRUE, a Welles Wilder type EMA will becalculated; see notes.

ratio

A smoothing/decay ratio.ratio overrideswilderinEMA.

v

The 'volume factor' (a number in [0,1]). See Notes.

wts

Vector of weights. Length ofwts vector must equal thelength ofx, orn (the default).

...

Other arguments passed on toggplot2::layer(). These areoften aesthetics, used to set an aesthetic to a fixed value, likecolor = "red" orsize = 3. They may also be parametersto the paired geom/stat.

Aesthetics

The following aesthetics are understood (required are in bold):

See Also

See individual modeling functions for underlying parameters:

Examples

library(dplyr)library(ggplot2)AAPL <- tq_get("AAPL", from = "2013-01-01", to = "2016-12-31")# SMAAAPL %>%    ggplot(aes(x = date, y = adjusted)) +    geom_line() +                         # Plot stock price    geom_ma(ma_fun = SMA, n = 50) +                 # Plot 50-day SMA    geom_ma(ma_fun = SMA, n = 200, color = "red") + # Plot 200-day SMA    coord_x_date(xlim = c("2016-01-01", "2016-12-31"),                 ylim = c(20, 30))                     # Zoom in# EVWMAAAPL %>%    ggplot(aes(x = date, y = adjusted)) +    geom_line() +                                                   # Plot stock price    geom_ma(aes(volume = volume), ma_fun = EVWMA, n = 50) +   # Plot 50-day EVWMA    coord_x_date(xlim = c("2016-01-01", "2016-12-31"),                 ylim = c(20, 30))                                  # Zoom in

tidyquant palettes for use with scales

Description

These palettes are mainly called internally by tidyquant⁠scale_*_tq()⁠ functions.

Usage

palette_light()palette_dark()palette_green()

Examples

library(scales)scales::show_col(palette_light())

Query or set Quandl API Key

Description

Query or set Quandl API Key

Usage

quandl_api_key(api_key)

Arguments

api_key

Optionally passed parameter to set Quandlapi_key.

Details

A wrapper forQuandl::Quandl.api_key()

Value

Returns invisibly the currently setapi_key

See Also

tq_get()get = "quandl"

Examples

## Not run: if (rlang::is_installed("Quandl")) {quandl_api_key(api_key = "foobar")}## End(Not run)

Description

Search the Quandl database

Usage

quandl_search(query, silent = FALSE, per_page = 10, ...)

Arguments

query

Search terms

silent

Prints the results when FALSE.

per_page

Number of results returned per page.

...

Additional named values that are interpretted as Quandl API parameters.

Details

A wrapper forQuandl::Quandl.search()

Value

Returns a tibble with search results.

See Also

tq_get()get = "quandl"

Examples

## Not run: quandl_search(query = "oil")## End(Not run)

tidyquant colors and fills for ggplot2.

Description

The tidyquant scales add colors that work nicely withtheme_tq().

Usage

scale_color_tq(..., theme = "light")scale_colour_tq(..., theme = "light")scale_fill_tq(..., theme = "light")

Arguments

...

common parameters forscale_color_manual() orscale_fill_manual():name,breaks,labels,na.value,limits andguide.

theme

one of "light", "dark", or "green". This should match thetheme_tq() that is used with it.

Details

scale_color_tq

For use whencolor is specified as anaes() in a ggplot.

scale_fill_tq

For use whenfill is specified as anaes() in a ggplot.

See Also

theme_tq()

Examples

# Load librarieslibrary(dplyr)library(ggplot2)# Get stock pricesstocks <- c("AAPL", "META", "NFLX") %>%    tq_get(from = "2013-01-01",           to   = "2017-01-01")# Plot for stocksg <- stocks %>%    ggplot(aes(date, adjusted, color = symbol)) +    geom_line() +    labs(title = "Multi stock example",         xlab = "Date",         ylab = "Adjusted Close")# Plot with tidyquant theme and colorsg +    theme_tq() +    scale_color_tq()

tidyquant themes for ggplot2.

Description

Thetheme_tq() function creates a custom theme using tidyquant colors.

Usage

theme_tq(base_size = 11, base_family = "")theme_tq_dark(base_size = 11, base_family = "")theme_tq_green(base_size = 11, base_family = "")

Arguments

base_size

base font size, given in pts.

base_family

base font family

See Also

scale_manual()

Examples

# Load librarieslibrary(dplyr)library(ggplot2)# Get stock pricesAAPL <- tq_get("AAPL", from = "2013-01-01", to = "2016-12-31")# Plot using ggplot with theme_tqAAPL %>% ggplot(aes(x = date, y = close)) +       geom_line() +       geom_bbands(aes(high = high, low = low, close = close),                   ma_fun = EMA,                   wilder = TRUE,                   ratio = NULL,                   n = 50) +       coord_x_date(xlim = c("2016-01-01", "2016-12-31"),                 ylim = c(20, 35)) +       labs(title = "Apple BBands",            x = "Date",            y = "Price") +       theme_tq()

Conflicts between the tidyquant and other packages

Description

This function lists all the conflicts between packages in the tidyverseand other packages that you have loaded.

Usage

tidyquant_conflicts(only = NULL)

Arguments

only

Set this to a character vector to restrict to conflicts onlywith these packages.

Details

There are four conflicts that are deliberately ignored:intersect,union,setequal, andsetdiff from dplyr. These functionsmake the base equivalents generic, so shouldn't negatively affect anyexisting code.

Examples

tidyquant_conflicts()

Set Tiingo API Key

Description

Requires the riingo package to be installed.

Usage

tiingo_api_key(api_key)

Arguments

api_key

Optionally passed parameter to set Tiingoapi_key.

Details

A wrapper forriingo::ringo_set_token()

Value

Returns invisibly the currently setapi_key

See Also

tq_get()get = "tiingo"

Examples

## Not run:   tiingo_api_key(api_key = "foobar")## End(Not run)

Get quantitative data intibble format

Description

Get quantitative data intibble format

Usage

tq_get(x, get = "stock.prices", complete_cases = TRUE, ...)tq_get_options()

Arguments

x

A single character string, a character vector or tibble representing a single (or multiple)stock symbol, metal symbol, currency combination, FRED code, etc.

get

A character string representing the type of data to getforx. Options include:

  • "stock.prices": Get the open, high, low, close, volume and adjustedstock prices for a stock symbol fromYahoo Finance (https://finance.yahoo.com/). Wrapper forquantmod::getSymbols().

  • "dividends": Get the dividends for a stock symbol fromYahoo Finance (https://finance.yahoo.com/). Wrapper forquantmod::getDividends().

  • "splits": Get the split ratio for a stock symbol fromYahoo Finance (https://finance.yahoo.com/). Wrapper forquantmod::getSplits().

  • "stock.prices.japan": Get the open, high, low, close, volume and adjustedstock prices for a stock symbol fromYahoo Finance Japan. Wrapper forquantmod::getSymbols.yahooj().

  • "economic.data": Get economic data fromFRED. rapper forquantmod::getSymbols.FRED().

  • "quandl": Get data sets fromQuandl. Wrapper forQuandl::Quandl().See alsoquandl_api_key().

  • "quandl.datatable": Get data tables fromQuandl. Wrapper forQuandl::Quandl.datatable().See alsoquandl_api_key().

  • "tiingo": Get data sets fromTingo (https://www.tiingo.com/). Wrapper forriingo::riingo_prices().See alsotiingo_api_key().

  • "tiingo.iex": Get data sets fromTingo (https://www.tiingo.com/). Wrapper forriingo::riingo_iex_prices().See alsotiingo_api_key().

  • "tiingo.crypto": Get data sets fromTingo (https://www.tiingo.com/). Wrapper forriingo::riingo_crypto_prices().See alsotiingo_api_key().

  • "alphavantager": Get data sets fromAlpha Vantage. Wrapper foralphavantager::av_get().See alsoav_api_key().

  • "rblpapi": Get data sets fromBloomberg. Wrapper forRblpapi.See alsoRblpapi::blpConnect() to connect to Bloomberg terminal (required).Use the argumentrblpapi_fun to set the function such as "bdh" (default), "bds", or "bdp".

complete_cases

Removes symbols that return an NA value due to an error with the getcall such as sending an incorrect symbol "XYZ" to get = "stock.prices". This is useful inscaling so user does not need toadd an extra step to remove these rows.TRUE by default, and a warningmessage is generated for any rows removed.

...

Additional parameters passed to the "wrapped"function. Investigate underlying functions to see full list of arguments.Common optional parameters include:

  • from: Standardized for time series functions inquantmod,quandl,tiingo,alphavantager packages.A character string representing a start date inYYYY-MM-DD format.

  • to: Standardized for time series functions inquantmod,quandl,tiingo,alphavantager packages.A character string representing a end date inYYYY-MM-DD format.

Details

tq_get() is a consolidated function that gets data from variousweb sources. The function is a wrapper for severalquantmodfunctions,Quandl functions, and also gets data from websources unavailablein other packages.The results are always returned as atibble. The advantagesare (1) only one function is needed for all data sources and (2) the functioncan be seamlessly used with the tidyverse:purrr,tidyr, anddplyr verbs.

tq_get_options() returns a list of validget options you canchoose from.

tq_get_stock_index_options() Is deprecated and will be removed in thenext version. Please usetq_index_options() instead.

Value

Returns data in the form of atibble object.

See Also

Examples

# Load libraries# Get the list of `get` optionstq_get_options()# Get stock prices for a stock from Yahooaapl_stock_prices <- tq_get("AAPL")# Get stock prices for multiple stocksmult_stocks <- tq_get(c("META", "AMZN"),                      get  = "stock.prices",                      from = "2016-01-01",                      to   = "2017-01-01")## Not run: # --- Quandl ---if (rlang::is_installed("quandl")) {quandl_api_key('<your_api_key>')tq_get("EIA/PET_MTTIMUS1_M", get = "quandl", from = "2010-01-01")}# Energy data from EIA# --- Tiingo ---if (rlang::is_installed("riingo")) {tiingo_api_key('<your_api_key>')# Tiingo Prices (Free alternative to Yahoo Finance!)tq_get(c("AAPL", "GOOG"), get = "tiingo", from = "2010-01-01")# Sub-daily prices from IEX ----tq_get(c("AAPL", "GOOG"),       get = "tiingo.iex",       from   = "2020-01-01",       to     = "2020-01-15",       resample_frequency = "5min")# Tiingo Bitcoin Prices ----tq_get(c("btcusd", "btceur"),       get    = "tiingo.crypto",       from   = "2020-01-01",       to     = "2020-01-15",       resample_frequency = "5min")}# --- Alpha Vantage ---if (rlang::is_installed("alphavantager")) {av_api_key('<your_api_key>')# Daily Time Seriestq_get("AAPL",       get        = "alphavantager",       av_fun     = "TIME_SERIES_DAILY_ADJUSTED",       outputsize = "full")# Intraday 15 Min Intervaltq_get("AAPL",       get        = "alphavantage",       av_fun     = "TIME_SERIES_INTRADAY",       interval   = "15min",       outputsize = "full")# FX DAILYtq_get("USD/EUR", get = "alphavantage", av_fun = "FX_DAILY", outputsize = "full")# FX REAL-TIME QUOTEtq_get("USD/EUR", get = "alphavantage", av_fun = "CURRENCY_EXCHANGE_RATE")}## End(Not run)

Get all stocks in a stock index or stock exchange intibble format

Description

Get all stocks in a stock index or stock exchange intibble format

Usage

tq_index(x, use_fallback = FALSE)tq_index_options()tq_exchange(x)tq_exchange_options()tq_fund_holdings(x, source = "SSGA")tq_fund_source_options()

Arguments

x

A single character string, a character vector or tibble representing asingle stock index or multiple stock indexes.

use_fallback

A boolean that can be used to return a fallback data setlast downloaded when the package was updated. Useful if the website is down.Set toFALSE by default.

source

The API source to use.

Details

tq_index() returns the stock symbol, company name, weight, and sector of every stockin an index.

tq_index_options() returns a list of stock indexes you canchoose from.

tq_exchange() returns the stock symbol, company, last sale price,market capitalization, sector and industry of every stockin an exchange. Three stock exchanges are available (AMEX, NASDAQ, and NYSE).

tq_exchange_options() returns a list of stock exchanges you canchoose from. The options are AMEX, NASDAQ and NYSE.

tq_fund_holdings() returns the the stock symbol, company name, weight, and sector of every stockin an fund. Thesource parameter specifies which investment management company to use.Example:source = "SSGA" connects to State Street Global Advisors (SSGA).Ifx = "SPY", then SPDR SPY ETF holdings will be returned.

tq_fund_source_options(): returns the options that can be used for thesource API fortq_fund_holdings().

Value

Returns data in the form of atibble object.

See Also

tq_get() to get stock prices, financials, key stats, etc using the stock symbols.

Examples

# Stock Indexes:# Get the list of stock index optionstq_index_options()# Get all stock symbols in a stock index## Not run: tq_index("DOW")## End(Not run)# Stock Exchanges:# Get the list of stock exchange optionstq_exchange_options()# Get all stocks in a stock exchange## Not run: tq_exchange("NYSE")## End(Not run)# Mutual Funds and ETFs:# Get the list of stock exchange optionstq_fund_source_options()# Get all stocks in a fund## Not run: tq_fund_holdings("SPY", source = "SSGA")## End(Not run)

Mutates quantitative data

Description

tq_mutate() adds new variables to an existing tibble;tq_transmute() returns only newly created columns and is typicallyused when periodicity changes

Usage

tq_mutate(  data,  select = NULL,  mutate_fun,  col_rename = NULL,  ohlc_fun = NULL,  ...)tq_mutate_(data, select = NULL, mutate_fun, col_rename = NULL, ...)tq_mutate_xy(data, x, y = NULL, mutate_fun, col_rename = NULL, ...)tq_mutate_xy_(data, x, y = NULL, mutate_fun, col_rename = NULL, ...)tq_mutate_fun_options()tq_transmute(  data,  select = NULL,  mutate_fun,  col_rename = NULL,  ohlc_fun = NULL,  ...)tq_transmute_(data, select = NULL, mutate_fun, col_rename = NULL, ...)tq_transmute_xy(data, x, y = NULL, mutate_fun, col_rename = NULL, ...)tq_transmute_xy_(data, x, y = NULL, mutate_fun, col_rename = NULL, ...)tq_transmute_fun_options()

Arguments

data

Atibble (tidy data frame) of data typically fromtq_get().

select

The columns to send to the mutation function.

mutate_fun

The mutation function from either thexts,quantmod, orTTR package. Executetq_mutate_fun_options()to see the full list of options by package.

col_rename

A string or character vector containing names that can be usedto quickly rename columns.

ohlc_fun

Deprecated. Useselect.

...

Additional parameters passed to the appropriate mutatationfunction.

x,y

Parameters used with⁠_xy⁠ that consist of column names of variablesto be passed to the mutatation function (instead of OHLC functions).

Details

tq_mutate andtq_transmute are very flexible wrappers for variousxts,quantmod andTTR functions. The main advantage is theresults are returned as atibble and thefunction can be used with thetidyverse.tq_mutate is used when additionalcolumns are added to the return data frame.tq_transmute works exactly liketq_mutateexcept it only returns the newly created columns. This is helpful whenchanging periodicity where the new columns would not have the same number of rowsas the original tibble.

select specifies the columns that get passed to the mutation function. Select worksas a more flexible version of the OHLC extractor functions fromquantmod wherenon-OHLC data works as well. Whenselect isNULL, all columns are selected.In Example 1 below,close returns the "close" price and sends this to themutate function,periodReturn.

mutate_fun is the function that performs the work. In Example 1, thisisperiodReturn, which calculates the period returns. The...are additional arguments passed to themutate_fun. Think ofthe whole operation in Example 1 as the close price, obtained byselect = close,being sent to theperiodReturn function alongwith additional arguments defining how to perform the period return, whichincludesperiod = "daily" andtype = "log".Example 4 shows how to apply a rolling regression.

tq_mutate_xy andtq_transmute_xy are designed to enable working with mutatationfunctions that require two primary inputs (e.g. EVWMA, VWAP, etc).Example 2 shows this benefit in action: using the EVWMA function that usesvolume to define the moving average period.

tq_mutate_,tq_mutate_xy_,tq_transmute_, andtq_transmute_xy_are setup for Non-StandardEvaluation (NSE). This enables programatically changing column names by modifyingthe text representations. Example 5 shows the difference in implementation.Note that character strings are being passed to the variables instead ofunquoted variable names. Seevignette("nse") for more information.

tq_mutate_fun_options andtq_transmute_fun_options return a list of variousfinancial functions that are compatible withtq_mutate andtq_transmute,respectively.

Value

Returns mutated data in the form of atibble object.

See Also

tq_get()

Examples

# Load librarieslibrary(dplyr)##### Basic Functionalityfb_stock_prices  <- tidyquant::FANG %>%    filter(symbol == "META") %>%        filter(            date >= "2016-01-01",            date <= "2016-12-31"        )goog_stock_prices  <- FANG %>%    filter(symbol == "GOOG") %>%        filter(            date >= "2016-01-01",            date <= "2016-12-31"        )# Example 1: Return logarithmic daily returns using periodReturn()fb_stock_prices %>%    tq_mutate(select = close, mutate_fun = periodReturn,              period = "daily", type = "log")# Example 2: Use tq_mutate_xy to use functions with two columns requiredfb_stock_prices %>%    tq_mutate_xy(x = close, y = volume, mutate_fun = EVWMA,                 col_rename = "EVWMA")# Example 3: Using tq_mutate to work with non-OHLC datatq_get("DCOILWTICO", get = "economic.data") %>%    tq_mutate(select = price, mutate_fun = lag.xts, k = 1, na.pad = TRUE)# Example 4: Using tq_mutate to apply a rolling regressionfb_returns <- fb_stock_prices %>%    tq_transmute(adjusted, periodReturn, period = "monthly", col_rename = "fb.returns")goog_returns <- goog_stock_prices %>%    tq_transmute(adjusted, periodReturn, period = "monthly", col_rename = "goog.returns")returns_combined <- left_join(fb_returns, goog_returns, by = "date")regr_fun <- function(data) {    coef(lm(fb.returns ~ goog.returns, data = as_tibble(data)))}returns_combined %>%    tq_mutate(mutate_fun = rollapply,              width      = 6,              FUN        = regr_fun,              by.column  = FALSE,              col_rename = c("coef.0", "coef.1"))# Example 5: Non-standard evaluation:# Programming with tq_mutate_() and tq_mutate_xy_()col_name <- "adjusted"mutate <- c("MACD", "SMA")tq_mutate_xy_(fb_stock_prices, x = col_name, mutate_fun = mutate[[1]])

Computes a wide variety of summary performance metrics from stock or portfolio returns

Description

Asset and portfolio performance analysis is a deep field with a wide range of theories andmethods for analyzing risk versus reward. ThePerformanceAnalytics packageconsolidates many of the most widely used performance metrics as functions that canbe applied to stock or portfolio returns.tq_performanceimplements these performance analysis functions in a tidy way, enabling scalinganalysis using the split, apply, combine framework.

Usage

tq_performance(data, Ra, Rb = NULL, performance_fun, ...)tq_performance_(data, Ra, Rb = NULL, performance_fun, ...)tq_performance_fun_options()

Arguments

data

Atibble (tidy data frame) of returns in tidy format (i.e long format).

Ra

The column of asset returns

Rb

The column of baseline returns (for functions that require comparison to a baseline)

performance_fun

The performance function fromPerformanceAnalytics. Seetq_performance_fun_options() for a complete list of integrated functions.

...

Additional parameters passed to thePerformanceAnalytics function.

Details

Important concept: Performance is based on the statistical properties of returns,and as a result this function uses stock or portfolio returns as opposedto stock prices.

tq_performance is a wrapper for variousPerformanceAnalytics functionsthat return portfolio statistics.The main advantage is the ability to scale with thetidyverse.

Ra andRb are the columns containing asset and baseline returns, respectively.These columns are mapped to thePerformanceAnalytics functions. Note thatRbis not always required, and in these instances the argument defaults toRb = NULL.The user can tell ifRb is required by researching the underlying performance function.

... are additional arguments that are passed to thePerformanceAnalyticsfunction. Search the underlying function to see what arguments can be passed through.

tq_performance_fun_options returns a list of compatiblePerformanceAnalytics functionsthat can be supplied to theperformance_fun argument.

Value

Returns data in the form of atibble object.

See Also

Examples

# Load librarieslibrary(dplyr)# Use FANG data set# Get returns for individual stock components grouped by symbolRa <- FANG %>%    group_by(symbol) %>%    tq_transmute(adjusted, periodReturn, period = "monthly", col_rename = "Ra")# Get returns for SP500 as baselineRb <- "^GSPC" %>%    tq_get(get  = "stock.prices",           from = "2010-01-01",           to   = "2015-12-31") %>%    tq_transmute(adjusted, periodReturn, period = "monthly", col_rename = "Rb")# Merge stock returns with baselineRaRb <- left_join(Ra, Rb, by = c("date" = "date"))##### Performance Metrics ###### View optionstq_performance_fun_options()# Get performance metricsRaRb %>%    tq_performance(Ra = Ra, performance_fun = SharpeRatio, p = 0.95)RaRb %>%    tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM)

Aggregates a group of returns by asset into portfolio returns

Description

Aggregates a group of returns by asset into portfolio returns

Usage

tq_portfolio(  data,  assets_col,  returns_col,  weights = NULL,  col_rename = NULL,  ...)tq_portfolio_(  data,  assets_col,  returns_col,  weights = NULL,  col_rename = NULL,  ...)tq_repeat_df(data, n, index_col_name = "portfolio")

Arguments

data

Atibble (tidy data frame) of returns in tidy format (i.e long format).

assets_col

The column with assets (securities)

returns_col

The column with returns

weights

Optional parameter for the asset weights, which can be passed as a numeric vector the length ofthe number of assets or a two column tibble with asset names in first columnand weights in second column.

col_rename

A string or character vector containing names that can be usedto quickly rename columns.

...

Additional parameters passed toPerformanceAnalytics::Return.portfolio

n

Number of times to repeat a data frame row-wise.

index_col_name

A renaming function for the "index" column, used when repeating data frames.

Details

tq_portfolio is a wrapper forPerformanceAnalytics::Return.portfolio.The main advantage is the results are returned as atibble and thefunction can be used with thetidyverse.

assets_col andreturns_col are columns withindata that are usedto compute returns for a portfolio. The columns should be in "long" format (or "tidy" format)meaning there is only one column containing all of the assets and one column containingall of the return values (i.e. not in "wide" format with returns spread by asset).

weights are the weights to be applied to the asset returns.Weights can be input in one of three options:

tq_repeat_df is a simple function that repeatsa data framen times row-wise (long-wise), and adds a new column for a portfolio index.The function is used to assist in Multiple Portfolio analyses, andis a useful precursor totq_portfolio.

Value

Returns data in the form of atibble object.

See Also

Examples

# Load librarieslibrary(dplyr)# Use FANG data set# Get returns for individual stock componentsmonthly_returns_stocks <- FANG %>%    group_by(symbol) %>%    tq_transmute(adjusted, periodReturn, period = "monthly")##### Portfolio Aggregation Methods ###### Method 1: Use tq_portfolio with numeric vector of weightsweights <- c(0.50, 0.25, 0.25, 0)tq_portfolio(data = monthly_returns_stocks,             assets_col = symbol,             returns_col = monthly.returns,             weights = weights,             col_rename = NULL,             wealth.index = FALSE)# Method 2: Use tq_portfolio with two column tibble and map weights# Note that GOOG's weighting is zero in Method 1. In Method 2,# GOOG is not added and same result is achieved.weights_df <- tibble(symbol = c("META", "AMZN", "NFLX"),                     weights = c(0.50, 0.25, 0.25))tq_portfolio(data = monthly_returns_stocks,             assets_col = symbol,             returns_col = monthly.returns,             weights = weights_df,             col_rename = NULL,             wealth.index = FALSE)# Method 3: Working with multiple portfolios# 3A: Duplicate monthly_returns_stocks multiple timesmult_monthly_returns_stocks <- tq_repeat_df(monthly_returns_stocks, n = 4)# 3B: Create weights table grouped by portfolio idweights <- c(0.50, 0.25, 0.25, 0.00,             0.00, 0.50, 0.25, 0.25,             0.25, 0.00, 0.50, 0.25,             0.25, 0.25, 0.00, 0.50)stocks <- c("META", "AMZN", "NFLX", "GOOG")weights_table <- tibble(stocks) %>%    tq_repeat_df(n = 4) %>%    bind_cols(tibble(weights)) %>%    group_by(portfolio)# 3C: Scale to multiple portfoliostq_portfolio(data = mult_monthly_returns_stocks,             assets_col = symbol,             returns_col = monthly.returns,             weights = weights_table,             col_rename = NULL,             wealth.index = FALSE)

[8]ページ先頭

©2009-2025 Movatter.jp