[Blotter-commits] r199 - pkg/blotter/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jan 25 17:09:12 CET 2010


Author: braverock
Date: 2010-01-25 17:09:12 +0100 (Mon, 25 Jan 2010)
New Revision: 199

Modified:
   pkg/blotter/R/synthetic.R
Log:
- commit in advance of move

Modified: pkg/blotter/R/synthetic.R
===================================================================
--- pkg/blotter/R/synthetic.R	2010-01-25 13:33:47 UTC (rev 198)
+++ pkg/blotter/R/synthetic.R	2010-01-25 16:09:12 UTC (rev 199)
@@ -1,47 +1,59 @@
-buildSpread<- function(primary_id, ..., Dates = NULL, onelot=FALSE) {
-    tmp_instr<-try(getInstrument(primary_id))
-    if(inherits(tmp_instr,"try-error") | !is.instrument(tmp_instr)){
-        stop(paste("Instrument",tmp_instr," not found, please create it first."))
+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
+    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(tmp_instr,"spread")) stop (paste("Instrument",primary_id," is not a spread, please use the primary_id of a spread."))
-    tmp_currency<-tmp_instr$currency
-    stopifnot(is.currency(tmp_currency))
+    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.")
+    spread_currency<-spread_instr$currency
+    stopifnot(is.currency(spread_currency))
 
-    primary_instr<-getInstrument(tmp_instr$memberlist$members[1])
+    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<-tmp_instr$memberlist$memberratios[1]
+        primary_ratio<-spread_instr$memberlist$memberratios[1]
+        primary_prices<-get(spread_instr$memberlist$members[1])
     }
-    secondary_instr<-getInstrument(tmp_instr$memberlist[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."))
+        stop(paste("Instrument", secondary_instr, " not found, please create it first."))
     } else {
-        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(!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")){
-                stop(paste("Exchange Rate",paste(primary_currency,secondary_currency,sep=''),"not found."))    
-            } else {
-                exchange_rate <- 1/exchange_rate
-            }   
+                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<-tmp_instr$memberlist$memberratios[2]
+        secondary_ratio<-spread_instr$memberlist$memberratios[2]
+        secondary_prices<-get(spread_instr$memberlist$members[2])
     }
     
-    spreadlevel<- (primary*primary_mult*primary_ratio)-(secondary*secondary_mult*secondary_ratio*exchange_rate)
+    spreadlevel<- (primary_prices*primary_mult*primary_ratio)-(secondary_prices*secondary_mult*secondary_ratio*exchange_rate)
     if(onelot) spreadlevel = spreadlevel/primary_ratio
-    if(!all.equal(tmp_currency,primary_currency)){
+    if(!all.equal(spread_currency,primary_currency)){
         #convert to the currency of the spread
-        spr_exch_rate <- try(get(paste(tmp_currency,primary_currency,sep='')))
+        spr_exch_rate <- try(get(paste(spread_currency,primary_currency,sep='')))
         if(inherits(spr_exch_rate,"try-error")){
-            stop(paste("Exchange Rate",paste(tmp_currency, primary_currency,sep=''),"not found."))    
+            stop(paste("Exchange Rate", paste(spread_currency, primary_currency,sep=''),"not found."))    
         } else {
             spreadlevel<-spreadlevel*exchange_rate
         }



More information about the Blotter-commits mailing list