R/read_prices.R

Defines functions read_prices

Documented in read_prices

#' Read Historical Price Data From File
#'
#' @param file the name of the file which the data are to be read from
#' @param src character string specifying price source
#'
#' @return returns an xts object containing historical price data for specified symbol
#' @export
#'
#' @import xts
#' @importFrom magrittr %<>%
#'
#' @examples read_prices("WFWIKIBOXX")
read_prices <- function(file, src = c("wikifolio")) {

  src %<>% match.arg(c("wikifolio"), several.ok = FALSE)

  namekey <- c(Datum = "Date", Erรถffnung = "Open", Eroeffnung = "Open",
               Hoch = "High", Tief = "Low", Schluss = "Close", Volumen = "Volume",
               `Begin date` = "Date", `Time interval (min)` = "Interval",
               Open = "Open", Close = "Close", High = "High", Low = "Low")

  # # wikifolio ------------------------------------------------------------
  if (src == "wikifolio") {

    prices <- read.csv2(file = file,
                        fileEncoding = c("UCS-4-INTERNAL"),
                        skip = 5,
                        sep = ";",
                        col.names = c("Date", "Interval",
                                      "Open", "Close", "High", "Low"),
                        allowEscapes = TRUE
    )
    prices$Date <- as.Date(
      as.POSIXct(lubridate::dmy_hms(prices$Date, tz = "UTC")
      ), tz = "UTC")
    prices <- prices[, -2]
    prices <- prices[, c("Date", "Open", "High", "Low", "Close")]
    prices <- as.xts(prices[, -1], order.by = prices[, 1])
    prices <- prices[!(weekdays(index(prices)) %in% c("Saturday", "Sunday")), ]
  } # # --------------------------------------------------------------------

  prices
}
rengelke/tradr documentation built on Jan. 2, 2022, 2:03 p.m.