R/fRatios.R

#' Fetch Financial Ratios
#'
#' Fetches financial ratios from tables.(This function still developing.)
#'
#' @param symbol A character value that you can use ticker.
#' @param get A character value that types of ratios. One of "\bold{debtratio}"and"\bold{currentratio}".
#' @param comment A logical value that set of comment. If true, ratios fetces with comment.
#' @param banking A logical value that set of ratios. If company in the financial sector, you should choose TRUE.
#' @return A integer.
#' @seealso \code{\link{fBIST},\link{barData},\link{finTables}}
#' @export
fRatios <- function(symbol,get,comment=FALSE,banking=FALSE) {
  library(gsubfn)
  if(banking==TRUE) {

    stop(paste0("fRatios not able to computing current ratio to financial servis sector."))

  }

  #    multi sub function to get rid of "B" and "M" at the same time!
  #    mgsub <- function(pattern, replacement, x, ...) {
  #     if (length(pattern)!=length(replacement)) {
  #       stop("pattern and replacement do not have the same length.")
  #     }
  #     result <- x
  #     for (i in 1:length(pattern)) {
  #       result <- gsub(pattern[i], replacement[i], result, ...)
  #     }
  #     result
  #   }

  link <- paste0("http://www.marketwatch.com/investing/stock/",symbol,"/financials/balance-sheet/quarter")
  meta <- readHTMLTable(link)
  names(meta[[1]])[1] <- ""
  dframe <- do.call(rbind, meta)
  dframe <- dframe[,-7]
  rowValue <- nrow(dframe)
  `rownames<-`(dframe, 1:rowValue)
  currentAssets <- dframe[20,6]
  currentAssets <- as.character(currentAssets)     # convert object through character to using in gsubfn.
  currentAssets <- unname(sapply(gsubfn('\\D+', list(B= '*1e9', M= '*1e6'), currentAssets),
                                 function(x) eval(parse(text=x))))   # get rid of B or M
  totalAssets   <- dframe[36,6]
  totalAssets   <- as.character(totalAssets)
  totalAssets   <- unname(sapply(gsubfn('\\D+', list(B= '*1e9', M= '*1e6'), totalAssets),
                                 function(x) eval(parse(text=x))))
  noncurrentAssets <- (totalAssets-currentAssets)
  currentLiabilities <- dframe[48,6]
  currentLiabilities <- as.character(currentLiabilities)
  currentLiabilities  <- unname(sapply(gsubfn('\\D+', list(B= '*1e9', M= '*1e6'), currentLiabilities),
                                       function(x) eval(parse(text=x))))
  Equity <- dframe[67,6]
  Equity <- as.character(Equity)
  Equity <-  unname(sapply(gsubfn('\\D+', list(B= '*1e9', M= '*1e6'), Equity),
                           function(x) eval(parse(text=x))))

  noncurrentLiabilities <- (totalAssets-(currentLiabilities+Equity))
  currentRatio <- (currentAssets/currentLiabilities)
  workingCapital <- (currentAssets-currentLiabilities)
  inventories <- dframe[13,6]
  inventories <- as.character(inventories)
  inventories <- unname(sapply(gsubfn('\\D+', list(B= '*1e9', M= '*1e6'), inventories),
                               function(x) eval(parse(text=x))))
  acidTest    <- ((currentAssets-inventories)/currentLiabilities)
  totalLiabilities <- currentLiabilities + noncurrentLiabilities
  debtRatio <- totalLiabilities/totalAssets
  equityRatio <- Equity / totalLiabilities


  if(get=="currentratio") {
    if(banking==TRUE) {

      stop(paste0("fRatios not able to computing current ratio to financial servis sector."))
    }
    print(currentRatio)


    if(comment==TRUE) {

      if (currentRatio < 1 ) {
        cat(" The current assets is an important section of
            liquidity because short-term liabilities are due
            within the next year.So this firm's current assets
            smaller than liabilities.This firm's ratio is low to pay its loan.")
      }
      if (currentRatio > 1 && currentRatio < 2  ) {
        cat("Current assets bigger than liabilities. So this firm able to pay its loan.
            But not enough, sufficently ")
      }
      if (currentRatio > 2 ) {
        cat("Current assets bigger than liabilities. So this firm able to pay its loan.
            And current assets and liabilities balanced.  ")
      }
      if (currentRatio > 5 ) {
        cat("Current assets bigger than liabilities. So this firm able to pay its loan.
               But current assets too bigger than liabilities. This situation not good for profitability")
      }
      }
    }
  if(get=="debtratio") {
    if (banking == TRUE) {

      stop(paste0("fRatios not able to computing debt ratio to financial servis sector."))
    }

    print(debtRatio)
    if(comment==TRUE) {

      if(debtRatio > 0.50 ) {

        cat("This company's liabilities are bigger than equity.")
      }
      if(debtRatio < 0.50) {

        cat("This company's liabilities are smaller than equity.")
      }

    }

  }

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