APPENDIX 3B RECORDING KEYSTROKES IN ‘RELATIVE REFERENCES’ MODE
4.3 TWO FUNCTIONS WITH MULTIPLE INPUTS FOR VALUING OPTIONS
As yet, Excel does not have a built-in function to calculate the value of an option using the Black–Scholes formula. This allows us to develop a user-defined function suitable for valuing a call, named BSCallValue say. The underlying theory that is the background to the Black–Scholes formula is introduced in Part III of the book. Although at this stage you will not necessarily understand the option value formula, remember that our purpose here is merely to turn the formula into workable VBA code.
The Black–Scholes pricing formula for a European call allowing for dividends is:
cDSexpqTNd1XexprTNd2
In the formula,S is the current share price,X the exercise price for the call at timeT,r the continuously compounded risk-free interest rate, hence the expression exprTfor
the risk-free discount factor over periodT. The continuous dividend yield on the share is denotedq, so that the share priceSis replaced bySexpqTin the valuation formulation.
The notationNdis used to denote the cumulative standard normal probability of a value less than d. Hered1 andd2 are given by:
d1 D[lnS/XCrqC2/2T]/[
p T]
d2 D[lnS/XCrq2/2T]/[p
T]Dd1 p
T where is the volatility of the share.
The spreadsheet extract in Figure 4.2 shows details of a six-month option on a share with current value SD100, XD95, rD8.0%, qD3% and D20%. Expressions d1
andd2are evaluated in cells G8 and G11, from which the cumulative normal probabilities Nd1andNd2in cells G9 and G12 are derived, using Excel’s NORMSDIST function.
These are inputs to the call value cell formula (in G4):
DD4ŁD16ŁG9D5ŁD15ŁG12
which evaluates as 9.73. (The same inputs produce the related put value for the option shown in cell H4.)
The call formula can be programmed into a user-defined function with six inputs S, X, r, q, T, , whose answer is shown in cell G5.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A B C D E F G H
Black-Scholes Formula
Call Put
Share price (S) 100.00 BS value 9.73 2.49
Exercise price (X) 95.00 BSval via fn 9.73 2.49
Int rate-cont (r) 8.00%
Dividend yield (q) 3.00% d1 0.6102
N (d1) 0.7291
Time now (0, years) 0.00
Time maturity (T, years) 0.50 d2 0.4688
Option life (T, years) 0.50 N (d2) 0.6804
Volatility (s) 20.00%
Exp (-rT) 0.9608
Exp (-qT) 0.9851
Figure 4.2 Details of an option and Black – Scholes values for the call and put in BS sheet
The VBA code for theFunction BSCallValue(S, X, r, q, tyr, sigma)is set out below.
The time to maturity for the option (in years) is denoted tyr and is denoted sigma. The calculations in the Black–Scholes formula are sufficiently complex for us to use intermediate variables [denoted DOne and NDOne for d1 and Nd1] in coding the function:
Option Explicit ’to force variable declaration Function BSCallValue(S, X, r, q, tyr, sigma)
’ returns Black–Scholes call value (allowing for q=div yld)
Writing VBA User-defined Functions 77 Dim ert, eqt
Dim DOne, DTwo, NDOne, NDTwo
ert = exp(-rŁtyr) ‘exp is the VBA function for ‘e’
eqt = exp(-qŁtyr) ‘dividend yield effect
DOne = (log(S/X)+(r-q + 0.5ŁsigmaO2)Łtyr)/(sigmaŁsqr(tyr)) DTwo = (log(S/X)+(r-q – 0.5ŁsigmaO2)Łtyr)/(sigmaŁsqr(tyr)) NDOne = Application.NormSDist(DOne)
NDTwo = Application.NormSDist(Dtwo) BSCallValue = (SŁeqtŁNDOne - XŁertŁNDTwo) End Function
The statement Option Explicitat the top of the module sheet forces the declaration of all variables used in calculating the function. To comply, the ‘Dim’ statement declares the six variables (ert, eqt, DOne, DTwo, NDOne, NDTwo). Descriptive names have been chosen for the variables, e.g. DOnefor d1 and NDOneforNd1, etc. to link the code to the conventional notation for the options formula. The variable ert is the discount factor for converting payoffs at maturity to present values: in algebraic terms, exprT.
The variable eqt is the dividend yield effect on the current share price, algebraically expqT. Note that the input variables (such asS,X, etc.) are automatically declared in the Function command itself, so should not be included in Dim statements.
Despite the unwieldy appearance of the code, the calculation is relatively simple. Notice that the three expressions ‘exp’, ‘log’ and ‘sqr’ are built-in VBA functions for ‘e’, natural logs (ln) and square-root respectively. Where both VBA and Excel functions exist for the same expression, the VBA function must be used rather than the corresponding Excel func- tion. When Excel functions are used in the code, they must be prefaced withApplication. orWorksheetFunction. Hence:
NDOne = Application.NormSDist(DOne)
uses the Excel function NORMSDIST.
Thus, the formula:
DBSCallValue(100, 95, 8%, 3%, 0.5, 20%)
evaluates to 9.73. (The formula in cell G5 displaying the value 9.73 takes its inputs from the cells in column B.)
Turning to the corresponding BS valuation formula for a European put, the put–call parity relationship ensures that the value of a European put is given by:
pD SexpqTNd1CXexprTNd2
withd1andd2as before. The only difference from the previous call formula is the change of sign from positive to negative for the d1 and d2 arguments in the cumulative normal probability expressions and the reversed signs for the two terms in the Black–Scholes formula. By adding a further argument, iopt (with value 1 for a call and 1 for a put) the VBA code for a call can be generalised to cover valuation of either European option, call or put.
The code for function BSOptValue with seven inputs (including iopt set to 1 or1) is given below. If ioptD 1, NDOne becomesNd1, NDTwo becomes Nd2, and the signs of the two terms in the BS equation have altered since ioptD 1. Confirm in the spreadsheet that for a put, BSOptValue(1, 100, 95, 8%, 3%, 0.5, 20%) equals 2.40 [whereas BSOptValue(1, 100, 95, 8%, 3%, 0.5, 20%) returns 9.73 as before]:
Function BSOptValue(iopt, S, X, r, q, tyr, sigma)
’ returns the Black-Scholes value (iopt=1 for call, -1 for put, q = div yld)
’ uses fns BSDOne and BSDTwo Dim ert, eqt, NDOne, NDTwo ert = exp(-rŁtyr)
eqt = exp(-qŁtyr)
NDOne = Application.NormSDist(ioptŁBSDOne(S, X, r, tyr, sigma)) NDTwo = Application.NormSDist(ioptŁBSDTwo(S, X, r, tyr, sigma)) BSOptValue = ioptŁ(SŁeqtŁNDOne - XŁertŁNDTwo)
End Function
Function BSDOne(S, X, r, q, tyr, sigma)
’ returns the Black - Scholes d1 value
BSDOne = (log(S/X) + (r-q +0.5ŁsigmaO2)Łtyr)/(sigmaŁsqr(tyr)) End Function
Function BSDTwo(S, X, r, q, tyr, sigma)
’ returns the Black-Scholes d2 value
BSDTwo = (log(S/X)+(r-q - 0.5ŁsigmaO2)Łtyr)/(sigmaŁsqr(tyr)) End Function
Note that the intermediate quantities, DOne and DTwo, have been coded as functions in their own right and are simply called from the ‘master’ function. The object is to modularise the VBA code making its operation more transparent and allowing errors in coding to be pinned down more accurately.
In summary, functions are VBA procedures which return values, their code being enclosed in between the keywords Function and End Function. Following the Function keyword, the name of the function is declared followed by a list of inputs in brackets.
A series of VBA statements follow and when the function has run, the name returns the output:
Functionname (input1, input2,. . .) [statements]
[name = expression]
End Function
In good VBA programming, it is preferable to head each Module sheet with the Option Explicit statement, which forces formal variable declaration. Note that whereas all vari- ables created and used in computing the function should be declared with Dim statements, variables that are function inputs do not need to be declared. If Excel functions are required in the code, they must be referred to with the Application. or the Worksheet- Function. prefix.