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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 19 23:51:28 CET 2010


Author: braverock
Date: 2010-01-19 23:51:27 +0100 (Tue, 19 Jan 2010)
New Revision: 191

Modified:
   pkg/blotter/R/synthetic.R
Log:
- add functionality to calculate spread ratio for two-instrument spread

Modified: pkg/blotter/R/synthetic.R
===================================================================
--- pkg/blotter/R/synthetic.R	2010-01-19 21:30:55 UTC (rev 190)
+++ pkg/blotter/R/synthetic.R	2010-01-19 22:51:27 UTC (rev 191)
@@ -1,10 +1,41 @@
-buildSpread<- function(primary_id, ...) {
+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",Symbol," not found, please create it first."))
+        stop(paste("Instrument",tmp_instr," not found, please create it first."))
     } 
-    if(!inherits(tmp_instr,"spread")) stop (paste("Instrument",Symbol," is not a spread, please use the primary_id of a spread."))
+    if(!inherits(tmp_instr,"spread")) stop (paste("Instrument",primary_id," is not a spread, please use the primary_id of a spread."))
 
+    primary_instr<-getInstrument(tmp_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]
+    }
+    secondary_instr<-getInstrument(tmp_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 {
+        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
+            }   
+        }
+        secondary_mult<-secondary_instr$multiplier
+        secondary_ratio<-tmp_instr$memberlist$memberratios[2]
+    }
+    
+    spreadlevel<- (primary*primary_mult*primary_ratio)-(secondary*secondary_mult*secondary_ratio*exchange_rate)
+    if(onelot) spreadlevel = spreadlevel/primary_ratio
+    return(spreadlevel)
 }
 
 



More information about the Blotter-commits mailing list