R/calculate_index.R

Defines functions calculate_index

#' Calculate a Price-Weighted Market Index From a List of Asset Prices
#'
#' @param OHLC_list a list containing OHLC prices
#' @param weights numeric value indicating asset weights
#'
#' @return returns OHLC object with index price
#' @export
#' @import xts
#' @import quantmod
#' @import magrittr
#'
#'
#' @examples OHLC_list <- getSymbols(c("FB", "AAPL", "NFLX", "GOOG"))
#'     calculate_index(OHLC_list)
#'
calculate_index <- function(OHLC_list, weights = NULL) {
  ohlc_tmp <- do.call(cbind.xts, OHLC_list) %>%
    na.approx()

  cbind.xts(
    xts(rowMeans(Op(ohlc_tmp), na.rm = TRUE), index(Op(ohlc_tmp))),
    xts(rowMeans(Hi(ohlc_tmp), na.rm = TRUE), index(Hi(ohlc_tmp))),
    xts(rowMeans(Lo(ohlc_tmp), na.rm = TRUE), index(Lo(ohlc_tmp))),
    xts(rowMeans(Cl(ohlc_tmp), na.rm = TRUE), index(Cl(ohlc_tmp)))
  ) %>%
    `colnames<-`(c("Open", "High", "Low", "Close")) %>%
    OHLC(.)
}
rengelke/quantTraiding_trato documentation built on Oct. 13, 2020, 12:01 p.m.