Hi all, This is my first attempt at RExcel VBA, and I'd like to hear your advice to improve my code.
I'm trying to write two VBA functions to value plain vanilla options (calls, puts) with a) the Cox-Ross-Rubinstein binomial model and b) the Generalized Black-Scholes model. The fOptions package has such R functions: CRRBinomialTreeOption() and GBSOption(), respectively. So I came up with the following VBA code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Function CRR(TypeFlag As String, S As String, Strike As String, T As String, r As String, CostOfCarry As String, sigma As String, n As String) As Double Dim Instruction As String Instruction = "x <- CRRBinomialTreeOption(TypeFlag = """ & TypeFlag _ & """, S=" & rnumber(S) & ", X = " & rnumber(Strike) & ", Time = " & rnumber(T) _ & ", r=" & rnumber(r) & ", b= " & rnumber(CostOfCarry) & ", sigma = " & rnumber(sigma) _ & ", n = " & rnumber(n) & ")" RInterface.StartRServer RInterface.RRun "library(fOptions)" RInterface.RRun Instruction RInterface.RRun "y<-getSlot(x, ""price"")" CRR = RInterface.GetRExpressionValueToVBA("y") End Function Function GBS(TypeFlag As String, S As String, Strike As String, T As String, r As String, CostOfCarry As String, sigma As String) As Double Dim Instruction As String Instruction = "x <- GBSOption(TypeFlag = """ & TypeFlag _ & """, S=" & rnumber(S) & ", X = " & rnumber(Strike) & ", Time = " & rnumber(T) _ & ", r=" & rnumber(r) & ", b= " & rnumber(CostOfCarry) & ", sigma = " & rnumber(sigma) & ")" RInterface.StartRServer RInterface.RRun "library(fOptions)" RInterface.RRun Instruction RInterface.RRun "y<-getSlot(x, ""price"")" GBS = RInterface.GetRExpressionValueToVBA("y") End Function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ They are working fine, but I probably could have coded them better. For example, I don't know if it's necessary or redundant to include"library(fOptions)" in each function call; or if it would be better not to use the same vectors' names (x and y) in both functions, just in case they somehow might "survive" a function call thus interfering with the other. Any ideas? Thank you very much for your time. Kind regards from Buenos Aires, Diego Mazzeo
_______________________________________________ Rcom-l mailing list Rcom-l@mailman.csd.univie.ac.at http://mailman.csd.univie.ac.at/mailman/listinfo/rcom-l More information (including a Wiki) at http://rcom.univie.ac.at