vba for finance
DESCRIPTION
4th Year of MSc, Options Pricing with Black & Scholes and Cox, Ross & Rubinstein using VBA in Excel.TRANSCRIPT
VBA Vincent JEANNIN – ESGF 4IFM
Q2 2012
1
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
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
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
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
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?
6
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
Right Triangle properties
𝐻𝑦𝑝𝑜𝑡𝑒𝑛𝑢𝑠𝑒 2 = 𝐴𝑗𝑎𝑐𝑒𝑛𝑡 2 + 𝑂𝑝𝑝𝑜𝑠𝑖𝑡𝑒 2
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
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
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
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?
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
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
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
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?
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
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
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
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?
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?
Alternatives to π estimation
20
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
Reminder of our function
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)
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)
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
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
25
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
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
σ
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)
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)?
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)
30
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
Full Formula
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
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
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
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
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
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
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
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
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
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
41
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
Structure
Yes… Early exercise or not?
42
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
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
44
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
We’re finished!
1/3
45
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
3/3
46
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
2/3
47
vin
zjea
nn
in@
ho
tmai
l.co
m
ESG
F 4
IFM
Q2
20
12
Use it in Excel
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