Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Thread Subject:
Question About Portfolio Optimization Code

Subject: Question About Portfolio Optimization Code

From: Ryan

Date: 12 Oct, 2012 02:22:08

Message: 1 of 1

Can someone please tell me what is wrong with this code?

c = yahoo

ticker = {'MMM', 'AA', 'AXP', 'T', 'BAC', 'BA', 'CAT', 'CVX', 'CSCO', 'KO', 'DD' ...
'XOM', 'GE', 'HPQ', 'HD', 'INTC', 'IBM', 'JPM', 'JNJ', 'KFT', 'MCD', 'MRK' ...
'MSFT', 'PFE', 'PG', 'TRV', 'UTX', 'VZ', 'WMT', 'DIS'}

for i = 1:30

Price.(ticker{i}) = fetch(c,ticker(i),'Adj Close','01/01/2000','12/31/2010','m');
temp = Price.(ticker{i});
ClosePrice(:,i) = temp(:,2);

Returns = price2ret(ClosePrice);

ylabel('Monthly Return');
legend('3M', 'Alcoa');
title('3M vs Alcoa Monthly Returns')

p = (Portfolio('Name', 'DJIA 30'));
p = p.setAssetList(ticker);

p = p.estimateAssetMoments(Returns);
p = p.setDefaultConstraints;

[prisk, preturns] = p.plotFrontier(10);
[pwvt] = p.estimateFrontier(10);
hold on
plot(prisk, preturns, 'ored');

A = zeros(size(ticker));
A([1 2 3]) = 1;
b = .1;

p = p.setEquality(A, b);

p = Portfolio(p,'AEquality', A, 'bEquality', b);

hold on

C = zeros(size(ticker));
C([4, 5]) = 1;
d = .05;

p = p.setInequality(C,d);

p = Portfolio(p, 'AInequality', C, 'bInequality', d);

hold on
format plots();

G = zeros(size(ticker));
G([4, 5]) = 1;

p = p.setGroups(G, 0.1, 0.5);

p = Portfolio(p, 'GroupMarix', G, 'LowerGroup', 0.1, 'UpperGroup', 0.5);

hold on

lb = -.05;
ub = 0.10;

p = p.setBounds(lb,ub);

p = Portfolio(p, 'LowerBound', lb, 'UpperBound', ub);

hold on

p = p.setBudget(.95, 1.05);
p = portfolio(p, 'LowerBudget', .95, 'UpperBudget', 1.05);

hold on

p = portfolio(p, 'RiskFreeRate', .01);
bc = 0.005;
sc = 0.007;
X_o = 0.20*ones(1,5);

p = p.setInitPort(X_o);
p = p.setCosts(bc,sc);

[pwgt, pbuy, psell] = p.estimateFrontier (4);

q = p;
p = Portfolio(p, 'BuyCost', 0, 'SellCost', 0);

I copied the code form the video here:

I didn’t see any place to download the original source code; I just typed it out as the instructor was going through it. I could have missed a couple of lines; not sure.

Can someone please step through the code and help me get it straightened out? I’m doing this simply as a learning example, and I’m getting some errors that are so weird that I can’t even explain them.

Thanks everyone!!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us