Help with Visual Basic
Say you have 3 basic calculations
Function Calc1(a, b, c)
Calc1 = a + b + c
End function
function Calc2(b,f,g)
calc2 = b + f + g
End Function
Function Calc3(a,f,i)
calc2 = a + f + i
End Function
Now you can use these in any function
Function Master1(a,b,c,d,e,f,g,i)
Master1 = (d + calc1(a,b,c) + calc2(b,f,g))/e + i
End Function
Function Master2(a,b,f,g,i,h)
Master2 = Calc3(a,f,i)/calc2(b,f,g)*h
EndFunction
So as much as you can identify repetative subcalculations, you can use a
construct like the above.
--
Regards,
Tom Ogilvy
"RK" wrote in message
...
I'm new to Visual Basic. By trial and error, I keyed in the following code
to determine the price of a Call Option, given stock price, exercise
price,
current yield, dividend yield, std. deviation & days to maturity.
Although the function works, I want to create an additional function for
Put
Option Price. Many of the same intermediate calculations are used again
(eg.
d1, d2, Nd1 and Nd2). How do I code so that I don't have to repeat all
this?
Thanks for your help!
Function CallPrice(PS, PE, YTM, DIV, SD, DAYS)
d1 = (Log(PS / PE) + (YTM - DIV + 0.5 * SD ^ 2) * DAYS / 365) / SD /
Sqr(DAYS / 365): d2 = d1 - SD * Sqr(DAYS / 365): Nd1 =
WorksheetFunction.NormSDist(d1): Nd2 = WorksheetFunction.NormSDist(d2):
CallPrice = PS * Nd1 * Exp(-DIV * DAYS / 365) - PE * Nd2 * Exp(-YTM * DAYS
/
365)
End Function
|