Main Content

Optimize Long Portfolio

This example shows how to determine the optimal portfolio weights for a specified dollar value using transaction cost analysis from the Kissell Research Group. The sample portfolio contains only long shares of stock. You can incorporate risk, return, and market-impact cost during implementation of the investment decision.

This example requires an Optimization Toolbox™ license. For background information, see Optimization Theory Overview (Optimization Toolbox).

The KRGPortfolioOptimizationExample function, which you can access by entering edit KRGPortfolioOptimizationExample.m, addresses three different optimization scenarios:

  1. Maximize the trade off between net portfolio return and portfolio risk. The trade off maximization is expressed as



    • R is the estimated return for each stock in the portfolio.

    • x denotes the weights for each stock in the portfolio.

    • MI is the market-impact cost for the specified dollar value and share quantities.

    • λ is the specified risk aversion parameter.

    • C is the covariance matrix of the stock data.

  2. Minimize the portfolio risk subject to a minimum return target using


  3. Maximize net portfolio return subject to a maximum risk exposure target using


Lower and upper bounds constrain x in each scenario. Each optimization finds a local optimum. For ways to search for the global optimum, see Local vs. Global Optima (Optimization Toolbox).

Retrieve Market-Impact Parameters and Load Data

Retrieve the market-impact data from the Kissell Research Group FTP site. Connect to the FTP site using the ftp function with a user name and password. Navigate to the MI_Parameters folder and retrieve the market-impact data in the MI_Encrypted_Parameters.csv file. miData contains the encrypted market-impact date, code, and parameters.

f = ftp('','username','pwd');

miData = readtable('MI_Encrypted_Parameters.csv','delimiter', ...

Create a Kissell Research Group transaction cost analysis object k. Specify initial settings for the date, market-impact code, and number of trading days.

k = krg(miData,datetime('today'),1,250);

Load the example data TradeDataPortOpt and the covariance data CovarianceData from the file KRGExampleData.mat, which is included with the Datafeed Toolbox™. Limit the data set to the first 50 rows.

load KRGExampleData TradeDataPortOpt CovarianceData

n = 50;
TradeDataPortOpt = TradeDataPortOpt(1:n,:);
CovarianceData = CovarianceData(1:n,1:n);

For a description of the example data, see Kissell Research Group Data Sets.

Maximize Net Portfolio Return

Run the optimization scenario using the example and covariance data. To run the first optimization, specify 1 in the last input argument.

[Weight,Shares,Value,MI] = KRGPortfolioOptimizationExample(TradeDataPortOpt, ...

KRGPortfolioOptimizationExample returns the optimized values for each stock in the portfolio:

  • Portfolio weight

  • Number of shares

  • Portfolio dollar value

  • Market-impact cost

To run the other two scenarios, specify 2 or 3 in the last input argument of KRGPortfolioOptimizationExample.

Display the portfolio weight for the first three stocks in the portfolio in decimal format.


ans =


Display the number of shares using two decimal places for the first three stocks in the portfolio.

format bank

ans =


Display the portfolio dollar value for the first three stocks in the portfolio.

ans =


Display the market-impact cost for the first three stocks in the portfolio in decimal format.


ans =

   1.0e-03 *



[1] Kissell, Robert. “Creating Dynamic Pre-Trade Models: Beyond the Black Box.” Journal of Trading. Vol. 6, Number 4, Fall 2011, pp. 8–15.

[2] Kissell, Robert. “TCA in the Investment Process: An Overview.” Journal of Index Investing. Vol. 2, Number 1, Summer 2011, pp. 60–64.

[3] Kissell, Robert. The Science of Algorithmic Trading and Portfolio Management. Cambridge, MA: Elsevier/Academic Press, 2013.

[4] Chung, Grace and Robert Kissell. “An Application of Transaction Costs in the Portfolio Optimization Process.” Journal of Trading. Vol. 11, Number 2, Spring 2016, pp. 11–20.

See Also

| | (Optimization Toolbox) | (Optimization Toolbox)

Related Topics