6
PROJECT 1 VBA IN EXCEL WITH FINANCIAL APPLICATIONS This assignment aims to introduce you to the development of user-defined functions and subroutines in VBA and guide you into how you can use a modular approach in the development of complex applications. We are interested in the process of developing computer codes to solve specific scientific problems. The development of such codes involves three distinct stages, the development of an appropriate numerical algorithm, the implementation of the algorithm in a computer language, and finally validation of the numerical results produced. In the first question you develop a simple VBA function that can return the option price, or the delta, or the vega of either a call or a put based on the Black and Scholes model. You will be using this closed form solution to validate the results that you will later obtain using a different numerical method. In question 2 you develop a function that calculates the value of European put and European call options using Monte-Carlo methodology. You will use your code to perform a number of numerical experiments which examine how the number of paths used influence the accuracy achieved and investigate how the antithetic variable technique can be used to improve the accuracy of your estimates. Finally, in question 3 you have to introduce a visual interface for the Black and Scholes function and the Monte-Carlo simulation subroutines that will make their use user-friendly. As you develop your code, make certain that each constituent part does produce the right result and can handle any possible errors that might occur due to user error. Design each component of your code in such a way that enables simple modifications without the need of major structural changes and can easily be used to create more complicated programs. Keep your functions and subroutines simple. Write your code in small modules each with a clearly defined purpose. Be consistent in the use of variables and select names for your variables, functions and procedures that are easy for you and for others to understand. Please bear in mind that you are expected to submit an Excel file containing all the modules that you created alongside a three pages report that addresses the questions set in questions 1 and 2 of the coursework. For your report you should use Time New Roman fonts, font size 12 and line spacing 1.5 lines. Good luck! Submission Date: October 28, 2009 Oral Examination Date: TBA

VBA IN EXCEL WITH FINANCIAL APPLICATIONSpjohnson/VBA-C++/VBA/VBACoursew… · PROJECT 1 VBA IN EXCEL WITH FINANCIAL APPLICATIONS This assignment aims to introduce you to the development

Embed Size (px)

Citation preview

Page 1: VBA IN EXCEL WITH FINANCIAL APPLICATIONSpjohnson/VBA-C++/VBA/VBACoursew… · PROJECT 1 VBA IN EXCEL WITH FINANCIAL APPLICATIONS This assignment aims to introduce you to the development

PROJECT 1

VBA IN EXCEL WITH FINANCIAL APPLICATIONS

This assignment aims to introduce you to the development of user-defined functions and

subroutines in VBA and guide you into how you can use a modular approach in the development

of complex applications. We are interested in the process of developing computer codes to solve

specific scientific problems. The development of such codes involves three distinct stages, the

development of an appropriate numerical algorithm, the implementation of the algorithm in a

computer language, and finally validation of the numerical results produced.

In the first question you develop a simple VBA function that can return the option price, or the

delta, or the vega of either a call or a put based on the Black and Scholes model. You will be

using this closed form solution to validate the results that you will later obtain using a different

numerical method. In question 2 you develop a function that calculates the value of European put

and European call options using Monte-Carlo methodology. You will use your code to perform a

number of numerical experiments which examine how the number of paths used influence the

accuracy achieved and investigate how the antithetic variable technique can be used to improve

the accuracy of your estimates.

Finally, in question 3 you have to introduce a visual interface for the Black and Scholes function

and the Monte-Carlo simulation subroutines that will make their use user-friendly.

As you develop your code, make certain that each constituent part does produce the right result

and can handle any possible errors that might occur due to user error. Design each component of

your code in such a way that enables simple modifications without the need of major structural

changes and can easily be used to create more complicated programs. Keep your functions and

subroutines simple. Write your code in small modules each with a clearly defined purpose. Be

consistent in the use of variables and select names for your variables, functions and procedures

that are easy for you and for others to understand. Please bear in mind that you are expected to

submit an Excel file containing all the modules that you created alongside a three pages report

that addresses the questions set in questions 1 and 2 of the coursework. For your report you

should use Time New Roman fonts, font size 12 and line spacing 1.5 lines. Good luck!

Submission Date: October 28, 2009 Oral Examination Date: TBA

Page 2: VBA IN EXCEL WITH FINANCIAL APPLICATIONSpjohnson/VBA-C++/VBA/VBACoursew… · PROJECT 1 VBA IN EXCEL WITH FINANCIAL APPLICATIONS This assignment aims to introduce you to the development

QUESTION 1 [30 POINTS]

Write a VBA function named BS_Value that uses the Black and Scholes formula to calculate:

(a) The price of a European put on a non-dividend paying stock.

(b) The price of a European call on a non-dividend paying stock.

(c) The delta of a European put on a non-dividend paying stock.

(d) The delta of a European call on a non-dividend paying stock.

(e) The vega of a European call option on a non-dividend paying stock.

(f) The vega of a European put option on a non-dividend paying stock.

The function arguments should be:

0S : the current price of the underlying stock

K : the strike price of the option

σ : the stock price volatility

r : the continuously compounded risk-free rate

T : the time to maturity of the option

TypeOfOutput: Option price, or delta, or vega

TypeOfPayoff: Call or Put option

To calculate the price of a European option on a non-dividend paying stock you should use the

Black and Scholes pricing formulas. More specifically, the price of the European call and

European put option are given by

)()( 210 dNKedNSBS rTcall

−−= ,

)()( 102 dNSdNKeBS rTput −−−= − ,

where

TTrKS

σ )2/()/ln( 20

1++

= ,

Tdd σ−= 12 ,

and N(x) is the cumulative probability distribution for a standardized normal distribution. In

EXCEL VBA we have a built-in function that returns the N(x) value:

N(x) = Application.WorksheetFunction.NormSDist(x)

The delta of a European call option on a non-dividend paying stock is given by, )( 1dNc =Δ .

The delta of a European put option on a non-dividend paying stock is given by, 1)( 1 −=Δ dNp .

Page 3: VBA IN EXCEL WITH FINANCIAL APPLICATIONSpjohnson/VBA-C++/VBA/VBACoursew… · PROJECT 1 VBA IN EXCEL WITH FINANCIAL APPLICATIONS This assignment aims to introduce you to the development

The vega for a European call or put option on a non-dividend paying stock is given by,

)(' 10 dNTSv = , where 2/1

21

21)(' dedN −=π

.

Develop two versions of this function one in which you explicitly define the data type of all the

input variables and one in which you set all the input variables to be of variant type.

Questions:

(a) How have you verified that the developed functions provide the right answer?

(b) What are the possible user-errors that you have identified? How have you dealt with

trapping and recovering from those possible user-errors?

(c) How do your error-handling techniques differ between the two different versions of your

function? What type of different checks do you perform when you declare explicitly the

data type of the function arguments compared with the case in which you declare all the

function arguments to be of variant type?

QUESTION 2 [40 POINTS]

Monte-Carlo simulation is a very powerful and popular methodology for pricing various

derivative products. The key steps in Monte-Carlo simulation are as follows:

1. Sample a random path for the underlying asset S in a risk-neutral world based on the

stochastic differential equation that describes the evolution of S.

2. Calculate the discounted payoff of the derivative based on this path.

3. Repeat steps 1 and 2 a number of times.

4. Calculate the mean of the sample of the discounted payoffs to get an estimate of the value

of the derivative and the standard deviation of the sample of the discounted payoffs to

check the accuracy of the simulation.

A pseudo code is provided below that describes how a typical MonteCarloSimulation

procedure is organized:

Provide σ,,,,0 TrKS ,TypeOfPayoff, NoOfPaths

Define RandomNumbers(NoOfPaths), OptionValue(NoOfPaths)

Call GenerateRandomNumbers which returns

RandomNumbers(NoOfPaths)

Page 4: VBA IN EXCEL WITH FINANCIAL APPLICATIONSpjohnson/VBA-C++/VBA/VBACoursew… · PROJECT 1 VBA IN EXCEL WITH FINANCIAL APPLICATIONS This assignment aims to introduce you to the development

Call GenerateOptionPrices which returns

OptionPrices(NoOfPaths)

Return:

OptionValue=Average[OptionPrices(i)]

StandardErrorOfEstimate=…

StandardDeviation[OptionPrices(i)]/Sqrt[NoOfPaths]

Based on the output of the subroutine it is very easy to calculate confidence intervals for the

price. For example if we denote the OptionValue by OV and the

StandardErrorOfEstimate by SE, a 95% confidence interval is given by:

OV-1.96×SE < OV < OV+1.96×SE.

The VBA provides the built-in function Rnd that returns a random number between 0 and 1. The

simplest way to obtain a random number from a univariate standardized normal distribution is to

use the formula:

6)(12

1

−∑=

=i

iRndε .

A pseudo code is provided below that describes how the subroutine

GenerateRandomNumbers works:

Provide NoOfPaths

Define RandomNumbers(NoOfPaths)

Iterate for i=1 To NoOfPaths

Sum = 0

Iterate for j=1 To 12

Sum = Sum + Rnd

End of Iteration j

RandomNumbers(i)= Sum –6

End of Iteration i

Return:

RandomNumbers(NoOfPaths)

Page 5: VBA IN EXCEL WITH FINANCIAL APPLICATIONSpjohnson/VBA-C++/VBA/VBACoursew… · PROJECT 1 VBA IN EXCEL WITH FINANCIAL APPLICATIONS This assignment aims to introduce you to the development

If we want each time we run the code a different set of random numbers to be generated, we must

introduce the Randomize statement in the MonteCarloSimulation before calling the

GenerateRandomNumbers procedure. To complete this basic Monte Carlo subroutine we

also need a subroutine that would simulate the share prices and produce a large number of option

prices. A pseudo code is provided below that describes how the subroutine

GenerateOptionPrices works:

Provide σ,,,,0 TrKS , ,TypeOfPayoff, NoOfPaths, _

RandomNumbers(NoOfPaths)

Iterate for i=1 To NoOfPaths

⎥⎥⎦

⎢⎢⎣

⎡⎟⎟⎠

⎞⎜⎜⎝

⎛××+−×= TiTrSST )(ersRandomNumb2

2exp0 σσ

),()exp(es(i)OptionPric KSPayoffTr T××−=

End of Iteration

Return:

OptionPrices(NoOfPaths)

For the payoff we should use the Application.WorkSheetFunction.Max() statement.

Extra care should be given on how we find the average and the standard deviation. Using the

built-in Excel functions Average and StD, is not advisable as they would impose an upper

limit to the number of paths equal to 65,536. Therefore, you should write two simple functions

that would return the average and the standard deviation of a matrix. If the simulation is carried

out as described so far, it is usually necessary to use a large number of paths to estimate the

option price with reasonable accuracy. A popular method that improves the estimate provided by

the MonteCarloSimulation is the antithetic variable technique. In employing the antithetic

variable technique, we calculate two values of the derivative. The first value P1 is calculated in

the usual way, the second value, P2, is computed using the same random numbers we used for P1,

but with changing the sign of all random numbers. An improved estimate is found by averaging

P1 and P2.

Questions:

(a) Create a subroutine that uses Monte-Carlo simulation and can price both put and call

European options. Using your developed code perform numerical experiments to analyze

how the number of paths influences the accuracy of the calculated option price.

Page 6: VBA IN EXCEL WITH FINANCIAL APPLICATIONSpjohnson/VBA-C++/VBA/VBACoursew… · PROJECT 1 VBA IN EXCEL WITH FINANCIAL APPLICATIONS This assignment aims to introduce you to the development

(b) Create a new subroutine that uses the antithetic variable technique to improve the

accuracy of your calculations. What is the effect on the calculated price and the

confidence intervals?

QUESTION 3 [30 POINTS]

Create a graphical interface for a calculator that provides the Black and Scholes value using

either the analytical or the Monte-Carlo simulation. The graphical calculator should look like the

one presented below. The calculator should appear when you press Ctrl+Shift+C. You

should create a number of different subroutines:

• A subroutine that resets the data to their default values and should also be used when you

first display the dialog box to initialize the data.

• A subroutine that displays the dialog box.

• Six different subroutines, one for each of the command keys.