Lab 2 - Using R to Compare Stocks Performance

  1. To reproduce the chart shown in the lecture for the comparison the performance of FOUR stocks CocaCola (KO), Apple (AAPL), Mc Donalds (MCD), General Electric (GE), and the Value Line Geometric Index
    as reference.
    1.1 In R, create a new script and type the following R codes.
library("quantmod")
symbols=c('^VLIC','GE','KO','AAPL','MCD')
getSymbols(symbols,src='yahoo',from="2012-02-01",to="2013-02-01")
#obtain adjusted closed
VLICad = VLIC$VLIC.Adjusted; GEad= GE$GE.Adjusted;
KOad=KO$KO.Adjusted; AAPLad=AAPL$AAPL.Adjusted;
MCDad = MCD$MCD.Adjusted
#compute cumulative sum (cumsum) of daily returns (Delt)
#Remove first term of the series, with [-1,], since cumsum is not defined
for it.
vl = cumsum((Delt(VLICad)*100)[-1,])
ge = cumsum((Delt(GEad)*100)[-1,])
ko = cumsum((Delt(KOad)*100)[-1,])
ap = cumsum((Delt(AAPLad)*100)[-1,])
md = cumsum((Delt(MCDad)*100)[-1,])
###range of values for the plot
lim = c(min(vl,ge,ko,ap,md),max(vl,ge,ko,ap,md))
###the plot
plot(vl,main="",ylim=lim,xlab="dates",ylab="% benefits")
lines(ge,col="green"); lines(ko,col="red")
lines(ap,col="violet"); lines(md,col="yellow")
legend(x="topleft",cex=0.4,c("VLIC","GE","KO","AAPL","MCD"),
lty=1, col=c("black","green","red","violet","yellow"))

你可能感兴趣的:(Lab 2 - Using R to Compare Stocks Performance)