[Returnanalytics-commits] r2664 - pkg/PortfolioAnalytics/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jul 29 17:47:31 CEST 2013


Author: rossbennett34
Date: 2013-07-29 17:47:31 +0200 (Mon, 29 Jul 2013)
New Revision: 2664

Modified:
   pkg/PortfolioAnalytics/R/constraints.R
   pkg/PortfolioAnalytics/R/objective.R
Log:
adding insert_constraints and insert_objectives functions

Modified: pkg/PortfolioAnalytics/R/constraints.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraints.R	2013-07-29 14:09:44 UTC (rev 2663)
+++ pkg/PortfolioAnalytics/R/constraints.R	2013-07-29 15:47:31 UTC (rev 2664)
@@ -824,3 +824,26 @@
 #   ))
 # }
 
+#' Insert a list of constraints into the constraints slot of a portfolio object
+#' @param portfolio object of class 'portfolio'
+#' @param constraints list of constraint objects
+#' @author Ross Bennett
+#' @export
+insert_constraints <- function(portfolio, constraints){
+  # Check portfolio object
+  if (is.null(portfolio) | !is.portfolio(portfolio)){
+    stop("you must pass in an object of class portfolio")
+  }
+  
+  # Check that constraints is a list
+  if(!is.list(constraints)) stop("constraints must be passed in as a list")
+  
+  # Check that all objects in the list are of class constraint
+  for(i in 1:length(constraints)){
+    if(!is.constraint(constraints[[i]]))
+      stop("constraints must be passed in as a list and all objects in constraints must be of class 'constraint'")
+  }
+  
+  portfolio$constraints <- constraints
+  return(portfolio)
+}

Modified: pkg/PortfolioAnalytics/R/objective.R
===================================================================
--- pkg/PortfolioAnalytics/R/objective.R	2013-07-29 14:09:44 UTC (rev 2663)
+++ pkg/PortfolioAnalytics/R/objective.R	2013-07-29 15:47:31 UTC (rev 2664)
@@ -24,6 +24,7 @@
 objective<-function(name , target=NULL , arguments, enabled=TRUE , ..., multiplier=1, objclass='objective'){
   if(!hasArg(name)) stop("you must specify an objective name")
   if (hasArg(name)) if(is.null(name)) stop("you must specify an objective name")
+  if (!hasArg(arguments) | is.null(arguments)) arguments<-list()
   if (!is.list(arguments)) stop("arguments must be passed as a named list")
   
   ## now structure and return
@@ -383,4 +384,28 @@
   Objective$min <- min
   Objective$max <- max
   return(Objective)
-} # end minmax_objective constructor
\ No newline at end of file
+} # end minmax_objective constructor
+
+#' Insert a list of objectives into the objectives slot of a portfolio object
+#' @param portfolio object of class 'portfolio'
+#' @param objectives list of objective objects
+#' @author Ross Bennett
+#' @export
+insert_objectives <- function(portfolio, objectives){
+  # Check portfolio object
+  if (is.null(portfolio) | !is.portfolio(portfolio)){
+    stop("you must pass in an object of class portfolio")
+  }
+  
+  # Check that objectives is a list
+  if(!is.list(objectives)) stop("objectives must be passed in as a list")
+  
+  # Check that all objects in the list are of class objective
+  for(i in 1:length(objectives)){
+    if(!is.objective(objectives[[i]]))
+      stop("objectives must be passed in as a list and all objects in objectives must be of class 'objective'")
+  }
+  
+  portfolio$objectives <- objectives
+  return(portfolio)
+}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list