#' 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(.)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.