clear all;
close all;
clc;
% House keeper 
T=readtable('stock_price.xlsx')
symbol=T.Properties.VariableNames(2:end)'
dailyReturn=tick2ret(T(:,2:end));

p=Portfolio('AssetList',symbol,'RiskFreeRate',0.01/252)


p=estimateAssetMoments(p,dailyReturn)


p=setDefaultConstraints(p)


w1=estimateMaxSharpeRatio(p)

[risk1,ret1]=estimatePortMoments(p,w1)


%%Plot graph

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',[]);