R/crack_tidying.R

Defines functions crack_tidying

Documented in crack_tidying

#' Crack Tidying
#'
#' \code{crack_tidying} returns a cleaned historical spread series of choosen petroleum product
#' versus Dubai imported from Marketview.
#'
#' @param load_product a name of product as a KOREAN character string choosen
#' @param load_from date from which a crack of choosen product to be loaded
#' @param data_form you can choose "monthly", "daily", "monthly_5" (monthly average of last 5 days available)
#' @param outright logical. If set to TRUE, outright price is called, not a crack spread versus DUBAI
#'
#' @return a numeric vector of crack spread
#' @export
#'
#' @examples
#' monthly_crack <- crack_tidying("DIESEL", "2011-01-01", "monthly", outright = TRUE)
crack_tidying <- function(load_product, load_from, data_form, outright = FALSE) {

 ##### Needed libraries
  library(readxl, quietly = TRUE)
  library(lubridate, quietly = TRUE)
  library(xts, quietly = TRUE)
  library(tibble, quietly = TRUE)
  library(dplyr, quietly = TRUE)
  library(purrr, quietly = TRUE)

 ##### Settings
 # load_product <- "GO_500"
 # load_from <- "2011-01-01"
 # data_form <- "monthly"


 ##### Data loading
 #crack_raw <- readxl::read_excel("price_history.xlsx", sheet = "Loading", col_names = TRUE, skip = 2, na = "", col_types = rep("text", 19))
 data(price_history, envir = .GlobalEnv)


 #####
 product_all <- names(crack_raw)[which(names(crack_raw) == "WTI"):length(crack_raw)]


 ##### Convert excel date numeric into Date class
 crack_raw$DATE <- crack_raw$DATE %>%
   (function(df) {as.numeric(df) + as.numeric(as.Date('1900-01-01')) - 2}) %>% as.Date(origin = "1970-01-01")


 ##### Convert columns into numeric class
 clean_1 <- crack_raw %>% (function(df) {
   df[, -which(names(df) == "DATE")] <- df[, -which(names(df) == "DATE")] %>% apply(2, as.numeric); df
 })


 ##### Erase weekends
 clean_2 <- (clean_1$WEEKENDS == 1) %>% (function(df) {!ifelse(is.na(df), FALSE, df)}) %>% clean_1[., ]


 ##### Convert into xts class
 clean_3 <- clean_2 %>% tibble::column_to_rownames(var = "DATE") %>% xts::xts(order.by = clean_2$DATE %>% ymd)


 ##### Apply load_from
 clean_n <- clean_3 %>% (function(df) {(df %>% index >= load_from) %>%  df[., ]})


 ##### Convert into monthly
 clean_monthly <- clean_n %>% (function(df) {
   1:length(product_all) %>%
     purrr::map(function(num) {
       df[, which(names(df) %in% c("DATE", product_all[num]))] %>%
         split("months") %>% lapply(FUN = mean, na.rm = TRUE) %>% do.call(rbind, .) %>%
         as.data.frame})
 }) %>% bind_cols %>% (function(df) {names(df) <- product_all; df})

 clean_monthly <- clean_monthly %>% xts(order.by = clean_n[, 1] %>% to.monthly %>% index)


 ##############################
 ##### Final monthly data
 final_monthly <- (clean_monthly[, load_product] - ifelse(outright, 0, 1) * clean_monthly[, "DUBAI_M1"]) %>%
   xts(order.by = clean_n[, 1] %>% to.monthly %>% index) %>% (function(df) {names(df) <- "choosen"; df})


 ##### Convert into daily
 crack_daily <- clean_n %>% (function(df) {
   1:length(product_all) %>%
     purrr::map(function(num) {
       df[, which(names(df) %in% c("DATE", product_all[num]))] %>% complete.cases %>%
         df[., which(names(df) %in% c("DATE", product_all[num]))]
     })
 }) %>% (function(df) {names(df) <- product_all; df})


 ##############################
 ##### Final daily data
 final_daily <- (crack_daily[[which(names(crack_daily) == load_product)]] -
                   ifelse(outright, 0, 1) * crack_daily[[which(names(crack_daily) == "DUBAI_M1")]]) %>%
   (function(df) {names(df) <- c("choosen"); df})


 ##############################
 ##### Final monthly_5 data
 final_monthly_5 <- crack_daily[[which(names(crack_daily) == load_product)]] %>%
   split("months") %>%
   purrr::map(function(df2) {xts::last(df2, n = 5)}) %>%
   purrr::map(~mean(., na.rm = TRUE)) %>% flatten %>%
   unlist %>% as.data.frame %>%
   xts::xts(., order.by = crack_daily[[1]] %>% to.monthly %>% index) %>%
   (function(df) {names(df) <- "load_product";  df})

 ##########
 ##### Datasets to be choosen
  if (data_form == "monthly") {
   final_monthly
 } else if (data_form == "daily") {
   final_daily
 } else if (data_form == "monthly_5") {
   final_monthly_5
 }

}
skylics/crack_analysis documentation built on March 20, 2018, 7:45 p.m.