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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Aug 19 07:28:55 CEST 2013


Author: rossbennett34
Date: 2013-08-19 07:28:55 +0200 (Mon, 19 Aug 2013)
New Revision: 2824

Modified:
   pkg/PortfolioAnalytics/R/optimize.portfolio.R
Log:
adding backwards compatibility to optimize.portfolio.rebalancingto accept a v1_constraint object

Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R	2013-08-19 05:23:27 UTC (rev 2823)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R	2013-08-19 05:28:55 UTC (rev 2824)
@@ -997,15 +997,30 @@
 #' @author Kris Boudt, Peter Carl, Brian G. Peterson
 #' @name optimize.portfolio.rebalancing
 #' @export
-optimize.portfolio.rebalancing <- function(R, portfolio, constraints=NULL, objectives=NULL, optimize_method=c("DEoptim","random","ROI"), search_size=20000, trace=FALSE, ..., rp=NULL, rebalance_on=NULL, training_period=NULL, trailing_periods=NULL)
+optimize.portfolio.rebalancing <- function(R, portfolio=NULL, constraints=NULL, objectives=NULL, optimize_method=c("DEoptim","random","ROI"), search_size=20000, trace=FALSE, ..., rp=NULL, rebalance_on=NULL, training_period=NULL, trailing_periods=NULL)
 {
   stopifnot("package:foreach" %in% search() || require("foreach",quietly=TRUE))
   start_t<-Sys.time()
   
+  if (!is.null(portfolio) & !is.portfolio(portfolio)){
+    stop("you must pass in an object of class 'portfolio' to control the optimization")
+  }
+  
   # Check for constraints and objectives passed in separately outside of the portfolio object
   if(!is.null(constraints)){
-    # Insert the constraints into the portfolio object
-    portfolio <- insert_constraints(portfolio=portfolio, constraints=constraints)
+    if(inherits(constraints, "v1_constraint")){
+      if(is.null(portfolio)){
+        # If the user has not passed in a portfolio, we will create one for them
+        tmp_portf <- portfolio.spec(assets=constraints$assets)
+      }
+      message("constraint object passed in is a 'v1_constraint' object, updating to v2 specification")
+      portfolio <- update_constraint_v1tov2(portfolio=tmp_portf, v1_constraint=constraints)
+      # print.default(portfolio)
+    }
+    if(!inherits(constraints, "v1_constraint")){
+      # Insert the constraints into the portfolio object
+      portfolio <- insert_constraints(portfolio=portfolio, constraints=constraints)
+    }
   }
   if(!is.null(objectives)){
     # Insert the objectives into the portfolio object



More information about the Returnanalytics-commits mailing list