[Blotter-commits] r1226 - pkg/FinancialInstrument/inst/parser

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Oct 21 02:31:49 CEST 2012


Author: peter_carl
Date: 2012-10-21 02:31:49 +0200 (Sun, 21 Oct 2012)
New Revision: 1226

Modified:
   pkg/FinancialInstrument/inst/parser/parse.MSCI.R
Log:
- added parsing for multiple columns in spreadsheet


Modified: pkg/FinancialInstrument/inst/parser/parse.MSCI.R
===================================================================
--- pkg/FinancialInstrument/inst/parser/parse.MSCI.R	2012-10-21 00:30:14 UTC (rev 1225)
+++ pkg/FinancialInstrument/inst/parser/parse.MSCI.R	2012-10-21 00:31:49 UTC (rev 1226)
@@ -8,9 +8,15 @@
 # For cases where there are multiple indexes, this script will need to be
 # extended.
 
+# TODO: Modify to take in four indexes in a particular order
+
 # Load needed packages:
-require(zoo)
+require(xts)
+require(PerformanceAnalytics)
 require(gdata)
+require(FinancialInstrument)
+require(quantmod)
+currency("USD")
 
 # Set the working directory, where there's a .incoming folder that contains
 # the downloaded spreadsheet.
@@ -25,9 +31,6 @@
   dir.create(paste(filesroot, "/.incoming", sep=""), mode="0777")
 setwd(paste(filesroot, "/.incoming", sep=""))
 
-if (!file.exists(paste(filesroot, "/MSCI.World.IDX", sep="")))
-  dir.create(paste(filesroot, "/MSCI.World.IDX", sep=""), mode="0777")
-
 if(!file.exists("historyIndex.xls"))
   stop(paste("No spreadsheet exists.  Download the spreadsheet to be processed from http://www.msci.com into ", filesroot, "/.incoming", sep=""))
 
@@ -35,22 +38,36 @@
 x = read.xls("historyIndex.xls", pattern="Date")
 x = x[-((dim(x)[1]-15):dim(x)[1]),] # trim off last 16 lines of disclaimer
 x.dates = paste(substring(x[,1],1,6),substring(x[,1],7,10)) # unmangle the dates
-x.dates = as.Date(x.dates, format="%b %d %Y")
-x.prices = as.numeric((sub(",","", x[,2], fixed=TRUE))) # get rid of commas
-x.xts = xts(x.prices, order.by=x.dates)
-x.returns = Return.calculate(x.xts)
-x.xts = cbind(x.xts, x.returns)
-colnames(x.xts) = c("Close","Returns")
+ISOdates = as.Date(x.dates, format="%b %d %Y")
 
-# Save it into an rda file on the filesystem
-save(x.xts, file=paste(filesroot,"MSCI.World.IDX/MSCI.World.IDX.rda", sep="/"))
+symbolNames = colnames(x[,-1])
+description = paste("MSCI", gsub(pattern = "\\.", replacement = " ", symbolNames))
+description = gsub(pattern = "  ", replacement = " ", description)
+symbolNames = gsub(pattern = ".Standard.*", replacement = "", symbolNames)
+symbolNames = paste("MSCI.", symbolNames, ".M.IDX", sep="")
+symbolNames = gsub(pattern = "\\.\\.", replacement = "\\.", symbolNames) # Remove double dots
 
-# Create currencies first:
-require(FinancialInstrument)
-currency("USD")
+for(i in 1:length(symbolNames)) {
+  # check to make sure directories exist for each symbol
+  dir.create(paste(filesroot, symbolNames[i], sep="/"), showWarnings = FALSE, 
+             recursive = FALSE, mode = "0777")
+}
 
-# Describe the metadata for the index
-instrument("MSCI.World.IDX", currency="USD", multiplier=1, tick_size=.01, start_date="1969-12-31", description="MSCI World Index Standard (Large+Mid Cap) USD", data="CR", source="msci.com", assign_i=TRUE)
+# Parse the columns into individual price objects
+print("Processing columns as symbols...")
+for( i in 1:length(symbolNames)){
+  # x.prices = as.numeric((sub(",","", x[,-1], fixed=TRUE)))
+  x.xts = as.xts(as.numeric(sub(",","", x[,i+1], fixed=TRUE)), order.by=ISOdates)
+  R.xts = Return.calculate(x.xts)
+  x.xts = cbind(x.xts, R.xts)
+  index(x.xts) = as.Date(as.yearmon(index(x.xts)), frac=1) # resets date to last day of the month rather than last business day
+  colnames(x.xts)=c(paste(symbolNames[i],".Close", sep=""), paste(symbolNames[i], ".Returns", sep=""))
+  xtsAttributes(x.xts) <- list(Description = paste(description[i], "Index"))
+  save(x.xts, file=paste(filesroot, symbolNames[i], paste(symbolNames[i], ".rda", sep=""), sep="/"))
+  # Describe the metadata for each index
+  instrument(symbolNames[i], currency="USD", multiplier=1, tick_size=.01, start_date=head(index(x.xts),1), description=paste(description[i], "Index", sep=""), data="CR", source="MSCI", frequency="Monthly", assign_i=TRUE)
+  print(paste(symbolNames[i], description[i]))
+}
 
 # Now, whenever you log in you need to register the instruments.  This
 # might be a line you put into .Rprofile so that it happens automatically:
@@ -58,6 +75,7 @@
 setSymbolLookup.FI(base_dir=filesroot, split_method='common')
 
 # Now you should be able to:
-getSymbols("MSCI.World.IDX")
-chartSeries(Cl(MSCI.World.IDX), theme="white")
-head(MSCI.World.IDX)
\ No newline at end of file
+getSymbols("MSCI.WORLD.M.IDX")
+getSymbols(symbolNames)
+chartSeries(Cl(MSCI.WORLD.M.IDX), theme="white")
+head(MSCI.WORLD.M.IDX)
\ No newline at end of file



More information about the Blotter-commits mailing list