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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Sep 7 01:19:23 CEST 2011


Author: gsee
Date: 2011-09-07 01:19:23 +0200 (Wed, 07 Sep 2011)
New Revision: 756

Modified:
   pkg/FinancialInstrument/R/instrument.R
   pkg/FinancialInstrument/R/load.instruments.R
   pkg/FinancialInstrument/R/parse_id.R
   pkg/FinancialInstrument/demo/demo.R
   pkg/FinancialInstrument/man/load.instruments.Rd
Log:
 - parse_id is much better at figuring out root_id/suffix_id when there is no underscore
 - future_series will now magically know if you pass root_ids to the primary_id arg, in 
   which case it will build new primary_ids. This allows load.instruments to work with 
   future_series.csv without making changes to the csv or load.instruments
 - added examples for load.instruments
 - very minor updates to demo


Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R	2011-09-06 21:30:53 UTC (rev 755)
+++ pkg/FinancialInstrument/R/instrument.R	2011-09-06 23:19:23 UTC (rev 756)
@@ -291,6 +291,11 @@
       return(unname(sapply(primary_id, future_series,
           root_id=root_id, suffix_id=suffix_id, first_traded=first_traded, 
           expires=expires, identifiers = identifiers, ...=...)))
+  } else if (is.null(root_id) && !is.null(suffix_id) && parse_id(primary_id)$type == 'root') {
+      #if we have primary_id, but primary_id looks like a root_id, and we have suffix_id and don't have root_id
+      #then primary_id is really root_id and we need to replace primary_id
+      root_id <- primary_id
+      primary_id <- paste(root_id, suffix_id, sep="_")
   }
   pid <- parse_id(primary_id)
   if (is.null(root_id)) root_id <- pid$root

Modified: pkg/FinancialInstrument/R/load.instruments.R
===================================================================
--- pkg/FinancialInstrument/R/load.instruments.R	2011-09-06 21:30:53 UTC (rev 755)
+++ pkg/FinancialInstrument/R/load.instruments.R	2011-09-06 23:19:23 UTC (rev 756)
@@ -42,6 +42,12 @@
 #' \code{\link{setSymbolLookup.FI}} 
 #' \code{\link[quantmod]{getSymbols}} 
 #' \code{\link{getSymbols.FI}}
+#' @examples
+#' \dontrun{
+#' 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'))
+#' }
 #' @export
 load.instruments <- function (file=NULL, ..., metadata=NULL, id_col=1, default_type='stock') {
 

Modified: pkg/FinancialInstrument/R/parse_id.R
===================================================================
--- pkg/FinancialInstrument/R/parse_id.R	2011-09-06 21:30:53 UTC (rev 755)
+++ pkg/FinancialInstrument/R/parse_id.R	2011-09-06 23:19:23 UTC (rev 756)
@@ -64,12 +64,26 @@
         hasdot <- !identical(integer(0),grep("\\.",x))        
         if (!silent) 
             warning("id of future_series should have an underscore in it. Trying to parse anyway.")
-        if (nchar(x) <= 3) {
-            root <- substr(x, 1, nchar(x))
-            suffix <- ""
-        } else if (nchar(x) < 9 && !hasdot) { #assume it's a future like ESU1 or ESU11
-            root <- substr(x,1,2)
-            suffix <- substr(x,3,nchar(x))
+        if (nchar(x) < 9 && !hasdot) { #assume it's a future like ESU1 or ESU11
+            if (!is.null(parse_suffix(substr(x,3,nchar(x)))) && 
+                    !is.na(parse_suffix(substr(x,3,nchar(x)))$format)) {
+                root <- substr(x,1,2)
+                suffix <- substr(x,3,nchar(x))
+            } else if (!is.null(parse_suffix(substr(x,4,nchar(x)))) && 
+                    !is.na(parse_suffix(substr(x,4,nchar(x)))$format)) {
+                root <- substr(x,1,3)        
+                suffix <- substr(x,4,nchar(x))
+            } else if (!is.null(parse_suffix(substr(x,2,nchar(x)))) && 
+                    !is.na(parse_suffix(substr(x,2,nchar(x)))$format)) {
+                root <- substr(x,1,1)
+                suffix <- substr(x,2,nchar(x))
+            } else if (nchar(x) <= 3) {
+                root <- substr(x, 1, nchar(x))
+                suffix <- "" 
+            } else { 
+                root <- substr(x,1,4)
+                suffix <- substr(x,5,nchar(x))
+            }
         } else {
             root <- gsub("[0-9.-]","",x) #now it looks like SPYC or TP
             root <- substr(root, 1,nchar(root)-1)

Modified: pkg/FinancialInstrument/demo/demo.R
===================================================================
--- pkg/FinancialInstrument/demo/demo.R	2011-09-06 21:30:53 UTC (rev 755)
+++ pkg/FinancialInstrument/demo/demo.R	2011-09-06 23:19:23 UTC (rev 756)
@@ -35,9 +35,9 @@
 option_series.yahoo('SPY')
 #option_series.yahoo("SPY",Exp=NULL) # would define all options on SPY
 
-#load.instruments("./FinancialInstrument/data/currencies.csv")
-#load.instruments("./FinancialInstrument/data/root_contracts.csv")
-#load.instruments("./FinancialInstrument/data/future_series.csv")
+#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
 future("ES",'USD',multiplier=50)
@@ -62,10 +62,10 @@
 
 # Define a spread
 getSymbols(c("SPY","DIA")) #download the data for both legs
-SPYDIA.fSB <- fn_SpreadBuilder("SPY","DIA") #build a 2 leg spread with multiple columns
+SPYDIA.fSB <- fn_SpreadBuilder("SPY","DIA", auto.assign=FALSE) #build a 2 leg spread with multiple columns
 #or define the spread first
-spread("SPYDIA", "USD", members=c("SPY","DIA"), memberratio=c(1,-1))
-SPYDIA.bS <- buildSpread("SPYDIA", auto.assign=FALSE) #and build it (could be multiple-leg)
+spread("SPY.DIA", "USD", members=c("SPY","DIA"), memberratio=c(1,-1))
+SPYDIA.bS <- buildSpread("SPY.DIA", auto.assign=FALSE) #and build it (could be multiple-leg)
 
 SPYDIA.rat <- buildRatio(c("SPY","DIA")) #calculate ratios of prices
 

Modified: pkg/FinancialInstrument/man/load.instruments.Rd
===================================================================
--- pkg/FinancialInstrument/man/load.instruments.Rd	2011-09-06 21:30:53 UTC (rev 755)
+++ pkg/FinancialInstrument/man/load.instruments.Rd	2011-09-06 23:19:23 UTC (rev 756)
@@ -53,6 +53,13 @@
   or as custom fields.  See \code{\link{instrument}} for
   more information on custom fields.
 }
+\examples{
+\dontrun{
+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'))
+}
+}
 \seealso{
   \code{\link{instrument}} \code{\link{setSymbolLookup.FI}}
   \code{\link[quantmod]{getSymbols}}



More information about the Blotter-commits mailing list