R/fun_GetVolAdReturn.R

#' fun_GetVolAdReturn
#'
#' @param df Dataframe with columns price, mean and sd
#' @param lag number of lags between period
#'
#' @return Dataframe with columns price, mean, sd, reVol, reVolLag and reSingLag. reVol is return divided by last period sd. reVolLag the same as but lagged the specified number of times and reSingLag is the sing of the lag.
#'
#' @export
#'
fun_GetVolAdReturn <- function(df, lag) {
  out.df <- cbind(df,
                  reVol = c(NA, diff(df)[,1] / df[-nrow(df), 3]))
  out.df <- cbind(out.df,
                  nameVol = as.numeric(c(rep(NA, lag), out.df[-c((nrow(out.df)-lag+1):nrow(out.df)),4])))
  out.df <- cbind(out.df,
                  nameSign = sign(out.df[,5]))
  colnames(out.df) <- c(colnames(df)[1], colnames(df)[2], colnames(df)[3],
                        "reVol", paste0("reVolLag", lag), paste0("reSingLag", lag))
  return(out.df)
}
3schwartz/SpecialeScrAndFun documentation built on May 4, 2019, 6:29 a.m.