#' mutate_f function
#'
#' mutate/add new columns (chg.. = last./ref.)
#'
#' Dependencies: library0("plyr", "dplyr"), closemod.v.f (this package)
#' Return df (output: final.df)
#' @importFrom magrittr %>%
#' @param df default to clean.df
#' @keywords mutate select new columns
#' @export
#' @examples
#' mutateselect.f(df= clean.df)
mutate_f <- function(df = clean_df){
#filter delisted
max_tolerance_date <- max(clean_df[,"date"])-20
df <- df %>% dplyr::group_by(code.) %>% dplyr::mutate(status2 = max(date) >= max_tolerance_date)
df1 <- df %>% dplyr::filter(status2) %>% dplyr::select(-status2)
#modify chg..
df1 <- df1 %>% dplyr::group_by(code.) %>% dplyr::mutate(chg.. = last./ref.-1)
#add lastmod
df1 <- df1 %>% dplyr::group_by(code.) %>% dplyr::mutate(lastmod = closemod.v.f(chg..))
return(as.data.frame(df1))
}
#' Sub-function: closemod.v.f
#'
#' modify closing price using return data, i.e. all stocks start from closing price of 1.00 and the next closing price is the product of previous closing price and (1+return)
#' @param return.v return.v
#' @keywords closemod
#' @export
#'
closemod.v.f <- function(return.v){
L <- length(return.v)
closemod.v <- sapply(1:L, FUN = function(x){
if(x == 1){
return(1)
}else{
prod(1+return.v[2:x])
}
})
# closemod.v <- formattable(closemod.v, digits = 3, format = "f")
return(closemod.v)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.