API Docs

Manual, tutorials and complete function reference

Optimization Constraint Methods


Each optimization constraint could be specified in 3 different formats:


Scalar Constraints

Scalar constraints are the simplest type of optimization boundaries. They require a single constant that is applied over a full time span of portfolio optimization. The example below sets portfolio beta constraint to be greater or equal to 0.1.

# create portfolio and add positions
portfolio=portfolio_create(fromTime="2014-10-02 09:30:00", toTime="2014-10-03 16:00:00")
portfolio_addPosition(portfolio, symbol=c('ะก','GOOG'),	quantity=c(500,600))

# rebalancing every minute, static portfolio ignores rebalancing history
portfolio_settings(portfolio, portfolioMetricsMode='price', resultsSamplingInterval='1m')

# set optimization goal and define constraints
optimizer=optimization_goal(portfolio, goal="SharpeRatio", direction="maximize")
optimization_constraint_beta(optimizer, "<=", constraintValue = 0.1)

# run optimization
optimalPortfolio=optimization_run(optimizer)

# plot results
util_plot2d(portfolio_beta(optimalPortfolio), title="Beta", legend="Optimal Beta") + 
util_line2d(portfolio_beta(portfolio), legend="Original Beta")

% create portfolio and add positions
portfolio=portfolio_create('fromTime','2014-10-02 09:30:00', 'toTime','2014-10-03 16:00:00');
portfolio_addPosition(portfolio, {'C';'GOOG'},	[500;600]);

% rebalancing every minute, static portfolio ignores rebalancing history
portfolio_settings(portfolio,'portfolioMetricsMode','price','resultsSamplingInterval','1m')

% set optimization goal and define constraints
optimizer=optimization_goal(portfolio, 'goal', 'SharpeRatio','direction', 'maximize');
optimizer=optimization_constraint_beta(optimizer,'<=',0.1);

% run optimization
optimalPortfolio=optimization_run(optimizer);

% plot results
util_plot2d(portfolio_beta(optimalPortfolio),'Optimal Beta', 'Title','Beta')+...
    util_line2d(portfolio_beta(portfolio),'Original Beta')

Vector Constraints

Instead of using a single scalar, one could specify an vector of constraint values with corresponding timestamps. Optimization algorithm would then automatically determine when certain constraint value should be applied based on the current rebalancing time.

# create portfolio and add positions
portfolio=portfolio_create(fromTime="2014-10-01 09:30:00", toTime="2014-10-02 16:00:00")
portfolio_addPosition(portfolio, symbol=c("C","GOOG"), quantity=c(500,600))

# rebalancing every minute, static portfolio ignores rebalancing history
portfolio_settings(portfolio, portfolioMetricsMode='price', resultsSamplingInterval='1m')

betaVector = cbind(c("2014-10-01 09:30:00","2014-10-01 12:30:00"), c(0.1,0.5))

# set optimization goal and define constraints
optimizer=optimization_goal(portfolio, goal="SharpeRatio", direction="maximize")
optimizer=optimization_constraint_beta(optimizer, "<=", constraintValue = betaVector)

# run optimization
optimalPortfolio=optimization_run(optimizer)

# plot results
util_plot2d(portfolio_beta(optimalPortfolio), title="Beta", legend="Optimal Beta") + 
util_line2d(portfolio_beta(portfolio), legend="Original Beta")
% create portfolio and add positions
portfolio=portfolio_create('fromTime','2014-10-02 09:30:00', 'toTime','2014-10-03 16:00:00');
portfolio_addPosition(portfolio, {'C';'GOOG'},	[500;600]);

% rebalancing every minute, static portfolio ignores rebalancing history
portfolio_settings(portfolio,'portfolioMetricsMode','price','resultsSamplingInterval','1m')

betaVector = [util_dateToPOSIXTime(['2014-10-02 09:30:00';'2014-10-02 14:30:00']),[0.1;0.5]];

% set optimization goal and define constraints
optimizer=optimization_goal(portfolio, 'goal', 'SharpeRatio','direction', 'maximize');
optimizer=optimization_constraint_beta(optimizer,'<=',betaVector);

% run optimization
optimalPortfolio=optimization_run(optimizer);

% plot results
util_plot2d(portfolio_beta(optimalPortfolio),'Optimal Beta', 'Title','Beta')+...
    util_line2d(portfolio_beta(portfolio),'Original Beta')

User-defined Constraints

User-defined constraint relies on a provided function, which is called during portfolio optimization at specified rebalancing times. Function should return a scalar constraint value and would optionally receive portfolio and current time, if they are specified as arguments.

  • function () - automatically converted to a scalar constraint
  • function (time) - automatically converted to a vector constraint
  • function (portfolio, time) - evaluated during optimization procedure

Note: When functional constraint specifies portfolio object as a required arguement, it could no longer be converted to a scalar or vector-based constraint. In such case, optimization procedure would take a performance hit.

# create portfolio and add positions
my_portfolio=portfolio_create(fromTime="2014-10-01 09:30:00", toTime="2014-10-01 10:30:00")
portfolio_addPosition(my_portfolio, symbol=c('C','GOOG'),  quantity=c(500,600))

# rebalancing every 30m, static portfolio ignores rebalancing history
portfolio_settings(my_portfolio, portfolioMetricsMode='price', resultsSamplingInterval='30m')

# compute benchmark beta values
bench_portfolio=portfolio_create(fromTime="2014-10-01 09:30:00", toTime="2014-10-01 10:30:00")
portfolio_settings(bench_portfolio, resultsSamplingInterval='30m')
portfolio_addPosition(bench_portfolio, symbol=c('SPY'), quantity=c(1))
bench_beta = portfolio_beta(bench_portfolio)

# define custom optimization constraint method for beta
myConstraint = function (portfolio, time) { 
  
  bench_beta_value = bench_beta[bench_beta[,1]==time, 2]
  portfolio_beta_value = portfolio_beta(portfolio)
  portfolio_beta_value = portfolio_beta_value[portfolio_beta_value[,1]==time,2]
  
  if(portfolio_beta_value > bench_beta_value) { 
    return (bench_beta_value)
  } 
  else { 
    return (portfolio_beta_value)
  }
  
}

# set optimization goal and define constraints
optimizer=optimization_goal(my_portfolio, goal="SharpeRatio", direction="maximize")
optimizer=optimization_constraint_beta(optimizer, "<=", constraintValue = myConstraint)

# run optimization
optimalPortfolio=optimization_run(optimizer)

# plot results
util_plot2d(portfolio_beta(optimalPortfolio), title="Beta", legend="Optimal Beta") + 
util_line2d(portfolio_beta(my_portfolio), legend="Original Beta")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	Example 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% create portfolio and add positions
portfolio=portfolio_create('fromTime','2014-10-02 09:30:00', 'toTime','2014-10-03 16:00:00');
portfolio_addPosition(portfolio, {'C';'GOOG'},	[500;600]);

% rebalancing every minute, static portfolio ignores rebalancing history
portfolio_settings(portfolio,'portfolioMetricsMode','price','resultsSamplingInterval','30m')

a=portfolio_beta(portfolio);

betaVector = [a(:,1),ones(length(a(:,1)),1)*0.8];

% define custom optimization constraint method for beta
% function [ output_args ] = myConstraint( portfolio )
% a=portfolio_beta(portfolio);
% output_args=a(:,2)-1;
% end

% set optimization goal and define constraints
optimizer=optimization_goal(portfolio, 'goal', 'SharpeRatio','direction', 'maximize');
optimizer=optimization_constraint_beta(optimizer,'<=','myConstraint');

% run optimization
optimalPortfolio=optimization_run(optimizer);

% plot results
util_plot2d(portfolio_beta(optimalPortfolio),'Optimal Beta', 'Title','Beta')+...
    util_line2d(portfolio_beta(portfolio),'Original Beta')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	Example 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% create portfolio and add positions
portfolio=portfolio_create('fromTime','2014-10-02 09:30:00', 'toTime','2014-10-03 16:00:00');
portfolio_addPosition(portfolio, {'C';'GOOG'},	[500;600]);

% rebalancing every minute, static portfolio ignores rebalancing history
portfolio_settings(portfolio,'portfolioMetricsMode','price','resultsSamplingInterval','30m')

a=portfolio_beta(portfolio);

betaVector = [a(:,1),ones(length(a(:,1)),1)*0.8];

% define custom optimization constraint method for beta
% function [ output_args ] = myConstraint( portfolio ,time )
% a=portfolio_beta(portfolio);
% if time < util_dateToPOSIXTime('2014-10-02 14:30:00')
% output_args=a(:,2)-0.5;
% else
% output_args=a(:,2)-1;  
% end
% end


% set optimization goal and define constraints
optimizer=optimization_goal(portfolio, 'goal', 'SharpeRatio','direction', 'maximize');
optimizer=optimization_constraint_beta(optimizer,'<=','myConstraint');

% run optimization
optimalPortfolio=optimization_run(optimizer);

% plot results
util_plot2d(portfolio_beta(optimalPortfolio),'Optimal Beta', 'Title','Beta')+...
    util_line2d(portfolio_beta(portfolio),'Original Beta')