Global Stock Index Watchlist

I am really pretty chuffed with my effort, I spent many hours today working on this R code. I haven’t commented it like I should because it is not yet a complete product. For those who don’t know, R is an open source statistical mathematical programming language, that has taken the data analytic world by storm. I am a novice at writing scripts but enjoy learning; it is my latest hobby.

Here is the output and code which any of you can run on your own PC, the data is from Yahoo. I notice Russia is missing so I must make an effort to include for my next post.

Global Stock Index Watchlist % change

require(quantmod)
require(PerformanceAnalytics)
require(gridExtra)
 
 
# select ticker symbols and time frame from Yahoo World Indexes
index <- c("^AORD",  "^SSEC",	"^HSI",	"^BSESN",	"^JKSE",	"^KLSE",	"^N225",	"^NZ50",	"^STI",	"^KS11",	"^TWII",	"^GSPTSE",	"^GSPC",	"^ATX",	"^BFX",	"^FCHI",	"^GDAXI",	"^SSMI",	"^FTSE",	"GD.AT
")
date_begin <- as.Date("2009-03-01")
date_end <- as.Date("2014-12-31")
 
tickers <- getSymbols(index, from=date_begin, to=date_end, auto.assign=TRUE)
 
dataset <- Ad(get(tickers[1]))
for (i in 2:length(tickers)) { dataset <- merge(dataset, Ad(get(tickers[i]))) }
 
# handle NA values 
data <- na.locf(dataset) # last observation carried forward to get rid of the NA's
data <- na.omit(data) # omit values with NA values
 
# daily returns
daily <- dailyReturn(data[,1])
for (i in 2:ncol(data)) 
    { daily <- merge(daily, dailyReturn(data[,i]))  
    } 
colnames(daily) <- index
day<- tail(daily,n=1)
 
# weekly returns
weekly <- weeklyReturn(data[,1])
for (i in 2:ncol(data)) 
    { weekly <- merge(weekly, weeklyReturn(data[,i]))  
    } 
colnames(weekly) <- index
week<- tail(weekly,n=1)
 
# monthly returns
monthly <- monthlyReturn(data[,1])
for (i in 2:ncol(data)) 
    { monthly <- merge(monthly, monthlyReturn(data[,i]))  
    } 
colnames(monthly) <- index
month<- tail(monthly,n=1)
 
# quarterly returns
quarterly <- quarterlyReturn(data[,1])
for (i in 2:ncol(data)) 
    { quarterly <- merge(quarterly, quarterlyReturn(data[,i]))  
    } 
colnames(quarterly) <- index
quarter<- tail(quarterly,n=1)
 
# Annual returns
annual <- annualReturn(data[,1])
for (i in 2:ncol(data)) 
    { annual <- merge(annual, annualReturn(data[,i]))  
    } 
colnames(annual) <- index
year<- tail(annual,n=1)
 
summary<- rbind(day,week,month,quarter,year)
colnames(summary) <- c("Aussie", "China", "Hong Kong", "India", "Indonesia", "Malaysia", "Japan", "New Zealand", "Singapore", "Korea", "Taiwan", "Canada", "S&P500", "Austria", "Belgium", "France", "Germany", "Swiss", "UK", "Greece")
 
 
# transpose the data
global<- t(summary)
colnames(global)<- c('Daily','Weekly','Monthly','Quarterly','Annual')
global<- as.data.frame(global) 
 
is.num<- sapply(global, is.numeric)
global[is.num]<- lapply(global[is.num], round,4)
global<- global*100
 
print(global)
grid.newpage(recording = FALSE)
grid.table(global)

Created by Pretty R at inside-R.org

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s