VBA for Finance
Upcoming SlideShare
Loading in...5
×
 

VBA for Finance

on

  • 1,135 views

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

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

Statistics

Views

Total Views
1,135
Views on SlideShare
1,116
Embed Views
19

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 19

http://www.docshut.com 19

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    VBA for Finance VBA for Finance Presentation Transcript

    • VBA Q2 2012 Vincent JEANNIN – ESGF 4IFM vinzjeannin@hotmail.com ESGF 4IFM Q2 20121
    • 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
    • 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,…
    • 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
    • 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
    • Right Triangle properties ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 𝐻𝑦𝑝𝑜𝑡𝑒𝑛𝑢𝑠𝑒 2 = 𝐴𝑗𝑎𝑐𝑒𝑛𝑡 2 + 𝑂𝑝𝑝𝑜𝑠𝑖𝑡𝑒 2 6
    • 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
    • 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
    • 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
    • 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?
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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?
    • 𝜋 ≅ 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?
    • Alternatives to π estimation Reminder of our function ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 20
    • 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
    • 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
    • What you CAN do but what you MUST NOT do ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 23 Bad algorithmic method
    • “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
    • 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 5 or 6 arguments Time to maturity Or Start Date – End Date vinzjeannin@hotmail.com S r K σ 26
    • 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
    • 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
    • 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
    • Full Formula vinzjeannin@hotmail.com ESGF 4IFM Q2 201230
    • 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
    • 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
    • ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comHow many nodes do you have at any given step? 𝑛+1 33 Asymmetric matrix
    • 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
    • Compute T, u, d & p 𝜎 𝑡 1 𝑒 𝑟𝑡 − 𝑑𝑢= 𝑒 𝑑 = = 𝑒 −𝜎 𝑡 𝑝= 𝑢 𝑢− 𝑑 ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com Allocate S to the first node 35
    • Our first double loop… ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com Times current step+1 36 Times the number of steps
    • One For/Next inside the other ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comConstruct the tree 37
    • Let’s check… Introduction to Subs Display results in Excel ESGF 4IFM Q2 2012 vinzjeannin@hotmail.com 38
    • Tree seems properly built ESGF 4IFM Q2 2012 vinzjeannin@hotmail.comBack to our functionTime for the backward induction 39
    • 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?
    • StructureYes… Early exercise or not? vinzjeannin@hotmail.com ESGF 4IFM Q2 2012 41
    • vinzjeannin@hotmail.com ESGF 4IFM Q2 201242
    • Final (Initial) NodeSame process ESGF 4IFM Q2 2012 Compare intrinsic to binomial value vinzjeannin@hotmail.com 43
    • 1/3 We’re finished! vinzjeannin@hotmail.com ESGF 4IFM Q2 2012 44
    • 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 vinzjeannin@hotmail.com For Next Do Loop 48