[Blotter-commits] r769 - pkg/FinancialInstrument/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Sep 11 19:55:23 CEST 2011
Author: gsee
Date: 2011-09-11 19:55:23 +0200 (Sun, 11 Sep 2011)
New Revision: 769
Modified:
pkg/FinancialInstrument/R/parse_id.R
pkg/FinancialInstrument/R/synthetic.R
Log:
- improved parsing of spread ids
- tweak how guaranteed_spread gets the root contract
Modified: pkg/FinancialInstrument/R/parse_id.R
===================================================================
--- pkg/FinancialInstrument/R/parse_id.R 2011-09-11 03:20:43 UTC (rev 768)
+++ pkg/FinancialInstrument/R/parse_id.R 2011-09-11 17:55:23 UTC (rev 769)
@@ -25,7 +25,7 @@
#' parse_id("SPY_111217C130")
#' @export
parse_id <- function(x, silent=TRUE, root=NULL) {
- sufftype <- TRUE #will we use the type given by parse_suffix, or overwrite it with e.g. 'exchange_rate'
+ sufftype <- TRUE #will we use the type given by parse_suffix, or overwrite it with e.g. 'exchange_rate', or 'synthetic'
if (!is.null(root)) {
suffix <- gsub(root,"",x) #turns ESU1 into U1, or ES_U11 into _U11
suffix <- gsub("_","",suffix) #take out the underscore if there is one
@@ -49,8 +49,10 @@
} else if (identical(all.equal(nchar(x) - nchar( gsub("\\.","",x)),2), TRUE)) {
#2 dots, so we'll treat it as a fly, although it could be a basket
#SPY.DIA.QQQ,
- suffix <- x
+ suffix <- ""
root <- x
+ type <- 'synthetic'
+ sufftype <- FALSE
} else {
root <- x
suffix <- ""
@@ -91,9 +93,45 @@
root <- substr(root, 1,nchar(root)-1)
suffix <- gsub(root,"",x) #whatever isn't the root
}
- } else { #there _is_ an underscore
- root <- strsplit(x,"_")[[1]][1]
- suffix <- strsplit(x,"_")[[1]][2]
+ } else { #there _is_ an underscore and at least 1 number.
+ #if there are dots then maybe it is a spread of futures?
+ #e.g. "CL_N1.HO_M1"
+ ss <- strsplit(x,"\\.")[[1]]
+ has.und <- function(x) { #TRUE if it has an underscore
+ sapply(x, FUN=function(x) !identical(x, gsub('_','',x)))
+ }
+ if (all(has.und(ss))) { #all parts have an underscore.
+ #e.g. CL_N1.HO_M1 --> "CL_N1" "HO_M1"
+ #or CL_N1.CL_M1
+ ssu <- strsplit(ss,"_")
+ tmprt <- ssu[[1]][1]
+ if (all(sapply(ssu, FUN=function(x) x[1] == tmprt))) {
+ #all share a root_id
+ return(parse_id(make_spread_id(ss)))
+ } else {
+ #all parts have an underscore, but they don't share a root.
+ #e.g. "CL_N1.HO_M1"
+ root <- x
+ suffix <- ""
+ type <- 'synthetic'
+ sufftype <- FALSE
+ }
+ } else if (has.und(ss[1]) && !has.und(ss[2])) {
+ #First part has underscore, but second doesn't. e.g. CL_H1.M1 --> "CL_H1" "M1"
+ spl.und <- strsplit(x,"_")[[1]]
+ root <- spl.und[1]
+ suffix <- paste(spl.und[2:length(spl.und)],collapse=".")
+ } else {
+ if (length(ss) > 1) {
+ suffix <- ""
+ root <- x
+ type <- 'synthetic'
+ sufftype <- FALSE
+ } else { #ES_U1, ES_U1M1,
+ root <- strsplit(x,"_")[[1]][1]
+ suffix <- strsplit(x,"_")[[1]][2]
+ }
+ }
}
suff <- parse_suffix(suffix, silent=silent)
if (sufftype) type <- suff$type
@@ -139,7 +177,7 @@
#' parse_suffix("110917C125")
#' @export
parse_suffix <- function(x, silent=TRUE) {
-#TODO better support for spreads and flies; inter and intra.
+#TODO better support for spreads and flies; inter and intra. Maybe add a 'members' to suffix.list
type <- 'outright'
cm <- FALSE
cc <- FALSE
Modified: pkg/FinancialInstrument/R/synthetic.R
===================================================================
--- pkg/FinancialInstrument/R/synthetic.R 2011-09-11 03:20:43 UTC (rev 768)
+++ pkg/FinancialInstrument/R/synthetic.R 2011-09-11 17:55:23 UTC (rev 769)
@@ -168,6 +168,16 @@
{
##TODO: butterfly can refer to expirations (futures) or strikes (options)
##TODO: A butterfly could either have 3 members that are outrights, or 2 members that are spreads
+ if (missing(members)) {
+ pid <- parse_id(primary_id)
+ root_id <- pid$root
+ suffix_id <- pid$suffix
+ #synthetic flies will have a root_id that looks like "
+ if (suffix_id == "")
+ members <- unlist(strsplit(root_id, "[-;:,\\.]"))
+ else members <- paste(root_id, unlist(strsplit(suffix_id, "[-;:_,\\.]")), sep="_")
+ }
+
if (length(members) == 3) {
synthetic.instrument(primary_id=primary_id,currency=currency,members=members,
memberratio=c(1,-2,1), multiplier=1, tick_size=tick_size,
@@ -209,7 +219,9 @@
}
# go get other instrument quantities from the root contract
- root_contract<-try(getInstrument(root_id,silent=TRUE))
+ root_contract<-try(getInstrument(root_id,silent=TRUE,type='future'))
+ if (inherits(root_contract, 'try-error'))
+ root_contract <-try(getInstrument(root_id,silent=TRUE,type='option'))
if(is.instrument(root_contract)){
if(is.null(currency)) currency <- root_contract$currency
if(is.null(multiplier)) multiplier <- root_contract$multiplier
More information about the Blotter-commits
mailing list