a)

closingPrices = [100 101 102 100 102 104 103 98 96 101];

b)

plot(1:10, closingPrices);

c)

function dailyReturn = dailyReturns ( closingPrices )
    n = size(closingPrices , 2);
    dailyReturn = zeros(1, n);
    for i = 2:n
        dailyReturn(i)= closingPrices(i)-closingPrices(i-1);
    end
end

 

d)

plot(1:10,dailyReturns(closingPrices);

e)

function boolean = isGoingUp(n, i, dailyReturn)
    boolean = true; 
    for j = i : -1: max(1, i-n+1)
        if dailyReturn(j) < 0
            boolean = false;
            return;
        end
    end
end

f)

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)

function boolean = isGoingDown(n, i, dailyReturn)
    boolean = true; 
    for j = i:-1:max(1, i-n+1)
        if dailyReturn(j) > 0
            boolean = false;
            return;
        end
    end
end
 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 ( isGoingDown (n, i, dailyReturn ))
            % Buy
            invested = invested + cash ;
            cash = 0;
        else
            % Sell
            cash = cash + invested ;
            invested = 0;
        end
    end
    returns = cash + invested ;
end
  • No labels