VBA                         Q2 2012                                   Vincent JEANNIN – ESGF 4IFM    vinzjeannin@hotmail.c...
ESGF 4IFM Q2 2012Summary of the session•   Introduction: from BASIC to VBA•   Think Algorithmic First: π•   First VBA Prog...
Introduction: from BASIC to VBA             Beginners All-purpose Symbolic Instruction Code                   Created in 1...
Think Algorithmic First: π                                                                      ESGF 4IFM Q2 2012       Le...
Let’s pick 2 random numbersUsing a uniform distribution                                                                 ES...
Right Triangle properties                                         ESGF 4IFM Q2 2012                                       ...
Repeat                                    Repeat                  Generate x and y                                        ...
Surface of the square: 1                                      𝜋               Surface of the circle:                      ...
First VBA Program: a formula      VBA can be sued to create custom Excel Functions                                        ...
Let’s call our function EstmPi         What are the arguments?                                                            ...
Basic structure of the function                                                          ESGF 4IFM Q2 2012Used in Excel   ...
Repeat                                    Repeat                                    Generate x and y                      ...
For… To… NextFull syntax          For counter = start To end [Step step]          [statements]                            ...
Do While/Until… LoopFull syntax          Do [{While | Until} condition]          [statements]                             ...
If… Then… Else… End IfFull syntax          If condition Then             statements                                       ...
Repeat                                   Repeat                                    Generate x and y                       ...
Reminder: basic structure of the function                                                     ESGF 4IFM Q2 2012We know we ...
Looping to numbers of simulationsGenerate 2 random variables                                                              ...
𝜋 ≅ 3.14159                                                                         ESGF 4IFM Q2 2012                     ...
Alternatives to π estimation   Reminder of our function                               ESGF 4IFM Q2 2012                   ...
Let’s use Do While / LoopDon’t forget to fix the initial value of the variableDon’t forget to increment the variable (bewa...
Let’s use Do Until/ LoopDon’t forget to fix the initial value of the variableDon’t forget to increment the variable (bewar...
What you CAN do but what you MUST NOT do                                           ESGF 4IFM Q2 2012                      ...
“Before going into detail of how to use the     GoTo statement it is important to be aware     that the use of the GoTo st...
vinzjeannin@hotmail.com   ESGF 4IFM Q2 201225
Formula using Excel References    Let’s build a formula pricing a Black & Scholes Call option                             ...
ESGF 4IFM Q2 2012                                                           vinzjeannin@hotmail.comEasy, no reason to have...
Basic structure                                              ESGF 4IFM Q2 2012Time to maturity, d1 and d2 easy to compute ...
In Excel, it would be NORMSDIST functionExcel formula can be used in VBA with the following                               ...
Full Formula     vinzjeannin@hotmail.com   ESGF 4IFM Q2 201230
Advanced Formula: CRR    Let’s build a formula pricing a CRR Call option                                                  ...
Program steps                                                        ESGF 4IFM Q2 2012       Build the tree for the price ...
ESGF 4IFM Q2 2012                                                vinzjeannin@hotmail.comHow many nodes do you have at any ...
2 dimension variable      Must be defined and re dimensioned                                           ESGF 4IFM Q2 2012  ...
Compute T, u, d & p       𝜎 𝑡                   1                    𝑒 𝑟𝑡 − 𝑑𝑢= 𝑒            𝑑 = = 𝑒 −𝜎       𝑡           ...
Our first double loop…                                                               ESGF 4IFM Q2 2012                    ...
One For/Next inside the other                                ESGF 4IFM Q2 2012                                vinzjeannin@...
Let’s check…       Introduction to Subs       Display results in Excel                                  ESGF 4IFM Q2 2012 ...
Tree seems properly built                                  ESGF 4IFM Q2 2012                                  vinzjeannin@...
Same principle      Two loops one inside the other                                                     ESGF 4IFM Q2 2012  ...
StructureYes… Early exercise or not?                                   vinzjeannin@hotmail.com   ESGF 4IFM Q2 2012        ...
vinzjeannin@hotmail.com   ESGF 4IFM Q2 201242
Final (Initial) NodeSame process                                               ESGF 4IFM Q2 2012         Compare intrinsic...
1/3                                                         We’re finished!           vinzjeannin@hotmail.com   ESGF 4IFM ...
3/3           vinzjeannin@hotmail.com   ESGF 4IFM Q2 2012      45
2/3           vinzjeannin@hotmail.com   ESGF 4IFM Q2 2012      46
Use it in Excel     vinzjeannin@hotmail.com   ESGF 4IFM Q2 201247
Conclusion                           ESGF 4IFM Q2 2012        Functions & Subs        If Then Else                        ...
Upcoming SlideShare
Loading in...5
×

VBA for Finance

1,179

Published on

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

Published in: Economy & Finance
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,179
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

VBA for Finance

  1. 1. VBA Q2 2012 Vincent JEANNIN – ESGF 4IFM vinzjeannin@hotmail.com ESGF 4IFM Q2 20121
  2. 2. ESGF 4IFM Q2 2012Summary of the session• Introduction: from BASIC to VBA• Think Algorithmic First: π• First VBA Program: a formula vinzjeannin@hotmail.com• Alternatives to π estimation• Formula using Excel References• Advanced Formula: CRR 2
  3. 3. Introduction: from BASIC to VBA Beginners All-purpose Symbolic Instruction Code Created in 1964 by students for students ESGF 4IFM Q2 2012 Spread because free Succeed because easy and base to most of languages vinzjeannin@hotmail.com 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 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 3 functions, automating processes,…
  4. 4. Think Algorithmic First: π ESGF 4IFM Q2 2012 Let’s approximate pi vinzjeannin@hotmail.com Think first about “How to” regardless of any programming Draw a chart of the program then the “translation” is easier 4
  5. 5. Let’s pick 2 random numbersUsing a uniform distribution ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comHow do you determine if (x,y) is inside or outside the circle? 5
  6. 6. Right Triangle properties ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 𝐻𝑦𝑝𝑜𝑡𝑒𝑛𝑢𝑠𝑒 2 = 𝐴𝑗𝑎𝑐𝑒𝑛𝑡 2 + 𝑂𝑝𝑝𝑜𝑠𝑖𝑡𝑒 2 6
  7. 7. Repeat Repeat Generate x and y ESGF 4IFM Q2 2012 Calculate the hypotenuse vinzjeannin@hotmail.com Is the hypotenuse shorter than the radius? Yes No Count 7
  8. 8. Surface of the square: 1 𝜋 Surface of the circle: 4 𝜋 Surface of the rest: 1 − 4 ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comLet’s make i trials, count the n points inside the circleRandomisation has been done uniformly 8 𝑛Proportions will be respected 𝜋≅ 𝑖 ∗ 𝑅𝑎𝑑𝑖𝑢𝑠 2
  9. 9. First VBA Program: a formula VBA can be sued to create custom Excel Functions ESGF 4IFM Q2 2012 Display the Visual Basic Editor vinzjeannin@hotmail.com Right Click on your file and insert a module 9
  10. 10. Let’s call our function EstmPi What are the arguments? ESGF 4IFM Q2 2012 Only one argument The number of simulations to make Let’s call the variable Simu vinzjeannin@hotmail.com Compulsory to define the type of variable String: Text Integer: Natural Number Double: 64-bits number Boolean: True/False 10What type is Simu?
  11. 11. Basic structure of the function ESGF 4IFM Q2 2012Used in Excel vinzjeannin@hotmail.com=EstmPi(100)Result will be the value stored in the variable ResultTime to be reminded the algorithmic fundamentals of ourprogram and learn the corresponding BASIC functions 11
  12. 12. Repeat Repeat Generate x and y ESGF 4IFM Q2 2012 For… To… Next Calculate the hypotenuse orDo While/Until… Loop vinzjeannin@hotmail.com Is the hypotenuse shorter than the radius? If… Then… Else… End If Yes No Count 12
  13. 13. For… To… NextFull syntax For counter = start To end [Step step] [statements] ESGF 4IFM Q2 2012 Next [counter]The program will repeat instructions a specified number of time vinzjeannin@hotmail.comStep is optional and by default 1 For i = 1 To Variable Example Variable2= Variable2+1 Next i 13
  14. 14. Do While/Until… LoopFull syntax Do [{While | Until} condition] [statements] ESGF 4IFM Q2 2012 LoopThe program will repeat instructions while or until a condition is met vinzjeannin@hotmail.com Do {While | Until} Variable<>0 Example Variable= Variable-1 LoopCareful to potentially endless loopWhat if in the Do While case Variable is negative at the start of the loop? 14
  15. 15. If… Then… Else… End IfFull syntax If condition Then statements ESGF 4IFM Q2 2012 [Else] [statements] End IfThe program will launch instructions if condition is met vinzjeannin@hotmail.comElse statements are optional 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 If Variable > 0 Then Variable2=1 Variable3=-1 15 Example Else Variable= Variable+1 End If
  16. 16. Repeat Repeat Generate x and y ESGF 4IFM Q2 2012 For… To… Next Calculate the hypotenuse orDo While/Until… Loop Is the hypotenuse shorter than the radius? vinzjeannin@hotmail.com If… Then… Else… End If Yes No Count For i=1 to Simu If Hypo<=Radius then VariableCount= VariableCount+1 Next i End If 16 Could have use Do Until Variable=Simu…. Loop… But manual increment needed of Variable
  17. 17. Reminder: basic structure of the function ESGF 4IFM Q2 2012We know we have to use For/Next and If/Then/End If vinzjeannin@hotmail.comWhat do we miss? Random number: Rnd commandWe Should have everything 17
  18. 18. Looping to numbers of simulationsGenerate 2 random variables ESGF 4IFM Q2 2012Calculate the length of the hypotenuseIs the hypotenuse inside the circle? vinzjeannin@hotmail.com Yes: count it No: Don’t count itSimulation finished, percentage of points inside the circleThe square area being 1, knowing the radius, you can extract Pi 𝜋 ≅ 3.14159 18 From how many simulations the estimation is correct?
  19. 19. 𝜋 ≅ 3.14159 ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comTest in Excel =EstmPi (xxx) for a few different numbers of simulations 19Why for a same number of simulations your results are different?From how many simulations the estimation is correct?
  20. 20. Alternatives to π estimation Reminder of our function ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 20
  21. 21. Let’s use Do While / LoopDon’t forget to fix the initial value of the variableDon’t forget to increment the variable (beware the endless loop) ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 21
  22. 22. Let’s use Do Until/ LoopDon’t forget to fix the initial value of the variableDon’t forget to increment the variable (beware the endless loop) ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 22
  23. 23. What you CAN do but what you MUST NOT do ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 23 Bad algorithmic method
  24. 24. “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 ESGF 4IFM Q2 2012The program has been led into a corner and some way of getting toanother section of code is needed vinzjeannin@hotmail.com Bad programming indeed… Most of the time avoidable if program written carefully Avoiding GoTo statement make the code easier to debug and maintain 24Spaghetti code
  25. 25. vinzjeannin@hotmail.com ESGF 4IFM Q2 201225
  26. 26. Formula using Excel References Let’s build a formula pricing a Black & Scholes Call option ESGF 4IFM Q2 2012 5 or 6 arguments Time to maturity Or Start Date – End Date vinzjeannin@hotmail.com S r K σ 26
  27. 27. ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comEasy, no reason to have and loops or conditional testsProblem… Thee Standard Normal Cumulative distributionis not available in VBA (and it’s not an analytic linearformula so can’t recreate it) 27
  28. 28. Basic structure ESGF 4IFM Q2 2012Time to maturity, d1 and d2 easy to compute vinzjeannin@hotmail.comHow to compute N(D1) and N(D2)? 28
  29. 29. In Excel, it would be NORMSDIST functionExcel formula can be used in VBA with the following ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comFinal part of the formula can then be written Use it in Excel =CallBS(D4,D8,D9,D5,D6,D7) 29
  30. 30. Full Formula vinzjeannin@hotmail.com ESGF 4IFM Q2 201230
  31. 31. Advanced Formula: CRR Let’s build a formula pricing a CRR Call option ESGF 4IFM Q2 2012 Challenges Build a tree with a variable number of nodes vinzjeannin@hotmail.com Enables American or European type 7 or 8 arguments Time to maturity Or Start Date – End Date σ S n 31 r A/E K
  32. 32. Program steps ESGF 4IFM Q2 2012 Build the tree for the price of the underlying vinzjeannin@hotmail.com Calculate the price of the option at maturity Do the backward induction Differentiate American & European 32
  33. 33. ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comHow many nodes do you have at any given step? 𝑛+1 33 Asymmetric matrix
  34. 34. 2 dimension variable Must be defined and re dimensioned ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comUse Dim and ReDim 34 Why +1? Matrix won’t be fully filled
  35. 35. Compute T, u, d & p 𝜎 𝑡 1 𝑒 𝑟𝑡 − 𝑑𝑢= 𝑒 𝑑 = = 𝑒 −𝜎 𝑡 𝑝= 𝑢 𝑢− 𝑑 ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com Allocate S to the first node 35
  36. 36. Our first double loop… ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com Times current step+1 36 Times the number of steps
  37. 37. One For/Next inside the other ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comConstruct the tree 37
  38. 38. Let’s check… Introduction to Subs Display results in Excel ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 38
  39. 39. Tree seems properly built ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comBack to our functionTime for the backward induction 39
  40. 40. Same principle Two loops one inside the other ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com Negative Step on the columns Backward inductionSpecific case on the last node Specific case ITM or OTMSpecific cases American or European 40 A few if structures inside the other?
  41. 41. StructureYes… Early exercise or not? vinzjeannin@hotmail.com ESGF 4IFM Q2 2012 41
  42. 42. vinzjeannin@hotmail.com ESGF 4IFM Q2 201242
  43. 43. Final (Initial) NodeSame process ESGF 4IFM Q2 2012 Compare intrinsic to binomial value vinzjeannin@hotmail.com 43
  44. 44. 1/3 We’re finished! vinzjeannin@hotmail.com ESGF 4IFM Q2 2012 44
  45. 45. 3/3 vinzjeannin@hotmail.com ESGF 4IFM Q2 2012 45
  46. 46. 2/3 vinzjeannin@hotmail.com ESGF 4IFM Q2 2012 46
  47. 47. Use it in Excel vinzjeannin@hotmail.com ESGF 4IFM Q2 201247
  48. 48. Conclusion ESGF 4IFM Q2 2012 Functions & Subs If Then Else vinzjeannin@hotmail.com For Next Do Loop 48
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×