Hello, I am writing a scenario analysis for option strategies. I am trying to 
build a framework, such that new pricing functions can be added as well as 
volatility surface, interest rate term structure, etc… But for now I am 
sticking to ear vanilla case. Anyways, the problem is not actually related to 
finance, but I could not find any answer so far.   

fun1 <- function(a,b){fun2(a)}
fun2 <- function(c){fun3(c)}
fun3 <- function(d){d+b}


fun1(2,2) #Error in fun3(c) : object 'b' not found

I have always thought that if functions can not find a variable it gradually 
goes back up through each single environment looking for that variable. 
However, now it is apparent, that it only looks for it in .GlobalEnv.

In my code fun1 is the main function.

fun2 is called via lapply wrapper from fun1 and simply has if else structure, 
which checks the security type and directs to appropriate "fun3".

fun3 is called via sapply and it returns a vector for each security, which 
indicates profit and loss function for a specified time. Though sapply wrapper 
returns matrix: xaxis=possible stock value, yaxis=date, zaxis=pnl.

As you can imagine there are plenty of variables to pass from fun1 to fun3 e.g. 
fun1 lapply wrapper:
pnl <- lapply(cur.instr, pnl.aggregate, tVec=tVec, cur.range=cur.range, 
cal=cal, par=cur.par)

cur.instr - list of objects(lists) of securities. Including all constant 
parameters of security: i.e. strike or time to maturity.
pnl.aggregate - fun2
tVec - vector of time fractions. if in pricing (rho=T-t) tVec is "t"
cur.range  - vector of possible stock values
cal  - "string". appropriate calendar indicating trading days. RQuantLib stuff
par  - object(list) of stock parameters, including s0, vol(integer or matrix), 
div(continuous), etc

anyways, you got the idea of my issue. I do not want to pass all of these via 
arguments through 2 functions. The main reason is that it becomes messy and 
complicates the code significantly. I was thinking about eval, that might 
evaluate fun3 in environment of fun1. However could not find a way how to do 
it. When I was working directly in fun1 environment. i.e. global frame, and 
evaluating "future" fun1 in it, all worked perfectly. However, when I finally 
ran fun1… it made me sad...

Do you have any ideas how to overcome this issue?
I could share the code, but there is quite a lot of it. I hope you understand 
my problem and I will appreciate any help. Thank you in advance.

Kind regards,--  
Dominykas Grigonis


        [[alternative HTML version deleted]]

_______________________________________________
R-SIG-Finance@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should 
go.

Reply via email to