[Blotter-commits] r201 - pkg/FinancialInstrument/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jan 25 22:45:41 CET 2010
Author: braverock
Date: 2010-01-25 22:45:41 +0100 (Mon, 25 Jan 2010)
New Revision: 201
Modified:
pkg/FinancialInstrument/R/buildSpread.R
pkg/FinancialInstrument/R/synthetic.R
Log:
- working version of synthetic ratio-based spreads
Modified: pkg/FinancialInstrument/R/buildSpread.R
===================================================================
--- pkg/FinancialInstrument/R/buildSpread.R 2010-01-25 16:12:32 UTC (rev 200)
+++ pkg/FinancialInstrument/R/buildSpread.R 2010-01-25 21:45:41 UTC (rev 201)
@@ -1,59 +1,90 @@
buildSpread<- function(spread_id, ..., Dates = NULL, onelot=FALSE) {
- #TODO this currently assumes a two-instrument spread, need to
- # make it work for arbitrary length ratio spreads
- # this will be done by using the ratio multiplier and currency to
- # create a factor for each element, and then calculating the price from that
+ #TODO subset using Dates arg? or let the +/- operators deal with it?
+ #TODO FIXME put some intelligence in the subsetting and intersection, maybe up front or in a checkData style
spread_instr<-try(getInstrument(spread_id))
if(inherits(spread_instr,"try-error") | !is.instrument(spread_instr)){
stop(paste("Instrument",spread_instr," not found, please create it first."))
}
- if(!inherits(spread_instr,"spread")) stop (paste("Instrument", spread_id, " is not a spread, please use the spread_id of a spread."))
- if(length(spread_instr$memberlist$members)>2) stop("Only two instrument spreads are supported at this time, patches welcome.")
+ if(!inherits(spread_instr,"spread")) stop (paste("Instrument", spread_id, " is not a spread, please use the symbol of a spread instrument."))
+
spread_currency<-spread_instr$currency
- stopifnot(is.currency(spread_currency))
-
- primary_instr<-getInstrument(spread_instr$memberlist$members[1])
- if(inherits(primary_instr,"try-error") | !is.instrument(primary_instr)){
- stop(paste("Instrument",primary_instr," not found, please create it first."))
- } else {
- primary_currency<-primary_instr$currency
- stopifnot(is.currency(primary_currency))
- primary_mult<-primary_instr$multiplier
- primary_ratio<-spread_instr$memberlist$memberratios[1]
- primary_prices<-get(spread_instr$memberlist$members[1])
- }
- secondary_instr<-getInstrument(spread_instr$memberlist[1])
- if(inherits(secondary_instr,"try-error") | !is.instrument(secondary_instr)){
- stop(paste("Instrument", secondary_instr, " not found, please create it first."))
- } else {
- if(!all.equal(primary_currency,secondary_currency)){
- secondary_currency<-secondary_instr$currency
- stopifnot(is.currency(secondary_currency))
- exchange_rate<-try(get( paste(primary_currency,secondary_currency,sep='')))
- if(inherits(exchange_rate,"try-error")){
- exchange_rate<-try(get( paste(secondary_currency,primary_currency,sep='')))
+ stopifnot(is.currency(spread_currency)) #TODO add assumption of Currency multiplier of 1?
+
+ # now build each spread factor and add them up
+ spreadlevel<-NULL
+ for(i in 1:length(spread_instr$memberlist$members)) {
+ instr<-try(getInstrument(as.character(spread_instr$memberlist$members[i])))
+ if(inherits(instr,"try-error") | !is.instrument(instr)){
+ stop(paste("Instrument",instr," not found, please create it first."))
+ } else {
+ instr_currency<-instr$currency
+ if(i==1) primary_currency=instr_currency
+ stopifnot(is.currency(instr_currency))
+ if(!all.equal(primary_currency,instr_currency)){
+ instr_currency<-instr_instr$currency
+ stopifnot(is.currency(instr_currency))
+ exchange_rate<-try(get( paste(primary_currency,instr_currency,sep='')))
if(inherits(exchange_rate,"try-error")){
- stop(paste("Exchange Rate", paste(primary_currency, secondary_currency, sep=''), "not found."))
- } else {
- exchange_rate <- 1/exchange_rate
- }
+ exchange_rate<-try(get( paste(instr_currency,primary_currency,sep='')))
+ if(inherits(exchange_rate,"try-error")){
+ stop(paste("Exchange Rate", paste(primary_currency, instr_currency, sep=''), "not found."))
+ } else {
+ exchange_rate <- 1/exchange_rate
+ }
+ }
+ } else {
+ #currencies of both instruments are the same
+ exchange_rate=1
}
- } else {
- #currencies of both instruments are the same
- exchange_rate=1
- }
- secondary_mult<-secondary_instr$multiplier
- secondary_ratio<-spread_instr$memberlist$memberratios[2]
- secondary_prices<-get(spread_instr$memberlist$members[2])
+ instr_mult<-instr$multiplier
+ instr_ratio<-spread_instr$memberlist$memberratio[i]
+ instr_prices<-Cl(get(as.character(spread_instr$memberlist$members[i])))
+ }
+ instr_norm<-instr_prices*instr_mult*instr_ratio*exchange_rate
+ if(is.null(spreadlevel)) spreadlevel<-instr_norm else spreadlevel=spreadlevel+instr_norm
}
-
- spreadlevel<- (primary_prices*primary_mult*primary_ratio)-(secondary_prices*secondary_mult*secondary_ratio*exchange_rate)
- if(onelot) spreadlevel = spreadlevel/primary_ratio
+# primary_instr<-getInstrument(spread_instr$memberlist$members[1])
+# if(inherits(primary_instr,"try-error") | !is.instrument(primary_instr)){
+# stop(paste("Instrument",primary_instr," not found, please create it first."))
+# } else {
+# primary_currency<-primary_instr$currency
+# stopifnot(is.currency(primary_currency))
+# primary_mult<-primary_instr$multiplier
+# primary_ratio<-spread_instr$memberlist$memberratios[1]
+# primary_prices<-Cl(get(spread_instr$memberlist$members[1]))
+# }
+# secondary_instr<-getInstrument(spread_instr$memberlist[1])
+# if(inherits(secondary_instr,"try-error") | !is.instrument(secondary_instr)){
+# stop(paste("Instrument", secondary_instr, " not found, please create it first."))
+# } else {
+# if(!all.equal(primary_currency,secondary_currency)){
+# secondary_currency<-secondary_instr$currency
+# stopifnot(is.currency(secondary_currency))
+# exchange_rate<-try(get( paste(primary_currency,secondary_currency,sep='')))
+# if(inherits(exchange_rate,"try-error")){
+# exchange_rate<-try(get( paste(secondary_currency,primary_currency,sep='')))
+# if(inherits(exchange_rate,"try-error")){
+# stop(paste("Exchange Rate", paste(primary_currency, secondary_currency, sep=''), "not found."))
+# } else {
+# exchange_rate <- 1/exchange_rate
+# }
+# }
+# } else {
+# #currencies of both instruments are the same
+# exchange_rate=1
+# }
+# secondary_mult<-secondary_instr$multiplier
+# secondary_ratio<-spread_instr$memberlist$memberratios[2]
+# secondary_prices<-get(spread_instr$memberlist$members[2])
+# }
+#
+# spreadlevel<- (primary_prices*primary_mult*primary_ratio)-(secondary_prices*secondary_mult*secondary_ratio*exchange_rate)
+ if(onelot) spreadlevel = spreadlevel/spread_instr$memberlist$memberratios[1]
if(!all.equal(spread_currency,primary_currency)){
#convert to the currency of the spread
spr_exch_rate <- try(get(paste(spread_currency,primary_currency,sep='')))
if(inherits(spr_exch_rate,"try-error")){
- stop(paste("Exchange Rate", paste(spread_currency, primary_currency,sep=''),"not found."))
+ stop(paste("Required Exchange Rate", paste(spread_currency, primary_currency,sep=''),"not found."))
} else {
spreadlevel<-spreadlevel*exchange_rate
}
Modified: pkg/FinancialInstrument/R/synthetic.R
===================================================================
--- pkg/FinancialInstrument/R/synthetic.R 2010-01-25 16:12:32 UTC (rev 200)
+++ pkg/FinancialInstrument/R/synthetic.R 2010-01-25 21:45:41 UTC (rev 201)
@@ -54,13 +54,13 @@
warning("passing in members as a list not fully tested")
memberlist=members
}
- synthetic_temp = synthetic(primary_id , currency , multiplier=multiplier , identifiers = identifiers, members=members , ... )
+ synthetic_temp = synthetic(primary_id , currency , multiplier=multiplier , identifiers = identifiers, members=members , memberratio=memberratio, ... )
## now structure and return
assign(primary_id, structure( list(primary_id = synthetic_temp$primary_id,
currency = synthetic_temp$currency,
multiplier = synthetic_temp$multiplier,
identifiers = synthetic_temp$identifiers,
- memberlist = synthetic_temp$memberlist
+ memberlist = memberlist
),
class=cl
), # end structure
More information about the Blotter-commits
mailing list