VBA for Finance

48
VBA Vincent JEANNIN – ESGF 4IFM Q2 2012 1 [email protected] ESGF 4IFM Q2 2012

description

4th Year of MSc, Options Pricing with Black & Scholes and Cox, Ross & Rubinstein using VBA in Excel.

Transcript of VBA for Finance

Page 1: VBA for Finance

VBA Vincent JEANNIN – ESGF 4IFM

Q2 2012

1

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Page 2: VBA for Finance

2

Summary of the session • Introduction: from BASIC to VBA • Think Algorithmic First: π • First VBA Program: a formula • Alternatives to π estimation • Formula using Excel References • Advanced Formula: CRR

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Page 3: VBA for Finance

Introduction: from BASIC to VBA

3

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Beginner's All-purpose Symbolic Instruction Code

Created in 1964 by students for students

Spread because free

Succeed because easy and base to most of languages

BASIC extended to event driven programming: Visual BASIC

The flow of the program is determined by events: sensor outputs, user actions (mouse clicks, key presses)…

Visual BASIC integrated in Microsoft Office: VBA

Visual BASIC for Applications enables building user defined functions, automating processes,…

If you’re old enough to know what is a 80-386 or to have played with an Apple II you may have heard of: Applesoft BASIC, GW BASIC, QBASIC

Page 4: VBA for Finance

Think Algorithmic First: π

4

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Let’s approximate pi

Think first about “How to” regardless of any programming

Draw a chart of the program then the “translation” is easier

Page 5: VBA for Finance

5

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Let’s pick 2 random numbers

Using a uniform distribution

How do you determine if (x,y) is inside or outside the circle?

Page 6: VBA for Finance

6

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Right Triangle properties

𝐻𝑦𝑝𝑜𝑡𝑒𝑛𝑢𝑠𝑒 2 = 𝐴𝑗𝑎𝑐𝑒𝑛𝑡 2 + 𝑂𝑝𝑝𝑜𝑠𝑖𝑡𝑒 2

Page 7: VBA for Finance

7

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Generate x and y

Calculate the hypotenuse

Is the hypotenuse shorter than the radius?

Yes No

Count

Repeat Repeat

Page 8: VBA for Finance

8

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Surface of the square: 1

Surface of the circle: 𝜋

4

Surface of the rest: 1 −𝜋

4

Let’s make i trials, count the n points inside the circle

Randomisation has been done uniformly

Proportions will be respected 𝜋 ≅𝑛

𝑖 ∗ 𝑅𝑎𝑑𝑖𝑢𝑠2

Page 9: VBA for Finance

First VBA Program: a formula

9

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

VBA can be sued to create custom Excel Functions

Display the Visual Basic Editor

Right Click on your file and insert a module

Page 10: VBA for Finance

10

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Let’s call our function EstmPi

What are the arguments?

Only one argument

The number of simulations to make

Let’s call the variable Simu

Compulsory to define the type of variable

String: Text

Integer: Natural Number

Double: 64-bits number

Boolean: True/False

What type is Simu?

Page 11: VBA for Finance

11

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Basic structure of the function

Used in Excel

=EstmPi(100)

Result will be the value stored in the variable Result

Time to be reminded the algorithmic fundamentals of our program and learn the corresponding BASIC functions

Page 12: VBA for Finance

12

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Generate x and y

Calculate the hypotenuse

Is the hypotenuse shorter than the radius?

Yes No

Count

Repeat Repeat

If… Then… Else… End If

For… To… Next or

Do While/Until… Loop

Page 13: VBA for Finance

13

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

For… To… Next

For counter = start To end [Step step] [statements] Next [counter]

Full syntax

The program will repeat instructions a specified number of time

Step is optional and by default 1

For i = 1 To Variable Variable2= Variable2+1 Next i

Example

Page 14: VBA for Finance

14

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Do While/Until… Loop

Do [{While | Until} condition] [statements] Loop

Full syntax

The program will repeat instructions while or until a condition is met

Do {While | Until} Variable<>0 Variable= Variable-1 Loop

Example

Careful to potentially endless loop

What if in the Do While case Variable is negative at the start of the loop?

Page 15: VBA for Finance

15

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

If… Then… Else… End If

If condition Then statements [Else] [statements] End If

Full syntax

Else statements are optional

The program will launch instructions if condition is met

If Variable > 0 Then Variable2=1 Variable3=-1 Else Variable= Variable+1 End If

The program will lunch instructions if condition is not met if Else is specified

The program won’t do anything if condition is not met if Else is not specified

Example

Page 16: VBA for Finance

16

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Generate x and y

Calculate the hypotenuse

Is the hypotenuse shorter than the radius?

Yes No

Count

Repeat Repeat

If… Then… Else… End If

For… To… Next or

Do While/Until… Loop

If Hypo<=Radius then VariableCount= VariableCount+1 End If

For i=1 to Simu Next i

Could have use Do Until Variable=Simu…. Loop… But manual increment needed of Variable

Page 17: VBA for Finance

17

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Reminder: basic structure of the function

We know we have to use For/Next and If/Then/End If

What do we miss?

Random number: Rnd command

We Should have everything

Page 18: VBA for Finance

18

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Looping to numbers of simulations

Generate 2 random variables

Calculate the length of the hypotenuse

Is the hypotenuse inside the circle?

Yes: count it

No: Don’t count it

Simulation finished, percentage of points inside the circle

The square area being 1, knowing the radius, you can extract Pi

𝜋 ≅ 3.14159

From how many simulations the estimation is correct?

Page 19: VBA for Finance

19

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

𝜋 ≅ 3.14159

From how many simulations the estimation is correct?

Test in Excel =EstmPi (xxx) for a few different numbers of simulations

Why for a same number of simulations your results are different?

Page 20: VBA for Finance

Alternatives to π estimation

20

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Reminder of our function

Page 21: VBA for Finance

21

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Let’s use Do While / Loop

Don’t forget to fix the initial value of the variable

Don’t forget to increment the variable (beware the endless loop)

Page 22: VBA for Finance

22

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Let’s use Do Until/ Loop

Don’t forget to fix the initial value of the variable

Don’t forget to increment the variable (beware the endless loop)

Page 23: VBA for Finance

23

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

What you CAN do but what you MUST NOT do

Bad algorithmic method

Page 24: VBA for Finance

24

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

“Before going into detail of how to use the GoTo statement it is important to be aware that the use of the GoTo statement is generally considered to be bad programming practice.” John Walkenbach

The program has been led into a corner and some way of getting to another section of code is needed

Bad programming indeed…

Most of the time avoidable if program written carefully

Avoiding GoTo statement make the code easier to debug and maintain

Spaghetti code

Page 25: VBA for Finance

25

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Page 26: VBA for Finance

Formula using Excel References

26

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Let’s build a formula pricing a Black & Scholes Call option

5 or 6 arguments

Time to maturity Or Start Date – End Date

S

r

K

σ

Page 27: VBA for Finance

27

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Easy, no reason to have and loops or conditional tests

Problem… Thee Standard Normal Cumulative distribution is not available in VBA (and it’s not an analytic linear formula so can’t recreate it)

Page 28: VBA for Finance

28

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Basic structure

Time to maturity, d1 and d2 easy to compute

How to compute N(D1) and N(D2)?

Page 29: VBA for Finance

29

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

In Excel, it would be NORMSDIST function

Excel formula can be used in VBA with the following

Final part of the formula can then be written

Use it in Excel

=CallBS(D4,D8,D9,D5,D6,D7)

Page 30: VBA for Finance

30

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Full Formula

Page 31: VBA for Finance

Advanced Formula: CRR

31

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Let’s build a formula pricing a CRR Call option

Challenges

Build a tree with a variable number of nodes

Enables American or European type

7 or 8 arguments

Time to maturity Or Start Date – End Date

S

r

K

σ

n

A/E

Page 32: VBA for Finance

32

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Build the tree for the price of the underlying

Calculate the price of the option at maturity

Do the backward induction

Program steps

Differentiate American & European

Page 33: VBA for Finance

33

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

𝑛 + 1

How many nodes do you have at any given step?

Asymmetric matrix

Page 34: VBA for Finance

34

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

2 dimension variable

Must be defined and re dimensioned

Use Dim and ReDim

Why +1?

Matrix won’t be fully filled

Page 35: VBA for Finance

35

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

𝑢 = 𝑒𝜎 𝑡 𝑑 =1

𝑢= 𝑒−𝜎 𝑡 𝑝 =

𝑒𝑟𝑡 − 𝑑

𝑢 − 𝑑

Compute T, u, d & p

Allocate S to the first node

Page 36: VBA for Finance

36

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Our first double loop…

Times the number of steps

Times current step+1

Page 37: VBA for Finance

37

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

One For/Next inside the other

Construct the tree

Page 38: VBA for Finance

38

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Let’s check…

Introduction to Subs

Display results in Excel

Page 39: VBA for Finance

39

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Tree seems properly built

Back to our function

Time for the backward induction

Page 40: VBA for Finance

40

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Same principle

Two loops one inside the other

Negative Step on the columns

Backward induction

Specific case on the last node

Specific case ITM or OTM

A few if structures inside the other?

Specific cases American or European

Page 41: VBA for Finance

41

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Structure

Yes… Early exercise or not?

Page 42: VBA for Finance

42

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Page 43: VBA for Finance

43

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Final (Initial) Node

Same process

Compare intrinsic to binomial value

Page 44: VBA for Finance

44

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

We’re finished!

1/3

Page 45: VBA for Finance

45

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

3/3

Page 46: VBA for Finance

46

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

2/3

Page 47: VBA for Finance

47

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Use it in Excel

Page 48: VBA for Finance

48

vin

zjea

nn

in@

ho

tmai

l.co

m

ESG

F 4

IFM

Q2

20

12

Conclusion

Functions & Subs

If Then Else

For Next

Do Loop