Portfolio optimization in markovitz

6 views (last 30 days)
Luan Vardari
Luan Vardari on 19 Sep 2018
Commented: Kevin Chng on 3 Oct 2018
I have problem in this code,
T = readtable('data (2).xlsx') % Name of your data
symbol = T.Properties.VariableNames(3:end)';
dailyReturn = tick2ret(T{:,3:end});
p = Portfolio('AssetList',symbol,'RiskFreeRate',0.01/252);
p = estimateAssetMoments(p, dailyReturn);
p = setDefaultConstraints(p);
w1 = estimateMaxSharpeRatio(p)
[risk1, ret1] = estimatePortMoments(p, w1)
f = figure;
tabgp = uitabgroup(f); % Define tab group
tab1 = uitab(tabgp,'Title','Efficient Frontier Plot'); % Create tab
ax = axes('Parent', tab1);
% Extract asset moments from portfolio and store in m and cov
[m, cov] = getAssetMoments(p);
scatter(ax,sqrt(diag(cov)), m,'oc','filled'); % Plot mean and s.d.
xlabel('Risk')
ylabel('Expected Return')
text(sqrt(diag(cov))+0.0003,m,symbol,'FontSize',7); % Label ticker names
hold on;
[risk2, ret2] = plotFrontier(p,10);
plot(risk1,ret1,'p','markers',15,'MarkerEdgeColor','k',...
'MarkerFaceColor','y');
hold off
tab2 = uitab(tabgp,'Title','Optimal Portfolio Weight'); % Create tab
% Column names and column format
columnname = {'Ticker','Weight (%)'};
columnformat = {'char','numeric'};
% Define the data as a cell array
data = table2cell(table(symbol(w1>0),w1(w1>0)*100));
% Create the uitable
uit = uitable(tab2, 'Data', data,...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'RowName',[]);
% Set width and height
uit.Position(3) = 450; % Widght
uit.Position(4) = 350; % Height
error messege: Undefined function 'diff' for input arguments of type 'cell'.
Error in tick2ret (line 92) RetSeries = diff(TickSeries)./TickSeries(1:end-1,:);
  7 Comments
Kevin Chng
Kevin Chng on 2 Oct 2018
I don't have any error in running your code.
However, your attached excel file is .csv format.
Hence recommend you change your code from
T = readtable('data (2).xlsx') % Name of your data
to
T = readtable('data (2).csv') % Name of your data
Luan Vardari
Luan Vardari on 2 Oct 2018
Dear Kevin, i have one more question, in other Markowitz portfolio code i have a problem. i send codes in attach. in the same data. can you help?

Sign in to comment.

Answers (1)

Kevin Chng
Kevin Chng on 3 Oct 2018
Hi Luan Vardari,
In your original question, i think you are able to resolve it by changing
T = readtable('data (2).xlsx') % Name of your data
to
T = readtable('data (2).csv') % Name of your data
In your comment, you attached another script which is using CVX.
Here is the brief explanation about CVX : http://cvxr.com/cvx/
How to make your script work?
2) Unzip the folder in your working folder/current folder : Click on Extract here. The working folder is the folder where has your markowitz_opt.m script
3) After unzip, open the folder. Open cvx_setup.m and cvx_startup.m in MATLAB, and run them. When you run them in MATLAB, your working folder is still the same folder which has your markowitz_opt.m script.
4) Now, open markowitz_opt.m and run them. Done!
if you have any doubt, you are welcome to ask here.
  2 Comments
Luan Vardari
Luan Vardari on 3 Oct 2018
I do this its work, but how can i do to read my data, i this it work wir random data, where do i need to write to read data(2).csv
Kevin Chng
Kevin Chng on 3 Oct 2018
Great to heard that.
Sorry that I'm not familiar with this. I have to understand what is the meaning of those symbols first.
n=25; %number of items
p_mean = randn(n,1);
temp = randn(n,n);
sig = temp'*temp;
r = floor(0.1*n);
alpha = 0.8;
r_min = 1;
Or you may post a new questions about this to seek help from the community.
^^

Sign in to comment.

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!