How to Find a Portfolio with the Highest Sharpe Ratio?
32 views (last 30 days)
Show older comments
Hello community,
I recently began to try using MatLab as an alternative to excel to deal with large amount of data. Currently, I am trying to build a portfolio with some stocks that maximizes the portfolio sharpe ratio.
I have in my hand the daily total return (%) for the 30 stocks that currently make up the Dow Jones Industrial Average (attached). Out of the 30 stocks, I hope to make a portfolio of five stocks, each weighted at 20%. Among all the possible portfolio, I wish to locate the one with the highest sharpe ratio assuming risk-free rate is 5%. Then, I hope to adjust the weight of the five stocks in this portfolio until I get the maximum sharpe ratio.
For now, I have the following:
PortfolioCombo = nchoosek(1:30, 5) %to produce all portfolios for 5 out of 30 stocks
PortfolioID = ndgrid(1:size(PortfolioCombo, 1), 1:size(PortfolioCombo, 2)) %assigns a unique id to each portfolio.
PortfolioMatrix = accumarray([PortfolioCombo(:),PortfolioID(:)],1)'./5 %this will produce a matrix with 142506 rows with 30 columns. Each row is a unique combination of five "1" and twenty five "0". Since I hope to put a 0.2 weight on each stock, I divide it by five.
Now this will only output a mapping of combinations (with weights). Then I imported my data as follows:
Data=xlsread('DJ.xlsx','B3:AE254');
Then, I have the following logic in my head:
I need to:
1. Find the mean return of every stock. This will be a 1x30 matrix.
2. Multiply each of the 30 mean returns by each cell in each row in PortfolioMatrix. I can imagine that the cells with value 0.2 will output the weighted return for that stock. At the end, there will still be 142506 rows with 30 columns. Each row has 25 zeros and 5 weighted return.
3. Then I can maybe use a for loop to find the sharpe ratio of each row using the sharpe() function.
4. Then I can sort the sharpe ratios in descending order, and the row with the highest sharpe ratio will be my desired portfolio.
5. Lastly, I will only focus on the desired portfolio, adjust the weight of each stock until the sharpe ratio is maximized.
I only manged to proceed with Step 1 as follows:
MeanReturn = mean(Data, 1);
But then I am stuck. Being new to this, I haven't been successful searching for examples that can solve my problems. Obviously, the MeanReturn matrix cannot multiply by PortfolioMatrix due to dimensions. And even if I managed to find the sharpe ratios for all 142506 rows and sort them, how am I going to know which stocks are in that portfolio? After all, it is all numbers with no ticker symbols.
I also began to doubt if my logic is suitable for the software. I hope to see some solutons or examples here and eventually understand better what codes can achieve and play with them. Thanks!
0 Comments
Answers (1)
Alejandra Pena-Ordieres
on 30 Mar 2023
Hello,
The Financial Toolbox has a funtion that does exaclty what you are looking for, estimateMaxSharpeRatio. Your workflow would be as follows:
% Load data
Data=xlsread('DJ.xlsx','B3:AE254');
% Create Portfolio
p = Portfolio(RiskFreeRate=0.05);
% Fill in portfolio mean and variance
p = estimateAssetMoments(p,Data);
% Add constraints
p = setBounds(p,0.2,0.2,BoundType='conditional'); % Weights can be either zero or exactly 0.2
p = setMinMaxNumAssets(p,5,5); % Choose exactly 5 assets
% Find max Sharpe ratio portfolio
w = estimateMaxSharpeRatio(p,Method='iterative');
I hope this helps.
0 Comments
See Also
Categories
Find more on Portfolio Optimization and Asset Allocation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!