Portfolio optimization in markovitz

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 Kommentare

TickSeriesMat = cell2mat(TickSeries);
RetSeries = diff(TickSeriesMat)./TickSeriesMat(1:end-1,:);
Do you mind to try replace your code with above? See whether it help you or not.
Luan Vardari
Luan Vardari am 19 Sep. 2018
where i need to put this code?
Luan Vardari
Luan Vardari am 19 Sep. 2018
i put it but does not work
Kevin Chng
Kevin Chng am 20 Sep. 2018
Do you mind to attach your data file here for better testing.
Luan Vardari
Luan Vardari am 27 Sep. 2018
dear kevin, i put data to comment.
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 am 2 Okt. 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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Kevin Chng
Kevin Chng am 3 Okt. 2018

0 Stimmen

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 Kommentare

Luan Vardari
Luan Vardari am 3 Okt. 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
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.
^^

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Portfolio Optimization and Asset Allocation finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 19 Sep. 2018

Kommentiert:

am 3 Okt. 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by