R/MasterFile.R

Defines functions Perform_Valuation_Complete Valuation_Complete

Valuation_Complete <- function(forecast.year, buy.month, holding.months, explicit.period, historic.period, coe.method = c('CAPM', 'Fama')) {

  # timer
  function.timer <- Sys.time()

  if(length(forecast.year) > 1) {

    ### initiate parallel processing
    no_cores    <- parallel::detectCores()
    parallel_cl <- parallel::makeCluster(no_cores)

    parallel::clusterEvalQ(parallel_cl, library(masterstrat))
    parallel::clusterExport(parallel_cl, varlist=c('forecast.year', 'buy.month', 'holding.months',
                                                   'explicit.period', 'historic.period', 'coe.method'),
                                                    envir = environment())
    ###
    return.list  <- parallel::parLapply(parallel_cl, forecast.year, function(x) {

      Perform_Valuation_Complete(x, buy.month, holding.months, explicit.period, historic.period, coe.method)

    })
    # stop cluster
    parallel::stopCluster(parallel_cl)
  }
  if(length(forecast.year) == 1) {

    return.list  <-  Perform_Valuation_Complete(forecast.year, buy.month, holding.months, explicit.period, historic.period, coe.method)

  }
  cat('Valuation took', Sys.time() - function.timer, '\n')

  return(return.list)

}
Perform_Valuation_Complete <- function(forecast.year, buy.month, holding.months, explicit.period, historic.period, coe.method) {

  forecast.valuation <- ValuationHistoric(data.list = LoadAndClean_Fundamental(), forecast.year, explicit.period, historic.period, coe.method)

  if(is.na(forecast.valuation) == TRUE) {

    return(NA)

  } else {

    return.valuation.list  <- ValuationSummary(forecast.valuation)
    return.valuation.list  <- ValuationTable(return.valuation.list, forecast.year, buy.month, holding.months, explicit.period, historic.period, coe.method)

    return(return.valuation.list)
  }

}
uncoollocket/strider documentation built on Jan. 16, 2018, 12:05 a.m.