...
Code Block |
---|
plot(1:10,dailyReturns(closingPrices); |
e)
Code Block |
---|
functionfunction resboolean = isGoingUp( dailyReturnn, i, ndailyReturn) resboolean = true; ; for startj = min(length(dailyReturn),i); stop =: -1: max (1, i-n +1); for j = start : -1: stop if ( dailyReturn (j) < 0) resboolean = false ; return ; end end end |
f)
Code Block |
---|
function returns = momentum ( startAmount , n, closingPrices ) dailyReturn = dailyReturns ( closingPrices ); cash = startAmount ; invested = 0; for i = n: size ( closingPrices , 2) invested = invested * closingPrices (i) / closingPrices (i -1) ; if ( is_going_up (n, i, dailyReturn )) % Buy invested = invested + cash ; cash = 0; else % Sell cash = cash + invested ; invested = 0; end end returns = cash + invested ; end |
g)
Code Block |
---|
function resboolean = isGoingDown ( n, i, dailyReturn ) resboolean = true ; for j = i: -1: max (1, i-n +1) if ( dailyReturn (j) > 0) res = false ; return ; end end end |
h)
Code Block |
---|
function res = isGoingDown ( n, i, dailyReturn ) res = true ; for j = i: -1: max (1, i-n +1) if ( dailyReturn (j) > 0) resboolean = false ; return ; end end end |
Code Block |
---|
function returns = contrarian ( startAmount , n, closingPrice ) dailyReturn = dailyReturns ( closingPrice ); cash = startAmount ; invested = 0; for i = n: size ( closingPrice , 2); invested = invested * closingPrice (i) / closingPrice (i -1) ; if ( is_going_downisGoingDown (n, i, dailyReturn )) % Buy invested = invested + cash ; cash = 0; else % Sell cash = cash + invested ; invested = 0; end end returns = cash + invested ; end |