[Blotter-commits] r763 - in pkg/FinancialInstrument: . R demo man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 9 16:16:46 CEST 2011


Author: gsee
Date: 2011-09-09 16:16:46 +0200 (Fri, 09 Sep 2011)
New Revision: 763

Added:
   pkg/FinancialInstrument/demo/FIdemo2.R
Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/R/format_id.R
   pkg/FinancialInstrument/R/instrument.R
   pkg/FinancialInstrument/demo/00Index
   pkg/FinancialInstrument/man/getInstrument.Rd
   pkg/FinancialInstrument/man/instrument.Rd
Log:
 - if instrument hasArg(src) setSymbolLookup(src) will be called
 - add demo FIdemo2 that shows some newly acceptable ways to define stuff
 - minor patch in format_id.



Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2011-09-08 12:32:01 UTC (rev 762)
+++ pkg/FinancialInstrument/DESCRIPTION	2011-09-09 14:16:46 UTC (rev 763)
@@ -11,7 +11,7 @@
     meta-data and relationships. Provides support for
     multi-asset class and multi-currency portfolios.  
     Still in heavy development.
-Version: 0.6.1
+Version: 0.6.2
 URL: https://r-forge.r-project.org/projects/blotter/
 Date: $Date$
 Depends:

Modified: pkg/FinancialInstrument/R/format_id.R
===================================================================
--- pkg/FinancialInstrument/R/format_id.R	2011-09-08 12:32:01 UTC (rev 762)
+++ pkg/FinancialInstrument/R/format_id.R	2011-09-09 14:16:46 UTC (rev 763)
@@ -50,7 +50,7 @@
                 xxCY=paste(pid$root, paste(substr(suffix,1,2), M2C(pid$month), substr(pid$year,4,4), sep=""), sep=sep),
                 xxCYY=paste(pid$root, paste(substr(suffix,1,2), M2C(pid$month), substr(pid$year,3,4), sep=""), sep=sep), 
                 xxCYYYY=paste(pid$root, paste(substr(suffix,1,2), M2C(pid$month), pid$year, sep=""), sep=sep),
-                NNNN=paste(pid$root, sprintf("%02d", match(pid$month,toupper(month.abb))), substr(pid$year,3,4), sep=sep),
+                NNNN=paste(pid$root, paste(sprintf("%02d", match(pid$month,toupper(month.abb))), substr(pid$year,3,4),sep=""), sep=sep),
                 opt2={
                     if (!any(pid$format == c("opt2","opt4"))) stop("I'm not programmed to convert non-option_series_ids to option_series_ids")
                     ifelse(pid$format == "opt4", paste(pid$root, substr(suffix,3,nchar(suffix)), sep=sep), i)

Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R	2011-09-08 12:32:01 UTC (rev 762)
+++ pkg/FinancialInstrument/R/instrument.R	2011-09-09 14:16:46 UTC (rev 763)
@@ -59,6 +59,8 @@
 #' For example, if you have a \code{stock} with \sQuote{SPY} as the \code{primary_id}, you could use 
 #' \sQuote{.SPY} as the \code{primary_id} of the \code{option} specs, and \sQuote{..SPY} as the 
 #' \code{primary_id} of the single stock \code{future} specs. (or vice versa)
+#'
+#' You can (optionally) provide a \code{src} argument in which case, it will be used unaltered in a call to setSymbolLookup.
 #' @param primary_id string describing the unique ID for the instrument. Most of the wrappers allow this to be a vector.
 #' @param ... any other passthru parameters, including 
 #' @param underlying_id for derivatives, the identifier of the instrument that this one is derived from, may be NULL for cash settled instruments
@@ -106,7 +108,12 @@
           arg<-c(arg,targ)
       }
   }
-  
+  if (!is.null(arg$src)) {
+      sarg <- list()
+      sarg[[primary_id]] <- arg$src
+      setSymbolLookup(sarg)
+      #arg[["src"]]<-NULL
+  }
   #check for identifiers we recognize 
   ident_str<-c("X.RIC","RIC","CUSIP","SEDOL","OSI","Bloomberg","Reuters","ISIN","CQG","TT","Yahoo","Google")
   for(i_s in ident_str ){
@@ -534,10 +541,8 @@
     stop("Must provide either 'primary_id' or both 'currency' and 'counter_currency'")
   if (length(primary_id) > 1) return(unname(sapply(primary_id, exchange_rate, identifiers=identifiers, ...=...)))
   
-  if (is.null(currency) && is.null(counter_currency)) {
-    currency <- substr(primary_id,4,6)
-    counter_currency <- substr(primary_id,1,3)
-  }
+  if (is.null(currency)) currency <- substr(primary_id,4,6)
+  if (is.null(counter_currency)) counter_currency <- substr(primary_id,1,3)
   if(!exists(currency, where=.instrument,inherits=TRUE)) warning(paste("currency",currency,"not found")) # assumes that we know where to look
   if(!exists(counter_currency, where=.instrument,inherits=TRUE)) warning(paste("counter_currency",counter_currency,"not found")) # assumes that we know where to look
 
@@ -610,7 +615,7 @@
 #' @param type type of object to look for. See Details
 #' @examples 
 #' \dontrun{
-#' option('..VX', multiplier=1, 
+#' option('..VX', multiplier=100, 
 #'   underlying_id=future('.VX',multiplier=1000, 
 #'     underlying_id=synthetic('VIX', currency("USD"))))
 #'
@@ -643,7 +648,7 @@
             }
         }
         if (!inherits(tmp_instr,'try-error') && inherits(tmp_instr, type)) return(tmp_instr)
-        if(!silent) warning(paste("Instrument",x," not found, please create it first."))
+        if(!silent) warning(paste(type,x,"not found, please create it first."))
         return(FALSE)
     } else{
         return(tmp_instr)

Modified: pkg/FinancialInstrument/demo/00Index
===================================================================
--- pkg/FinancialInstrument/demo/00Index	2011-09-08 12:32:01 UTC (rev 762)
+++ pkg/FinancialInstrument/demo/00Index	2011-09-09 14:16:46 UTC (rev 763)
@@ -1 +1,2 @@
-demo    FinancialInstrument demo
+demo    FinancialInstrument demo 
+FIdemo2 FinancialInstrument more ways to define instruments

Added: pkg/FinancialInstrument/demo/FIdemo2.R
===================================================================
--- pkg/FinancialInstrument/demo/FIdemo2.R	                        (rev 0)
+++ pkg/FinancialInstrument/demo/FIdemo2.R	2011-09-09 14:16:46 UTC (rev 763)
@@ -0,0 +1,91 @@
+require(FinancialInstrument)
+require(quantmod)
+
+#Similar to the other demo, but demonstrates new functionality
+
+# currencies need to be defined first
+#load.instruments(system.file('data/currencies.csv',package='FinancialInstrument')))
+currency(c("USD","GBP","EUR","JPY"))
+
+# now exchange rates
+exchange_rate(counter_currency=c("GBP","EUR"), currency=c("GBP","JPY","USD"))
+exchange_rate("USDJPY")
+
+# now some stocks 
+stock(c("IBM","SPY","DIA","AAPL"),"USD")
+
+#Contract specs for options
+option(".IBM","USD",multiplier=100,tick_size=0.01, underlying_id="IBM")
+#If we don't provide the currency, it will use the currency of the underlying stock
+option(".SPY",multiplier=100,tick_size=0.01,underlying_id='SPY')
+option(".GOOG",multiplier=100,underlying_id=stock("GOOG","USD")) #underlying can be defined on-the-fly
+
+#Define tradeable option_series instrument 
+# (the expiration date in the suffix_id is a Saturday. 
+# You may want to use the Friday before that in 'expires')
+option_series(root_id="IBM", suffix_id="110716C175", expires="2011-07-15", callput='call',underlying_id='IBM')
+# Also note that even though we defined the root with a primary_id of '.IBM', 
+# 'option_series' will still be able to find it when we call it with root_id='IBM' 
+option_series('IBM_110917C175') #magically figures everthing out...however, since we didn't provide expires, it will be '2011-09' with no day.
+option_series(primary_id='IBM_110917P175', expires='2011-09-16')
+
+#Or use yahoo to help define the specs and all near-term options
+option_series.yahoo('SPY')
+#option_series.yahoo("SPY",Exp=NULL) # would define all options on SPY
+
+#load.instruments(system.file("data/currencies.csv",package='FinancialInstrument'))
+#load.instruments(system.file("data/root_contracts.csv",package='FinancialInstrument'))
+#load.instruments(system.file("data/future_series.csv",package='FinancialInstrument'))
+
+# Define a futures root (don't have to provide currency if the underlying is defined.)
+future("ES",multiplier=50,underlying_id=synthetic("SPX","USD",exchange='CBOE')) #underlying can be defined on the fly.
+future_series(root_id='ES', suffix_id='Z11', expires='2011-12-16')
+# or use magic if you are okay with an 'expires' that is just YYYY-MM
+future_series("ES_U11") 
+#or provide expires and let magic take care of the rest
+future_series("ES_M2", expires='2012-06-15') 
+
+# You can later update the expiration date manually if you like
+getInstrument("ES_U11")
+instrument_attr("ES_U11", 'expires', '2011-09-16')
+getInstrument("ES_U11")
+
+future_series("ESM2") #this works, but it is recommended to use the underscore to avoid ambiguity.
+
+
+##Using the src arg.
+#if you specify 'src' when defining an instrument, a call will be made to setSymbolLookup 
+#so that getSymbols will know where to get the data.
+stock("YHOO",'USD',src='google')
+synthetic("AORD",currency("AUD"),src=list(name='^AORD',src='yahoo')) #we also are defining "AUD" on-the-fly
+exchange_rate("AUDUSD", src=list(name='AUD/USD', src='oanda'))
+#stock("GM","USD",src=list(src='FI',dir='/mnt/data') 
+#now getSymbols will look in appropriate places for data
+getSymbols(c("YHOO","AORD","AUDUSD"), from=Sys.Date()-250)
+showSymbols()
+
+# Define a spread
+getSymbols(c("SPY","DIA")) #download the data for both legs
+SPYDIA.fSB <- fn_SpreadBuilder("SPY","DIA", auto.assign=FALSE) #build a 2 leg spread with multiple columns
+fn_SpreadBuilder(stock(c("CVX","XOM"),"USD")) #define stocks, download data, calculate spread, and define and assign spread
+
+#or define the spread first
+spread("SPY.DIA", "USD", members=c("SPY","DIA"), memberratio=c(1,-1))
+
+#let 'spread' make the primary_id for you. Since both members have the same currency, you don't have to provide it. 
+spread(members=c("AAPL","GOOG"),memberratio=c(1,-1)) 
+SPYDIA.bS <- buildSpread("SPY.DIA", auto.assign=FALSE) # (could be multiple-leg.  See VX fly below)
+buildSpread('AAPL.GOOG')
+
+#VX fly -- need qmao package for the getSymbols.cfe method
+future("VX", "USD", 1000, src='cfe', underlying_id=synthetic("VIX","USD")) #first the root
+butterfly(members=future_series(c('VX_F11','VX_G11','VX_H11'),src='cfe')) #we can define the future_series on-the-fly
+if (require(qmao)) {
+    buildSpread("VX_F11.G11.H11")
+    head(VX_F11.G11.H11)
+}
+
+#ratios of prices
+SPYDIA.rat <- buildRatio(c("SPY","DIA")) #calculate ratios of prices
+
+

Modified: pkg/FinancialInstrument/man/getInstrument.Rd
===================================================================
--- pkg/FinancialInstrument/man/getInstrument.Rd	2011-09-08 12:32:01 UTC (rev 762)
+++ pkg/FinancialInstrument/man/getInstrument.Rd	2011-09-09 14:16:46 UTC (rev 763)
@@ -36,7 +36,7 @@
 }
 \examples{
 \dontrun{
-option('..VX', multiplier=1,
+option('..VX', multiplier=100,
 underlying_id=future('.VX',multiplier=1000,
 underlying_id=synthetic('VIX', currency("USD"))))
 

Modified: pkg/FinancialInstrument/man/instrument.Rd
===================================================================
--- pkg/FinancialInstrument/man/instrument.Rd	2011-09-08 12:32:01 UTC (rev 762)
+++ pkg/FinancialInstrument/man/instrument.Rd	2011-09-09 14:16:46 UTC (rev 763)
@@ -117,6 +117,10 @@
   \code{primary_id} of the \code{option} specs, and
   \sQuote{..SPY} as the \code{primary_id} of the single
   stock \code{future} specs. (or vice versa)
+
+  You can (optionally) provide a \code{src} argument in
+  which case, it will be used unaltered in a call to
+  setSymbolLookup.
 }
 \seealso{
   \code{\link{exchange_rate}} \code{\link{option_series}}



More information about the Blotter-commits mailing list