R/get_prices_RS.R

Defines functions get_prices_RS

Documented in get_prices_RS

#' Start Remote RSelenium Server and Login to Web Services
#'
#' @param username username
#' @param password password
#' @param service character string specifying web service for login
#'
#' @import RSelenium
#' @return Remote RSelenium Server Environment
#' @export
#'
#' @examples startRS(username = "email@gmail.com", password ="***")
start_RS <- function (username = NULL, password = NULL, service = "wikifolio") {

  if (service == "wikifolio") {
    # start remote server
    rD <- rsDriver(browser = "chrome")
    remDr <- rD$client

    #can be added later to verify is login still active
    #remDr$getCurrentUrl()
    #XML::htmlParse(remDr$getPageSource()[[1]])

    #login to page
    login_url <- "https://www.wikifolio.com/dynamic/de/de/login/login?ReturnUrl=/de/"
    remDr$navigate(login_url)
    login_user <- remDr$findElement(using = 'css selector', "#Username.textbox")
    login_user$sendKeysToElement(list(username))
    login_pw <- remDr$findElement(using = 'css selector', "#Password.textbox")
    login_pw$sendKeysToElement(list(password))
    login_button <- remDr$findElement(using = 'css selector', "button.c-button.c-button--large.c-button--block.c-button--uppercase.c-button--bold")
    login_button$clickElement()

    remDr
  }
}


#' Download Historical Price Data from Wikifolio using RSelenium
#'
#' @param symbol wikifolio symbol
#' @param acc_statement if TRUE will download account statement; default FALSE
#'
#' @return returns an xts object containing historical price data of the wikifolio
#' @import zoo
#' @import xts
#' @importFrom lubridate dmy_hms
#' @export
#'
#' @examples get_prices_RS("WF0AAABEST")
get_prices_RS <- function(symbol, acc_statement = FALSE) {

  yesterday <- paste0(
    strsplit(as.character(Sys.Date() - 1), "-")[[1]][3], ".",
    strsplit(as.character(Sys.Date() - 1), "-")[[1]][2], ".",
    strsplit(as.character(Sys.Date() - 1), "-")[[1]][1]
  )

  if (isTRUE(acc_statement)) {
    url <- paste0(
      "https://www.wikifolio.com/dynamic/de/de/invest/download?type=account-statement&name=",
      symbol, "&dateFrom=01.01.2010&dateTo=", yesterday
    )
    } else {
      url <- paste0(
        "https://www.wikifolio.com/dynamic/de/de/invest/download?type=daily&name=",
        symbol, "&dateFrom=01.01.2010&dateTo=", yesterday
      )
    }

  remDr$navigate(url)
  Sys.sleep(18)

  # find file in download folder
  file_n <- list.files("~/Downloads") %>%
    grep(., pattern = symbol, perl = FALSE) %>%
    tail(., 1)
  file_tmp <- paste0("~/Downloads/",
                     list.files("~/Downloads")[file_n])

  prices <- read.csv2(file_tmp,
                             fileEncoding = c("UCS-4-INTERNAL"),
                             skip = 5,
                             sep = ";",
                             col.names = c("Date", "Interval", "Open", "Close", "High", "Low"),
                             allowEscapes = TRUE
  )
  if (file.exists(file_tmp)) file.remove(file_tmp)

  # format price data
  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.