## 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

`$\underset{x}{\mathrm{arg}\mathrm{max}}\left[R\text{'}x-MI\text{'}|x|-\lambda x\text{'}Cx\right],$`

where:

• 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.

• $\lambda$ 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

`$\underset{x}{\mathrm{arg}\mathrm{min}}\left[x\text{'}Cx\right].$`
3. Maximize net portfolio return subject to a maximum risk exposure target using

`$\underset{x}{\mathrm{arg}\mathrm{max}}\left[R\text{'}x-MI\text{'}|x|\right].$`

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('ftp.kissellresearch.com','username','pwd'); mget(f,'MI_Encrypted_Parameters.csv'); close(f) miData = readtable('MI_Encrypted_Parameters.csv','delimiter', ... ',','ReadRowNames',false,'ReadVariableNames',true);```

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 Trading 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, ... CovarianceData,1); ```

`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.

```format Weight(1:3)```
```ans = 0.0100 0.3198 0.1610 ```

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

```format bank Shares(1:3)```
```ans = 24420.02 3249893.71 402364.47```

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

`Value(1:3)`
```ans = 1000000.00 31977654.17 16097274.50 ```

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

```format MI(1:3)```
```ans = 1.0e-03 * 0.1250 0.7879 0.3729 ```

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

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

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

 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.