R/get_rates_from_prices.R
get_rates_from_prices.Rd
get_rates_from_prices
is used for computing rates of return from
prices for different classes.
get_rates_from_prices( prices, quote = c("Open", "Close"), multi_day = TRUE, compounding = c("discrete", "continuous") )
prices | an object containing prices of securities. Three classes are
allowed: |
---|---|
quote | a character vector specifying the type of the quote:
|
multi_day | logical, is a rate of return between more than 1 day is allowed? |
compounding | a character vector defining the type of compounding:
|
Rates of returns of the same class as prices.
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.
## Download historical prices of seven companies' stocks and estimate rates ## of returns form prices: if (FALSE) { 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") } ## 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: if (FALSE) { 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") } ## The result of the above code is stored in: data(rates_indx)