rets_calc: Returns Calculation

Description Usage Arguments Value Examples

View source: R/data-clean.R

Description

Calculates the returns from a prices dataset.

Usage

1
rets_calc(prices, type = "d")

Arguments

prices

a numerical vector or an nxp data matrix with stock prices.

type

a character vector, indicating which type of returns are to be calculated. type="d" calculates the discrete returns (default) and type="c" calculates the continuous returns.

Value

a numerical vector or an (n-1)xp data matrix with returns.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
data(sp500)
sp500[,1] <- as.Date(sp500[,1],format="%d.%m.%Y",stringsAsFactors=FALSE)
sp500 <- sp500[,-which(substr(colnames(sp500),1,7)=="X.ERROR")]
NYSE_hol <- as.Date(timeDate::holidayNYSE(as.numeric(unique(format(sp500[,1],format="%Y")))))
no_trades <- sort(c(NYSE_hol, as.Date(c("2001-09-11","2001-09-12","2001-09-13","2001-09-14"),
format="%Y-%m-%d")))
sp500 <- sp500[-match(no_trades,sp500[,1]),]
nonas <- which(apply(is.na(sp500[,-1]),2,sum)==0)
sp500 <- sp500[,c(1, nonas+1)]
repindex <- find_repval(sp500[,-1])
sp500_prices <- sp500[,-c(1, repindex+1)]
sp500_ret <- rets_calc(sp500_prices)

antshi/auxPort documentation built on Oct. 27, 2020, 1:16 p.m.