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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jun 5 20:13:01 CEST 2011


Author: gsee
Date: 2011-06-05 20:13:01 +0200 (Sun, 05 Jun 2011)
New Revision: 612

Added:
   pkg/FinancialInstrument/man/option_series.yahoo.Rd
Modified:
   pkg/FinancialInstrument/R/instrument.R
Log:
added option_series.yahoo function to define option chains using yahoo data. added documentation.

Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R	2011-06-05 02:48:36 UTC (rev 611)
+++ pkg/FinancialInstrument/R/instrument.R	2011-06-05 18:13:01 UTC (rev 612)
@@ -252,6 +252,58 @@
     }
 }
 
+option_series.yahoo <- function(symbol, Exp, currency="USD", multiplier=100, first_traded=NULL, tick_size=NULL) {
+    #FIXME: identifiers?
+    opts <- getOptionChain(Symbol=symbol,Exp=Exp, src="yahoo")
+	
+	locals <- function(x) c(rownames(x$calls),rownames(x$puts))
+	if (is.null(opts$calls)) { #if is.null(Exp) we'll get back all chains
+		led <- (lapply(opts, locals))  
+		optnames <- unname(do.call(c, led))	#FIXME: Is this a reasonable way to get rownames?		
+	} else 	optnames <- locals(opts) #c(rownames(opts$calls),rownames(opts$puts))
+	
+    for (r in optnames) {
+        si <- gsub(symbol,"",r) #suffix_id
+        expiry <- paste('20',substr(si,1,6),sep="")
+        right <- substr(si,7,7)
+        strike <- as.numeric(substr(si,8,15))/1000
+#        local <- paste(symbol, si, sep="   ")      
+		clean.si <- paste(expiry,right,strike,sep="")		
+		primary_id <- paste(".", symbol, "_", clean.si, sep="")
+
+		#create currency if it doesn't exist	#?? Any reason not to ??	
+		tmpccy <- try(getInstrument(currency,silent=TRUE),silent=TRUE)
+		if (!inherits(tmpccy, "currency")) {
+			warning(paste("Created currency", currency, "because it did not exist."))			
+			currency(currency) #create it
+		}
+		#create option spec if we need to.
+		tmpInstr <- try(getInstrument(paste('.',symbol,sep=""),silent=TRUE),silent=TRUE)
+		if (!inherits(tmpInstr, "option")) {
+			warning(paste('Created option specs for root',paste('.',symbol,sep="")))
+			option(primary_id=paste('.',symbol,sep=""), currency=currency,
+					multiplier=multiplier, tick_size=tick_size, 
+					underlying_id=symbol)		
+		}
+		#create specific option
+        tempseries = instrument( primary_id=primary_id, 
+				                    suffix_id=clean.si, 
+				                    first_traded=first_traded, 
+				                    currency=currency, 
+				                    multiplier=multiplier, 
+				                    tick_size=tick_size, 
+				                    expires=expiry, 
+				                    callput=switch(right, C="call", P="put"), #to be consistent with the other option_series function	
+				                    strike=strike, 
+				                    underlying_id=symbol, 
+				                    type = c("option_series","option"), 
+				                    defined.by='yahoo', assign_i=TRUE
+                                )    
+#		option_series(primary_id=primary_id, suffix_id=si, exires=expiry, currency=currency,
+#                        callput = switch(right,C='call',P='put'))
+    }
+}
+
 #' @export
 currency <- function(primary_id , currency=NULL , multiplier=1 , identifiers = NULL, ...){
   ## now structure and return

Added: pkg/FinancialInstrument/man/option_series.yahoo.Rd
===================================================================
--- pkg/FinancialInstrument/man/option_series.yahoo.Rd	                        (rev 0)
+++ pkg/FinancialInstrument/man/option_series.yahoo.Rd	2011-06-05 18:13:01 UTC (rev 612)
@@ -0,0 +1,66 @@
+\name{option_series.yahoo}
+\alias{option_series.yahoo}
+%- Also NEED an '\alias' for EACH other topic documented here.
+\title{
+constructor for series of options using yahoo data
+}
+\description{
+Defines a chain or several chains of options by looking up necessary info from yahoo 
+}
+\usage{
+option_series.yahoo(symbol, Exp, currency = "USD", multiplier = 100, first_traded = NULL, tick_size = NULL)
+}
+%- maybe also 'usage' for other objects documented here.
+\arguments{
+  \item{symbol}{
+ticker symbol of the underlying instrument (Currently, should only be stock tickers)
+}
+  \item{Exp}{
+Expiration date or dates to be passed to getOptionChain
+}
+  \item{currency}{
+currency of underlying and options
+}
+  \item{multiplier}{
+contract multiplier. Usually 100 for stock options
+}
+  \item{first_traded}{
+first date that contracts are tradeable. Probably not applicable if defining several chains.
+}
+  \item{tick_size}{
+minimum price change of options. 
+}
+}
+\details{
+If \code{Exp} is missing it will define only the nearby options. 
+If \code{Exp} is NULL it will define all options
+
+If \code{first_traded} and/or \code{tick_size} should not be the same for all options being defined, they should be left NULL and defined outside of this function.
+}
+\value{
+Called for side-effect. The instrument that is created and stored will inherit option_series, option, and instrument classes. 
+}
+\references{
+Yahoo \url{http://finance.yahoo.com}
+}
+\author{
+Garrett See
+}
+\note{
+Has only been tested with stock options.
+Does not support quanto options -- The options' currency should be the same as the underlying's.
+}
+
+%% ~Make other sections like Warning with \section{Warning }{....} ~
+
+\seealso{
+option_series, option, instrument, getOptionChain
+}
+\examples{
+\dontrun{
+option_series.yahoo('SPY') #only nearby calls and puts
+option_series.yahoo('DIA', Exp=NULL) #all chains
+ls(.instrument, all.names=TRUE)
+}
+}
+


Property changes on: pkg/FinancialInstrument/man/option_series.yahoo.Rd
___________________________________________________________________
Added: svn:executable
   + *



More information about the Blotter-commits mailing list