get_rates_from_prices: Calculate rates of return for given prices.

View source: R/get_rates_from_prices.R

get_rates_from_pricesR Documentation

Calculate rates of return for given prices.

Description

get_rates_from_prices is used for computing rates of return from prices for different classes.

Usage

get_rates_from_prices(
  prices,
  quote = c("Open", "Close"),
  multi_day = TRUE,
  compounding = c("discrete", "continuous")
)

Arguments

prices

an object containing prices of securities. Three classes are allowed: list, data.frame, and zoo.

quote

a character vector specifying the type of the quote: "Open" (default) or "Close".

multi_day

logical, is a rate of return between more than 1 day is allowed?

compounding

a character vector defining the type of compounding: "continuous" (default) or "discrete".

Details

This is a generic function, dispatched for such classes as list, data.frame, and zoo that represent prices.

The calculation is made in C++ (Rcpp) in favor of speed.

If prices is a data frame, than the first column should be of the class Date and contain ordered dates of prices.

The correspondence between dates and values of the rates depends on the quote, which can be either Open or Close. If the quote is Open, than the value of rate belongs to the former date. Otherwise, to the latter one. This is also applied for the algorithm, if multiday is allowed: the value of the rate of return is assigned to the latter day in case of Close price, and to the former day in in case of Open quote.

The multi_day parameter specifies how to handle missing values and weekends. If the value is TRUE, the function ignores missing values and the rates are calculated between non-missing prices. If it is FALSE, then only one-day period rates of return are computed (between two consecutive calendar dates).

The function uses either continuous (by default) or discrete (periodic) compounding.

Value

Rates of returns of the same class as prices.

Examples

## Download historical prices of seven companies' stocks and estimate rates
## of returns form prices:
## Not run: 
library("magrittr")
tickers <- c("AMZN", "ZM", "UBER", "NFLX", "SHOP", "FB", "UPWK")
rates <- tickers %>%
    get_prices_from_tickers(start = as.Date("2019-04-01"),
                            end = as.Date("2020-04-01"),
                            quote = "Close",
                            retclass = "zoo") %>%
    get_rates_from_prices(quote = "Close",
                          multi_day = TRUE,
                          compounding = "continuous")

## End(Not run)
## The result of the above code is stored in:
data(rates)

## Download historical prices of S&P 500 index and estimate rates of
## returns from prices:
## Not run: 
library("magrittr")
rates_indx <- get_prices_from_tickers("^GSPC",
                                      start = as.Date("2019-04-01"),
                                      end = as.Date("2020-04-01"),
                                      quote = "Close",
                                      retclass = "zoo") %>%
    get_rates_from_prices(quote = "Close",
                          multi_day = TRUE,
                          compounding = "continuous")

## End(Not run)
## The result of the above code is stored in:
data(rates_indx)


irudnyts/estudy2 documentation built on April 21, 2022, 10:50 p.m.