\chapter{Code}
\label{ch:Rcode}

\begin{verbatim}

datafiles<- read.table("datanodates.csv", header = TRUE, sep = ",", na.string=".") 
datareturns<- read.table("datanodates2.csv", header = TRUE, sep = ",", na.string=".") 

SPX<-datareturns[2]
CCMP<-datareturns[3]
INDU<-datareturns[4]
T<-datareturns[5]
MSFT<-datareturns[6]
PTR<-datareturns[7]
Y<-data.frame(SPX, CCMP, INDU, T, MSFT, PTR)

x <- seq(as.Date("2005-01-01"), as.Date("2009-10-31"), by = "day")
plot(x[1:length(Y$SPX)],Y$SPX, xlab = "Time", ylab = "Last Price", main = "S&P Price (2005-2009)",type="l")
####################

SPX<-datafiles[1]
CCMP<-datafiles[2]
INDU<-datafiles[3]
T<-datafiles[4]
MSFT<-datafiles[5]
PTR<-datafiles[6]
Y<-data.frame(SPX, CCMP, INDU, T, MSFT, PTR)

par(mfrow = c(3,2))
xname<-"S&P Daily Returns (2005-2009)"
hist(Y$SPX,breaks = "FD",main = paste("Histogram of" , xname),xlab = xname)

xname<-"NASDAQ Daily Returns (2005-2009)"
hist(Y$CCMP,breaks = "FD",main = paste("Histogram of" , xname),xlab = xname)

xname<-"DOW Daily Returns (2005-2009)"
hist(Y$INDU,breaks = "FD",main = paste("Histogram of" , xname),xlab = xname)

xname<-"AT&T Daily Returns (2005-2009)"
hist(Y$T,breaks = "FD",main = paste("Histogram of" , xname),xlab = xname)

xname<-"MICROSOFT Daily Returns (2005-2009)"
hist(Y$MSFT,breaks = "FD",main = paste("Histogram of" , xname),xlab = xname)

xname<-"PETROCHINA Daily Returns (2005-2009)"
hist(Y$PTR,breaks = "FD",main = paste("Histogram of" , xname),xlab = xname)

###############################
par(mfrow = c(3,2))
xname<-"S&P Daily Returns (2005-2009)"
plot(density(Y$SPX, bw="ucv",kernel = "epanechnikov"),main = paste("Density Estimation of" , xname),xlab = xname)
#X<-seq(min(Y$SPX), max(Y$SPX), length=length(Y$SPX))

xname<-"NASDAQ Daily Returns (2005-2009)"
plot(density(Y$CCMP, bw="ucv",kernel = "epanechnikov"),main = paste("Density Estimation of" , xname),xlab = xname)
#X<-seq(min(Y$CCMP), max(Y$CCMP), length=length(Y$CCMP))

xname<-"DOW JONES Daily Returns (2005-2009)"
plot(density(Y$INDU, bw="ucv",kernel = "epanechnikov"),main = paste("Density Estimation of" , xname),xlab = xname)
#X<-seq(min(Y$INDU), max(Y$INDU), length=length(Y$INDU))

xname<-"AT&T Daily Returns (2005-2009)"
plot(density(Y$T, bw="ucv",kernel = "epanechnikov"),main = paste("Density Estimation of" , xname),xlab = xname)
#X<-seq(min(Y$T), max(Y$T), length=length(Y$T))

xname<-"MICROSOFT Daily Returns (2005-2009)"
plot(density(Y$MSFT, bw="ucv",kernel = "epanechnikov"),main = paste("Density Estimation of" , xname),xlab = xname)
#X<-seq(min(Y$MSFT), max(Y$MSFT), length=length(Y$MSFT))

xname<-"PETROCHINA Daily Returns (2005-2009)"
plot(density(Y$PTR, bw="ucv",kernel = "epanechnikov"),main = paste("Density Estimation of" , xname),xlab = xname)
#X<-seq(min(Y$PTR), max(Y$PTR), length=length(Y$PTR))

############
library(VaR)
var1<-VaR.norm(Y$SPX[1:1230], p = 0.95, dt = 1)
var2<-VaR.norm(Y$CCMP[1:1230], p = 0.95, dt = 1)
var3<-VaR.norm(Y$INDU[1:1230], p = 0.95, dt = 1)
var4<-VaR.norm(Y$T[1:1230], p = 0.95, dt = 1)
var5<-VaR.norm(Y$MSFT[1:1230], p = 0.95, dt = 1)
var6<-VaR.norm(Y$PTR[1:1230], p = 0.95, dt = 1)

\end{verbatim}