Lgstk difflogstock4 lgslgstk 1 fit garchfitgarch11

Info iconThis preview shows pages 7–10. Sign up to view the full content.

View Full Document Right Arrow Icon
lgstk <- diff(log(stock[,4])) lgs=lgstk[-1] fit = garchFit(~garch(1,1), data=lgs) predict(fit, n.ahead = 5, plot=T) } #UNIT 5 ############################################################################## #Test the 3-Soldier Candlestick Pattern #Example 5.1 a=getSymbols(x, auto.assign=F) cond1=with(a, ifelse( a[,4]>a[,1] & lag(a[,4],1)>lag(a[,1],1) & lag(a[,4],2)>lag(a[,1],2) , 1, -1) ) cond2=with(a, ifelse( a[,4]> lag(a[,4],1) & lag(a[,4],1)>lag(a[,4],2) , 1, -1) ) cond3=with(a, ifelse( lag(a[,4],2)> lag(a[,1],1) & lag(a[,1],1)>lag(a[,1],2) , 1, -1) ) cond4=with(a, ifelse( lag(a[,4],1)> lag(a[,1],0) & lag(a[,1],0)>lag(a[,1],1) , 1, -1) ) b=merge(cond1, cond2, cond3,cond4) pattern=with(b, ifelse(b[,1]==1 & b[,2]==1 & b[,3]==1 & b[,4]==1, 111, -1)) #111 means FOUND while -1 NOT FOUND sma3=SMA(a[,4] ,3) t=merge(a, sma3) singleDayTrend=with(t, ifelse( t[,1] > lag(t[,1],1), 1, -1)) cumulativeSum=function(vx,n) { with(vx, ifelse(vx == lag(vx,n) &
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(n<20), vx+cumulativeSum(vx,n+1), vx)) } trend=cumulativeSum(singleDayTrend[,1], 1) observedResult= lag( sma3 , -3)- a[,4] k=merge(trend, pattern, observedResult) names(k)[1]=paste("trend") names(k)[2]=paste("pattern") names(k)[3]=paste("result") subset(k, k[,2]==111) #UNIT 6 ############################################################################## #Markowitz Portfolio Theory #calculate the covariance matrix #Example 6.1 a=getSymbols("T", from="2012-03-01", auto.assign=F) #ATT b=getSymbols("GM",from="2012-03-01", auto.assign=F) #GMC c=getSymbols("X", from="2012-03-01", auto.assign=F) #us Steel (USX) a=monthlyReturn(a) b=monthlyReturn(b) c=monthlyReturn(c) mean.a=mean(a); mean.b=mean(b); mean.c=mean(c) sd.a=sd(a); sd.b= sd(b); sd.c= sd(c) var.a= sd.a^2 ; var.b= sd.b^2 ; var.c= sd.c^2 #get covariance matrix a.minus.mu=a - mean.a b.minus.mu=b - mean.b c.minus.mu=c - mean.c a.x.b = a.minus.mu*b.minus.mu b.x.c = b.minus.mu*c.minus.mu c.x.a = c.minus.mu*a.minus.mu covar.a.x.b = sum(a.x.b)/(nrow(a.x.b)-1) covar.b.x.c = sum(b.x.c)/(nrow(b.x.c)-1) covar.c.x.a = sum(c.x.a)/(nrow(c.x.a)-1) covarM = matrix( c( var.a , covar.a.x.b , covar.c.x.a, covar.a.x.b, var.b , covar.b.x.c, covar.c.x.a, covar.b.x.c , var.c ) , nrow=3, ncol=3, byrow=T) # Another Method x=NULL x=cbind(x, a) x=cbind(x, b) x=cbind(x, c) covarM=cov(x) #Calculate the return and variance of a multi asset portfolio #Example 6.2 #
Background image of page 8
# MeanReturns, covarM and porfolio allocation are given for four stocks A, B, C, D # # Asset A , Asset B , Asset c, Asset D MeanReturns=c(0.177416667, 0.216166667, 0.19625, 0.05 ) covarM= matrix( c( 0.0412268, 0.0226614 , 0.0305563, 3.502E-35, 0.0226614, 0.047918 , 0.015001, -2.45e-34 , 0.0305628, 0.015001 , 0.090306, -1.4e-34 , 3.50169E-35,-2.45118E-34,-1.40068E-34,5.25254E-35), nrow=4, ncol=4, byrow=T) Allocation=c(0.136, 0.392, 0.120, 0.352) # We can calculate the porfolio mean and variance as below # PorfolioMean=sum(MeanReturns* Allocation) >PorfolioMean [1] 0.150016 PorfolioVariance = sum((covarM %*% Allocation)*Allocation) > PorfolioVariance [1] 0.01425122
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page7 / 15

lgstk difflogstock4 lgslgstk 1 fit garchFitgarch11 datalgs...

This preview shows document pages 7 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online