R/barData.R

#' Fetch XU100 Bar TickData
#'
#' Fetches bar data from historical data service
#'
#' @param symbol A character value that you can use ticker.
#' @param startDate A character value or NULL. "MM-DD-YYYY"
#' @param endDate A character value or NULL.   "MM-DD-YYYY"
#' @param col A character value that provide possibility choose column.One of "\code{open}","\code{high}","\code{low}" and "\code{close}, \code{volume}".
#'
#' @return A data.frame is contains Open, High, Low, Close Prices and Volume.
#' @seealso \code{\link{fBIST},\link{finTables}}
#' @export
barData <- function(symbol,startDate=NULL,endDate=NULL,col=NULL) {

  ## setup local time according os before set xts.
  if (Sys.info()[['sysname']]== "Windows") {

    Sys.setlocale("LC_TIME", "English")
  }
  else
    Sys.setlocale("LC_TIME", "en_US.UTF-8")  ## to set up xts.

  #################################################

  if(is.null(startDate)) {

    src <-paste0("https://www.google.com/finance/historical?q=",symbol,"&output=csv")
    if(!is.null(endDate)) {

      stop(paste0("When starDate argument is null, endDate have to null."))
    }
  }

  else {
    src <-paste0("https://www.google.com/finance/historical?q=",symbol,
                 "&startdate=",startDate,"&enddate=",endDate,"&output=csv")
  }
  meta <- read.csv(src, sep=",")
  bar <- strptime(meta[,1],"%d-%b-%y","GMT")   # set time to xts
  meta[[1]] <- bar
  meta <- xts(meta[,-1], order.by=meta[,1])    # set xts.



  if (is.null(col)) {
    meta
  }
  else if (col=="open") {

    meta<- meta[,-5]
    meta<- meta[,-4]
    meta<- meta[,-3]
    meta<- meta[,-2]
    meta
  }
  else if (col=="high") {
    meta<- meta[,-5]
    meta<- meta[,-4]
    meta<- meta[,-3]
    meta<- meta[,-1]
    meta
  }
  else if (col=="low") {

    meta<- meta[,-5]
    meta<- meta[,-4]
    meta<- meta[,-2]
    meta<- meta[,-1]
    meta
  }
  else if (col=="close")
  {
    meta<- meta[,-5]
    meta<- meta[,-3]
    meta<- meta[,-2]
    meta<- meta[,-1]
    meta
  }
  else if (col=="volume")
  {

    meta<- meta[,-4]
    meta<- meta[,-3]
    meta<- meta[,-2]
    meta<- meta[,-1]
    meta
  }


}
enesn/rBIST documentation built on May 16, 2019, 5:12 a.m.