[Blotter-commits] r1087 - in pkg/FinancialInstrument: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jun 28 17:12:02 CEST 2012
Author: gsee
Date: 2012-06-28 17:12:01 +0200 (Thu, 28 Jun 2012)
New Revision: 1087
Modified:
pkg/FinancialInstrument/R/FinancialInstrument-package.R
pkg/FinancialInstrument/R/all.equal.instrument.R
pkg/FinancialInstrument/R/buildSpread.R
pkg/FinancialInstrument/R/instrument.R
pkg/FinancialInstrument/man/FinancialInstrument-package.Rd
pkg/FinancialInstrument/man/add.defined.by.Rd
pkg/FinancialInstrument/man/add.identifier.Rd
pkg/FinancialInstrument/man/all.equal.instrument.Rd
pkg/FinancialInstrument/man/buildSpread.Rd
pkg/FinancialInstrument/man/fn_SpreadBuilder.Rd
pkg/FinancialInstrument/man/getInstrument.Rd
pkg/FinancialInstrument/man/instrument.Rd
pkg/FinancialInstrument/man/instrument.auto.Rd
pkg/FinancialInstrument/man/instrument_attr.Rd
pkg/FinancialInstrument/man/make_spread_id.Rd
pkg/FinancialInstrument/man/option_series.yahoo.Rd
pkg/FinancialInstrument/man/series_instrument.Rd
Log:
- Updates to docs
- formatting related to trying to get margin down to 80
Modified: pkg/FinancialInstrument/R/FinancialInstrument-package.R
===================================================================
--- pkg/FinancialInstrument/R/FinancialInstrument-package.R 2012-06-28 13:41:41 UTC (rev 1086)
+++ pkg/FinancialInstrument/R/FinancialInstrument-package.R 2012-06-28 15:12:01 UTC (rev 1087)
@@ -60,10 +60,10 @@
#' attribute after the instrument has been created using
#' \code{\link{instrument_attr}} as shown in the examples section below.
#'
-#' Defining instruments can be tedious, so we've also included a CSV loader in
-#' the package (See \code{\link{load.instruments}}, as well as some functions
+#' Defining instruments can be tedious, so we've also included a CSV loader,
+#' \code{\link{load.instruments}}, in the package, as well as some functions
#' that will update instruments with data downloaded from the internet.
-#' See, e.g. \code{\link{update_instruments.yahoo}},
+#' See, e.g., \code{\link{update_instruments.yahoo}},
#' \code{\link{update_instruments.TTR}},
#' \code{\link{update_instruments.morningstar}},
#' \code{\link{update_instruments.iShares}}. You can also update an instrument
Modified: pkg/FinancialInstrument/R/all.equal.instrument.R
===================================================================
--- pkg/FinancialInstrument/R/all.equal.instrument.R 2012-06-28 13:41:41 UTC (rev 1086)
+++ pkg/FinancialInstrument/R/all.equal.instrument.R 2012-06-28 15:12:01 UTC (rev 1087)
@@ -15,6 +15,9 @@
#' instrument all.equal method
#'
+#' This is most useful for seeing the difference between two \code{instrument}
+#' objects.
+#'
#' @param char.n If length of a character vector is \code{char.n} or less it
#' will be treated as a single element. A negative value for \code{char.n} will
#' be treated as if it were positive \code{Inf}.
@@ -154,4 +157,4 @@
if (is.null(msg)) {
TRUE
} else msg
-}
\ No newline at end of file
+}
Modified: pkg/FinancialInstrument/R/buildSpread.R
===================================================================
--- pkg/FinancialInstrument/R/buildSpread.R 2012-06-28 13:41:41 UTC (rev 1086)
+++ pkg/FinancialInstrument/R/buildSpread.R 2012-06-28 15:12:01 UTC (rev 1087)
@@ -1,29 +1,38 @@
-#' construct a price/level series for pre-defined multi-leg spread instrument
+#' Construct a price/level series for pre-defined multi-leg spread instrument
#'
-#' build price series for spreads, butterflies, or other synthetic instruments,
+#' Build price series for spreads, butterflies, or other synthetic instruments,
#' using metadata of a previously defined synthetic instrument.
#'
#' The spread and all legs must be defined instruments.
#'
-#' This function can build multileg spreads such as calendars, butterflies, condors, etc.
-#' However, the returned series will be univariate. It does not build Bid Ask Mid data
-#' like fn_SpreadBuilder does.
+#' This function can build multileg spreads such as calendars, butterflies,
+#' condors, etc. However, the returned series will be univariate. It does not
+#' return multiple columns (e.g. \sQuote{Bid}, \sQuote{Ask}, \sQuote{Mid}) like
+#' \code{\link{fn_SpreadBuilder}} does.
#'
+#' \code{buildBasket} is an alias
+#'
#' TODO: allow for multiplier (divisor) that is a vector.
-#' @param spread_id The name of the instrument that contains members and memberratio
-#' @param Dates date range to subset on, will be used for \code{\link[quantmod]{getSymbols}}
-#' if the instrument is not available via \code{\link{get}}
+#' @param spread_id The name of the \code{instrument} that contains members and
+#' memberratio
+#' @param Dates Date range on which to subset. Also, if a member's data is not
+#' available via \code{\link{get}} \code{\link[quantmod]{getSymbols}} will be
+#' called, and the values of the \code{from} and \code{to} arguments will be
+#' determined using \code{\link[xts]{.parseISO8601}} on \code{Dates}.
#' @param onelot Should the series be divided by the first leg's ratio?
-#' @param prefer price column to use to build structure.
-#' @param auto.assign assign the spread? If FALSE, the xts object will be returned
-#' @param env environment in which to assign spread.
+#' @param prefer Price column to use to build structure.
+#' @param auto.assign Assign the spread? If FALSE, the xts object will be
+#' returned.
+#' @param env Environment in which to assign spread data.
#' @return If \code{auto.assign} is FALSE, a univariate xts object.
-#' Otherwise, the xts object will be assigned to \code{spread_id} and the \code{spread_id} will be returned.
+#' Otherwise, the xts object will be assigned to \code{spread_id} and the
+#' \code{spread_id} will be returned.
#' @seealso
#' \code{\link{fn_SpreadBuilder}}
#' \code{\link{spread}} for instructions on defining the spread
#' @author Brian Peterson, Garrett See
-#' @note this could also be used to build a basket or a strip by using only positive values in memberratio
+#' @note this could also be used to build a basket or a strip by using only
+#' positive values in memberratio
#' @examples
#' \dontrun{
#' currency("USD")
@@ -145,39 +154,51 @@
#' Calculate prices of a spread from 2 instruments.
#'
-#' Given 2 products, calculate spread values for as many columns as practicable
+#' Given 2 products, calculate spread values for as many columns as practicable.
#'
-#' \code{prod1} and \code{prod2} can be the names of instruments, or the xts objects themselves.
-#' Alternatively, \code{prod2} can be omitted, and a vector of 2 instrument names can be given to \code{prod1}.
-#' See the last example for this usage.
+#' \code{prod1} and \code{prod2} can be the names of instruments, or the xts
+#' objects themselves. Alternatively, \code{prod2} can be omitted, and a vector
+#' of 2 instrument names can be given to \code{prod1}. See the last example for
+#' this usage.
#'
-#' If \code{prod1} and \code{prod2} are names (not xts data), it will try to get data for \code{prod1} and \code{prod2}
-#' from \code{env} (.GlobalEnv by default). If it cannot find the data, it will get it with a call to getSymbols.
-#' Prices are multiplied by multipliers and exchange rates to get notional values in the currency specified.
-#' The second leg's notional values are multiplied by \code{ratio}.
-#' Then the difference is taken between the notionals of leg1 and the new values for leg2.
+#' If \code{prod1} and \code{prod2} are names (not xts data), it will try to get
+#' data for \code{prod1} and \code{prod2} from \code{env} (.GlobalEnv by
+#' default). If it cannot find the data, it will get it with a call to
+#' getSymbols. Prices are multiplied by multipliers and exchange rates to get
+#' notional values in the currency specified. The second leg's notional values
+#' are multiplied by \code{ratio}. Then the difference is taken between the
+#' notionals of leg1 and the new values for leg2.
#'
#' \sQuote{make.index.unique} uses the xts function \code{make.index.unique}
-#' \sQuote{least.liq} subsets the spread time series, by using the timestamps of the leg that has the fewest rows.
+#' \sQuote{least.liq} subsets the spread time series, by using the timestamps
+#' of the leg that has the fewest rows.
#' \sQuote{duplicated} removes any duplicate indexes.
-#' \sQuote{price.change} only return rows where there was a price change in the Bid, Mid or Ask Price of the spread.
+#' \sQuote{price.change} only return rows where there was a price change in the
+#' Bid, Mid or Ask Price of the spread.
#'
-#' @param prod1 chr name of instrument that will be the 1st leg of a 2 leg spread (Can also be xts data for first product)
-#' @param prod2 chr name of instrument that will be the 2nd leg of a 2 leg spread (Can also be xts data for second product)
-#' @param ratio hedge ratio. Can be a single number, or a vector of same length as data.
+#' @param prod1 chr name of instrument that will be the 1st leg of a 2 leg
+#' spread (Can also be xts data for first product)
+#' @param prod2 chr name of instrument that will be the 2nd leg of a 2 leg
+#' spread (Can also be xts data for second product)
+#' @param ratio Hedge ratio. Can be a single number, or a vector of same length
+#' as data.
#' @param currency chr name of currency denomination of the spread
#' @param from from Date to pass through to getSymbols if needed.
#' @param to to Date to pass through to getSymbols if needed.
-#' @param session_times ISO-8601 time subset for the session time, in GMT, in the format 'T08:00/T14:59'
+#' @param session_times ISO-8601 time subset for the session time, in GMT, in
+#' the format 'T08:00/T14:59'
#' @param notional TRUE/FALSE. Should the prices be multiplied by contract
#' multipliers before calculating the spread?
#' @param unique_method method for making the time series unique
-#' @param auto.assign if \code{TRUE} (the default) the constructed spread will be stored in symbol created with \code{\link{make_spread_id}}. instrument metadata will also be created and stored with the same primary_id.
-#' @param env if \code{prod1} and \code{prod1} are character, this is where to
+#' @param auto.assign If \code{TRUE} (the default) the constructed spread will
+#' be stored in symbol created with \code{\link{make_spread_id}}. instrument
+#' metadata will also be created and stored with the same primary_id.
+#' @param env If \code{prod1} and \code{prod1} are character, this is where to
#' \code{get} the data. Also, if \code{auto.assign} is \code{TRUE} this is
#' the environment in which to store the data (.GlobalEnv by default)
#' @param silent silence warnings? (FALSE by default)
-#' @param \dots other arguments to pass to \code{getSymbols} and/or \code{\link{make_spread_id}}
+#' @param \dots other arguments to pass to \code{getSymbols} and/or
+#' \code{\link{make_spread_id}}
#' @return
#' an xts object with
#' Bid, Ask, Mid columns,
@@ -216,10 +237,6 @@
silent=FALSE, auto.assign=TRUE, env=.GlobalEnv, ...)
{
##TODO: allow for different methods for calculating Bid and Ask
- if (!("package:quantmod" %in% search() || require("quantmod",quietly=TRUE))) {
- stop("Please install quantmod before using this function.")
- }
-
dargs <- list(...)
# dots may have args for getSymbols or for make_spread_id.
# extract args that should be passed to make_spread_id
@@ -271,8 +288,10 @@
}
prod2.instr <- list(multiplier=1,currency=currency)
}
- if (is.null(Data.1)) Data.1 <- try(get(as.character(prod1), pos=env),silent=TRUE)
- if (is.null(Data.2)) Data.2 <- try(get(as.character(prod2), pos=env),silent=TRUE)
+ if (is.null(Data.1)) Data.1 <- try(get(as.character(prod1), pos=env),
+ silent=TRUE)
+ if (is.null(Data.2)) Data.2 <- try(get(as.character(prod2), pos=env),
+ silent=TRUE)
if (inherits(Data.1, "try-error") || (inherits(Data.2, "try-error"))) {
gS.args <- list()
@@ -290,7 +309,9 @@
if ( (all(has.Op(Data.1), has.Cl(Data.2)) && !(all(has.Op(Data.2), has.Cl(Data.2)))) ||
(is.BBO(Data.1) && !is.BBO(Data.2)) ||
(!(all(has.Op(Data.1), has.Cl(Data.2))) && (all(has.Op(Data.2), has.Cl(Data.2)))) ||
- (!is.BBO(Data.1) && is.BBO(Data.2)) ) stop('prod1 and prod2 must be the same types of data (BBO,OHLC,etc.)')
+ (!is.BBO(Data.1) && is.BBO(Data.2)) ) {
+ stop('prod1 and prod2 must be the same types of data (BBO,OHLC,etc.)')
+ }
if (is.null(from)) from <- max(index(first(Data.1)),index(first(Data.2)))
if (is.null(to)) to <- min(index(last(Data.1)),index(last(Data.2)))
@@ -311,11 +332,14 @@
#Determine what type of data it is
if (all(has.Op(Data.1), has.Cl(Data.1), has.Ad(Data.1))) {
- M <- merge(Op(Data.1)[,1],Cl(Data.1)[,1],Ad(Data.1)[,1],Op(Data.2)[,1],Cl(Data.2)[,1],Ad(Data.2)[,1])
- colnames(M) <- c("Open.Price.1","Close.Price.1","Adjusted.Price.1","Open.Price.2","Close.Price.2","Adjusted.Price.2")
+ M <- merge(Op(Data.1)[,1],Cl(Data.1)[,1],Ad(Data.1)[,1],Op(Data.2)[,1],
+ Cl(Data.2)[,1],Ad(Data.2)[,1])
+ colnames(M) <- c("Open.Price.1","Close.Price.1","Adjusted.Price.1",
+ "Open.Price.2","Close.Price.2","Adjusted.Price.2")
} else if(all(has.Op(Data.1), has.Cl(Data.1))) {
M <- merge(Op(Data.1)[,1],Cl(Data.1)[,1],Op(Data.2)[,1],Cl(Data.2)[,1])
- colnames(M) <- c("Open.Price.1","Close.Price.1","Open.Price.2","Close.Price.2")
+ colnames(M) <- c("Open.Price.1","Close.Price.1","Open.Price.2",
+ "Close.Price.2")
} else if (is.BBO(Data.1)) {
M <- merge(Data.1[,c( grep('Bid',colnames(Data.1),ignore.case=TRUE)[1],
grep('Ask',colnames(Data.1),ignore.case=TRUE)[1])],
@@ -412,23 +436,31 @@
if (auto.assign) { #store the data in
msi.args$x <- c(prod1,prod2)
id <- do.call("make_spread_id", msi.args) #can pass 'root' or 'format' through dots
- memberratio <- if(length(ratio) > 1) {list(1,-as.numeric(ratio))} else c(1,-ratio)
- spread(id, currency=currency, members=msi.args$x, memberratio=memberratio, defined.by='fn_SpreadBuilder')
+ memberratio <- if(length(ratio) > 1) {
+ list(1,-as.numeric(ratio))
+ } else c(1,-ratio)
+ spread(id, currency=currency, members=msi.args$x,
+ memberratio=memberratio, defined.by='fn_SpreadBuilder')
assign(id, Spread, pos=env)
id
} else Spread
}
-#' construct a primary_id for a spread instrument from the primary_ids of its members
+#' Construct a primary_id for a \code{spread} \code{instrument} from the
+#' primary_ids of its members
#'
-#' @param x vector of member primary_ids
-#' @param root optional character string of root_id to use.
-#' @param format string indicating how to format the suffix_ids of the spread. If \code{NULL} (the default), or \code{FALSE}, no formatting will be done. the See \code{\link{format_id}} for other accepted values for \code{format}
+#' @param x character vector of member primary_ids
+#' @param root Optional character string of root_id to use.
+#' @param format String indicating how to format the suffix_ids of the spread.
+#' If \code{NULL} (the default), or \code{FALSE}, no formatting will be done.
+#' See \code{\link{format_id}} for other accepted values for \code{format}
#' @param sep character string to separate root_id and suffix_id
-#' @return character string that can be used as a primary_id for a \code{\link{spread}} instrument
+#' @return character string that can be used as a primary_id for a
+#' \code{\link{spread}} instrument
#' @author Garrett See
-#' @seealso \code{\link{spread}}, \code{\link{build_spread_symbols}}, \code{\link{build_series_symbols}}
+#' @seealso \code{\link{spread}}, \code{\link{build_spread_symbols}},
+#' \code{\link{build_series_symbols}}
#' @examples
#' ids <- c('VX_aug1','VX_U11')
#' make_spread_id(ids, format='CY')
@@ -442,7 +474,8 @@
}
if (length(root) != 1) return(paste(format_id(x, format=format),collapse="."))
suff <- paste(unlist(unique(sapply(x,parse_id)['suffix',])),collapse='.')
- #if (is.character(format)) suff <- paste(sapply(strsplit(suff,"\\.")[[1]], format_id, format=format, parse='suffix'), collapse=".")
+ #if (is.character(format)) suff <- paste(sapply(strsplit(suff,"\\.")[[1]],
+ # format_id, format=format, parse='suffix'), collapse=".")
if (!is.null(format) && is.character(format))
suff <- paste(format_id(strsplit(suff,"\\.")[[1]], format=format, parse='suffix'), collapse=".")
id <- paste(root,suff, sep=sep)
@@ -452,9 +485,11 @@
#' format the price of a synthetic instrument
#'
-#' Divides the notional spread price by the spread multiplier and rounds prices to the nearest \code{tick_size}.
+#' Divides the notional spread price by the spread multiplier and rounds prices
+#' to the nearest \code{tick_size}.
#' @param x xts price series
-#' @param multiplier numeric multiplier (e.g. 1000 for crack spread to get from $ to $/bbl)
+#' @param multiplier numeric multiplier (e.g. 1000 for crack spread to get
+#' from $ to $/bbl)
#' @param tick_size minimum price change of the spread
#' @return price series of same length as \code{x}
#' @author Garrett See
Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R 2012-06-28 13:41:41 UTC (rev 1086)
+++ pkg/FinancialInstrument/R/instrument.R 2012-06-28 15:12:01 UTC (rev 1087)
@@ -61,51 +61,74 @@
#' instrument class constructors
#'
-#' All 'currency' instruments must be defined before instruments of other types may be defined.
+#' All 'currency' instruments must be defined before instruments of other types
+#' may be defined.
#'
-#' In \dots you may pass any other arbitrary instrument fields that will be used to create 'custom' fields.
-#' S3 classes in \R are basically lists with a class attribute.
-#' We use this to our advantage to allow us to set arbitrary fields.
+#' In \dots you may pass any other arbitrary instrument fields that will be used
+#' to create 'custom' fields. S3 classes in \R are basically lists with a class
+#' attribute. We use this to our advantage to allow us to set arbitrary fields.
#'
-#' \code{identifiers} should be a named list to specify other identifiers beyond the \code{primary_id}.
-#' Please note that whenever possible, these should still be unique. Perhaps Bloomberg, Reuters-X.RIC, CUSIP, etc.
-#' The code will return the first (and only the first) match that it finds, starting with the primary_id, and then searching all instruments in the list alphabetically by primary_id.
-#' This is robust enough if you take some care, though a more robust patch would be welcomed.
+#' \code{identifiers} should be a named list to specify other identifiers beyond
+#' the \code{primary_id}. Please note that whenever possible, these should
+#' still be unique. Perhaps Bloomberg, Reuters-X.RIC, CUSIP, etc.
+#' \code{\link{getInstrument}} will return the first (and only the first) match
+#' that it finds, starting with the primary_id, and then searching the
+#' primary_ids of all instruments for each of the \code{identifiers}. Note that
+#' when a large number of instruments are defined, it is faster to find
+#' instruments by \code{primary_id} than by \code{identifiers} because it looks
+#' for \code{primary_id}s first.
#'
-#' The \code{primary_id} will be coerced within reason to a valid \R variable name by
-#' using \code{\link{make.names}}. We also remove any leading '1' digit (a simple workaround to account for issues with the Reuters API).
-#' Please use some care to choose your primary identifiers so that R won't complain.
-#' If you have better regular expression code, we'd be happy to include it.
+#' The \code{primary_id} will be coerced within reason to a valid \R variable
+#' name by using \code{\link{make.names}}. We also remove any leading '1' digit
+#' (a simple workaround to account for issues with the Reuters API).
+#' Please use some care to choose your primary identifiers so that R won't
+#' complain. If you have better regular expression code, we'd be happy to
+#' include it.
#'
-#' Identifiers will also try to be discovered as regular named arguments passed in via \code{...}.
-#' We currently match any of the following: \code{"CUSIP","SEDOL","ISIN","OSI","Bloomberg","Reuters","X.RIC","CQG","TT","Yahoo","Google"}
+#' Identifiers will also try to be discovered as regular named arguments passed
+#' in via \code{...}. We currently match any of the following:
+#' \code{"CUSIP","SEDOL","ISIN","OSI","Bloomberg","Reuters","X.RIC","CQG","TT","Yahoo","Google"}
#' Others may be specified using a named list of identifiers, as described above.
#'
#' \code{assign_i} will use \code{\link{assign}} to place the constructed
#' instrument class object into the \code{.instrument} environment. Most of the
#' special type-specific constructors will use \code{assign_i=TRUE} internally.
-#' Calling with \code{assign_i=FALSE}, or not specifying it, will return an object and
-#' will \emph{not} store it. Use this option ether to wrap calls to \code{instrument}
-#' prior to further processing (and presumably assignment) or to test your parameters
-#' before assignment.
+#' Calling with \code{assign_i=FALSE}, or not specifying it, will return an
+#' object and will \emph{not} store it. Use this option ether to wrap calls to
+#' \code{instrument} prior to further processing (and presumably assignment) or
+#' to test your parameters before assignment.
#'
-#' \code{future} and \code{option} are used to define the contract specs of a series of instruments. The
-#' \code{primary_id} for these can begin with 1 or 2 dots if you need to avoid overwriting another instrument.
-#' 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)
+#' As of version 0.10.0, the .instrument environment is located at the top level
+#' of the package. i.e. \code{FinancialInstrument:::.instrument}.
+#'
+#' \code{future} and \code{option} are used to define the contract specs of a
+#' series of instruments. The \code{primary_id} for these can begin with 1 or
+#' 2 dots if you need to avoid overwriting another instrument.
+#' 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 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
-#' @param currency string describing the currency ID of an object of type \code{\link{currency}}
-#' @param multiplier numeric multiplier to apply to the price in the instrument currency to get to notional value
-#' @param tick_size the tick increment of the instrument price in it's trading venue, as numeric quantity (e.g. 1/8 is .125)
-#' @param identifiers named list of any other identifiers that should also be stored for this instrument
-#' @param type instrument type to be appended to the class definition, typically not set by user
-#' @param assign_i TRUE/FALSE. Should the instrument be assigned to the \code{.instrument} environment? Default is FALSE for
-#' \code{instrument}, TRUE for wrappers.
+#' You can (optionally) provide a \code{src} argument in which case, it will be
+#' used in a call to \code{\link[quantmod]{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 \code{NULL} for cash settled instruments
+#' @param currency String describing the currency ID of an object of type
+#' \code{\link{currency}}
+#' @param multiplier Numeric multiplier to apply to the price in the instrument
+#' to get to notional value.
+#' @param tick_size The tick increment of the instrument price in it's
+#' trading venue, as numeric quantity (e.g. 1/8 is .125)
+#' @param identifiers Named list of any other identifiers that should also be
+#' stored for this instrument
+#' @param type instrument type to be appended to the class definition, typically
+#' not set by user
+#' @param assign_i TRUE/FALSE. Should the instrument be assigned to the
+#' \code{.instrument} environment? Default is FALSE for \code{instrument},
+#' TRUE for wrappers.
#' @aliases
#' stock
#' bond
@@ -121,16 +144,22 @@
#' \code{\link{spread}}
#' \code{\link{load.instruments}}
#' @export
-instrument<-function(primary_id , ..., currency , multiplier , tick_size=NULL, identifiers = NULL, type=NULL, assign_i=FALSE ){
- if(is.null(primary_id)) stop("you must specify a primary_id for the instrument")
+instrument<-function(primary_id , ..., currency , multiplier , tick_size=NULL,
+ identifiers = NULL, type=NULL, assign_i=FALSE ){
+ if(is.null(primary_id)) {
+ stop("you must specify a primary_id for the instrument")
+ }
raw_id <- primary_id
#deal with leading digits or illegal characters
- if(substr(primary_id,1,1)==1) primary_id <- substr(primary_id,2,nchar(primary_id))
+ if(substr(primary_id,1,1)==1) {
+ primary_id <- substr(primary_id,2,nchar(primary_id))
+ }
primary_id<-make.names(primary_id)
- if(missing(currency) || is.null(currency) || (!missing(currency) && !is.currency.name(currency)))
- stop("currency ",currency," must be defined first")
-
+ if(missing(currency) || is.null(currency) ||
+ (!missing(currency) && !is.currency.name(currency))) {
+ stop("currency ",currency," must be defined first")
+ }
if(!hasArg(identifiers) || is.null(identifiers)) identifiers = list()
if(!is.list(identifiers)) {
warning("identifiers",identifiers,"do not appear to be a named list")
@@ -164,50 +193,64 @@
## TODO note that multiplier could be a time series, probably add code here to check
- if(!is.numeric(multiplier) | length(multiplier) > 1) stop("multiplier must be a single number")
- if(!is.null(tick_size) && (!is.numeric(tick_size) | length(tick_size) > 1)) stop("tick_size must be NULL or a single number")
-
- if(is.null(type)) tclass="instrument" else tclass = unique(c(type,"instrument"))
+ if(!is.numeric(multiplier) || length(multiplier) > 1) {
+ stop("multiplier must be a single number")
+ }
+ if(!is.null(tick_size) && (!is.numeric(tick_size) | length(tick_size) > 1)) {
+ stop("tick_size must be NULL or a single number")
+ }
+ if(is.null(type)) {
+ tclass="instrument"
+ } else tclass = unique(c(type,"instrument"))
tmpinstr <- list(primary_id = primary_id,
currency = currency,
multiplier = multiplier,
tick_size=tick_size,
identifiers = identifiers,
- type = type
- )
+ type = type)
if(length(arg)>=1) {
tmpinstr <- c(tmpinstr,arg)
}
class(tmpinstr)<-tclass
if(assign_i) {
- assign(primary_id, tmpinstr, envir=as.environment(FinancialInstrument:::.instrument) )
+ assign(primary_id, tmpinstr,
+ envir=as.environment(FinancialInstrument:::.instrument) )
return(primary_id)
} else return(tmpinstr)
}
#' @export
#' @rdname instrument
-stock <- function(primary_id , currency=NULL , multiplier=1 , tick_size=.01, identifiers = NULL, assign_i=TRUE, ...){
+stock <- function(primary_id , currency=NULL , multiplier=1 , tick_size=.01,
+ identifiers = NULL, assign_i=TRUE, ...){
if (is.null(currency)) stop ("'currency' is a required argument")
if (length(primary_id) > 1) return(unname(sapply(primary_id, stock,
- currency=currency, multiplier=multiplier, tick_size=tick_size, identifiers=identifiers, ...=...)))
- instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ..., type="stock", assign_i=assign_i)
+ currency=currency, multiplier=multiplier, tick_size=tick_size,
+ identifiers=identifiers, ...=...)))
+ instrument(primary_id=primary_id, currency=currency, multiplier=multiplier,
+ tick_size=tick_size, identifiers = identifiers, ...,
+ type="stock", assign_i=assign_i)
}
#' @export
#' @rdname instrument
-fund <- function(primary_id , currency=NULL , multiplier=1 , tick_size=.01, identifiers = NULL, assign_i=TRUE, ...){
+fund <- function(primary_id , currency=NULL , multiplier=1 , tick_size=.01,
+ identifiers = NULL, assign_i=TRUE, ...){
if (is.null(currency)) stop ("'currency' is a required argument")
if (length(primary_id) > 1) return(unname(sapply(primary_id, fund,
- currency=currency, multiplier=multiplier, tick_size=tick_size, identifiers=identifiers, ...=...)))
- instrument(primary_id = primary_id, currency = currency, multiplier = multiplier, tick_size = tick_size, identifiers = identifiers, ..., type="fund", assign_i=assign_i)
+ currency=currency, multiplier=multiplier, tick_size=tick_size,
+ identifiers=identifiers, ...=...)))
+ instrument(primary_id=primary_id, currency=currency,
+ multiplier=multiplier, tick_size=tick_size,
+ identifiers=identifiers, ..., type="fund", assign_i=assign_i)
}
#' @export
#' @rdname instrument
-future <- function(primary_id , currency , multiplier , tick_size=NULL, identifiers = NULL, assign_i=TRUE, ..., underlying_id=NULL){
+future <- function(primary_id , currency , multiplier , tick_size=NULL,
+ identifiers = NULL, assign_i=TRUE, ..., underlying_id=NULL){
if(missing(primary_id)) primary_id <- paste("..",underlying_id,sep="")
if (length(primary_id) > 1) stop('primary_id must be of length 1')
if (missing(currency) && !is.null(underlying_id)) {
@@ -219,7 +262,10 @@
if(is.null(underlying_id)) {
warning("underlying_id should only be NULL for cash-settled futures")
} else {
- if(!exists(underlying_id, where=FinancialInstrument:::.instrument,inherits=TRUE)) warning("underlying_id not found") # assumes that we know where to look
+ if(!exists(underlying_id, where=FinancialInstrument:::.instrument,
+ inherits=TRUE)) {
+ warning("underlying_id not found") # assumes that we know where to look
+ }
if (primary_id == underlying_id) {
primary_id <- paste("..",primary_id,sep="")
warning(paste('primary_id is the same as underlying_id,',
@@ -227,33 +273,50 @@
}
}
- instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ... , type="future", underlying_id=underlying_id, assign_i=assign_i )
+ instrument(primary_id=primary_id, currency=currency, multiplier=multiplier,
+ tick_size=tick_size, identifiers = identifiers, ... ,
+ type="future", underlying_id=underlying_id, assign_i=assign_i)
}
-#' constructors for series contracts on instruments such as options and futures
+#' Constructors for series contracts
#'
-#' constructors for series contracts on instruments such as options and futures
+#' Constructors for series contracts on instruments such as options and futures
#'
+#' The root \code{instrument} (e.g. the \code{future} or \code{option}) must be
+#' defined first.
+#'
#' In custom parameters for these series contracts, we have often found it
#' useful to store attributes such as local roll-on and roll-off dates
#' (rolling not on the \code{first_listed} or \code{expires}.
#'
-#' For \code{future_series} and \code{option_series} you may either provide a \code{primary_id} (or vector of \code{primary_id}s),
+#' For \code{future_series} and \code{option_series} you may either provide a
+#' \code{primary_id} (or vector of \code{primary_id}s),
#' OR both a \code{root_id} and \code{suffix_id}.
-#' @param primary_id string describing the unique ID for the instrument. May be a vector for \code{future_series} and \code{option_series}
-#' @param root_id string product code or underlying_id, usually something like 'ES' or 'CL' for futures,
-#' or the underlying stock symbol (maybe preceded with a dot) for equity options.
-#' @param suffix_id string suffix that should be associated with the series, usually something like 'Z9' or 'Mar10' denoting expiration and year
-#' @param first_traded string coercible to Date for first trading day
-#' @param expires string coercible to Date for expiration date
-#' @param maturity string coercible to Date for maturity date of bond series
-#' @param callput right of option; call or put
-#' @param strike strike price of option
-#' @param payment_schedule not currently being implemented
-#' @param identifiers named list of any other identifiers that should also be stored for this instrument
-#' @param assign_i TRUE/FALSE. Should the instrument be assigned in the \code{.instrument} environment?
-#' @param overwrite TRUE/FALSE. If FALSE, only \code{first_traded} and \code{expires} will be updated.
+#'
+#' Note that the code for \code{bond} and \code{bond_series} has not been
+#' updated recently and may not support all the features supported for
+#' \code{option_series} and \code{future_series}. Patches welcome.
+#'
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/blotter -r 1087
More information about the Blotter-commits
mailing list