Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lin Yuan

ISDS570 HW2

Q2.1: What is the expected (average) annualized monthly return and volatility (standard
deviation) of SP500TR between January 2012 and December 2017?

symbol annualized monthly return Volatility (Std.Dev)


SP500T
R 0.1583 0.0959

qry='SELECT * FROM custom_calendar ORDER by date'


ccal<-dbGetQuery(conn,qry)
qry1="SELECT symbol,eod_indices.date,adj_close FROM eod_indices INNER JOIN custom_calendar ON
eod_indices.date = custom_calendar.date WHERE eod_indices.date BETWEEN '2011-12-30' AND '2017-
12-31' and eom=1"
qry2="SELECT ticker,eod_quotes.date,adj_close FROM eod_quotes INNER JOIN custom_calendar ON
eod_quotes.date = custom_calendar.date WHERE eod_quotes.date BETWEEN '2011-12-30' AND '2017-
12-31' and eom=1"
eom<-dbGetQuery(conn,paste(qry1,'UNION',qry2))
head(eom[which(eom$symbol=='SP500TR'),])
eom_row<-data.frame(symbol='SP500TR',date=as.Date('2011-12-30'),adj_close=2158.94)
eom<-rbind(eom,eom_row)
tmonths<-ccal[which(ccal$trading==1) & ccal$eom==1,,drop=F]
head(tmonths)
nrow(tmonths)-1
pct<-table(eom$symbol)/(nrow(tmonths)-1)
selected_symbols_monthly<-names(pct)[which(pct>=0.99)]
eom_complete<-eom[which(eom$symbol %in% selected_symbols_monthly),,drop=F]
eom_pvt<-dcast(eom_complete, date ~ symbol,value.var='adj_close',fun.aggregate = mean, fill=NULL)
eom_pvt_complete<-merge.data.frame(x=tmonths[,'date',drop=F],y=eom_pvt,by='date',all.x=T)
rownames(eom_pvt_complete)<-eom_pvt_complete$date
eom_pvt_complete$date<-NULL
eom_pvt_complete<-na.locf(eom_pvt_complete,na.rm=F,fromLast=F,maxgap=3)
eom_ret<-CalculateReturns(eom_pvt_complete)
eom_ret<-tail(eom_ret,-1)
colMax <- function(data) sapply(data, max, na.rm = TRUE)
max_monthly_ret<-colMax(eom_ret)
selected_symbols_monthly<-names(max_monthly_ret)[which(max_monthly_ret<=1.00)]
length(selected_symbols_monthly)
eom_ret<-eom_ret[,which(colnames(eom_ret) %in% selected_symbols_monthly)]
Rb<-as.xts(eom_ret[,'SP500TR',drop=F]) #benchmark
table.AnnualizedReturns(Rb,scale=12)
Q2.2: Randomly choose three stock tickers which have complete data between 2012
and 2017 in the Quandl Wiki data set and which begin with the same letter as your last
name*. What are their expected annualized monthly returns and volatilities?

symbol annualized monthly returns Volatilities (Std.Dev)


Y 0.1307 0.1422
YORW 0.1422 0.2118
YUM 0.1384 0.1966

qry='SELECT * FROM custom_calendar ORDER by date'


ccal<-dbGetQuery(conn,qry)
qry1="SELECT symbol,eod_indices.date,adj_close FROM eod_indices INNER JOIN custom_calendar ON
eod_indices.date = custom_calendar.date WHERE eod_indices.date BETWEEN '2011-12-30' AND '2017-
12-31' and eom=1"
qry2="SELECT ticker,eod_quotes.date,adj_close FROM eod_quotes INNER JOIN custom_calendar ON
eod_quotes.date = custom_calendar.date WHERE eod_quotes.date BETWEEN '2011-12-30' AND '2017-
12-31' and eom=1"
eom<-dbGetQuery(conn,paste(qry1,'UNION',qry2))
head(eom[which(eom$symbol=='SP500TR'),])
eom_row<-data.frame(symbol='SP500TR',date=as.Date('2011-12-30'),adj_close=2158.94)
eom<-rbind(eom,eom_row)
tmonths<-ccal[which(ccal$trading==1) & ccal$eom==1,,drop=F]
head(tmonths)
nrow(tmonths)-1
pct<-table(eom$symbol)/(nrow(tmonths)-1)
selected_symbols_monthly<-names(pct)[which(pct>=0.99)]
eom_complete<-eom[which(eom$symbol %in% selected_symbols_monthly),,drop=F]
eom_pvt<-dcast(eom_complete, date ~ symbol,value.var='adj_close',fun.aggregate = mean, fill=NULL)
eom_pvt_complete<-merge.data.frame(x=tmonths[,'date',drop=F],y=eom_pvt,by='date',all.x=T)
rownames(eom_pvt_complete)<-eom_pvt_complete$date
eom_pvt_complete$date<-NULL
eom_pvt_complete<-na.locf(eom_pvt_complete,na.rm=F,fromLast=F,maxgap=3)
eom_ret<-CalculateReturns(eom_pvt_complete)
eom_ret<-tail(eom_ret,-1)
colMax <- function(data) sapply(data, max, na.rm = TRUE)
max_monthly_ret<-colMax(eom_ret)
selected_symbols_monthly<-names(max_monthly_ret)[which(max_monthly_ret<=1.00)]
length(selected_symbols_monthly)
eom_ret<-eom_ret[,which(colnames(eom_ret) %in% selected_symbols_monthly)]
Ra<-as.xts(eom_ret[,c('Y','YORW','YUM'),drop=F])
Rb<-as.xts(eom_ret[,'SP500TR',drop=F]) #benchmark
table.AnnualizedReturns(cbind(Rb,Ra),scale=12)
Q2.3: Prepare a chart with cumulative monthly returns for the three tickers and
SP500TR. What was the total cumulative (accumulated) return of SP500TR and your
selected stocks between January 2012 and December 2017?

symbol cumulative returns


Y 1.089418
YORW 1.22001
YUM 1.177027
SP500T
R 1.4145

chart.CumReturns(Ra,legend.loc = 'topleft')
chart.CumReturns(Rb,legend.loc = 'topleft')

You might also like