I like to start this thread as a tutorial, discussion on learning quantiative programming.. I've seen some people here messaging not knowing how to prgramme and think it's hard.. and so this is a tutorial thread on quantiative programming to show how simple it can be.. so get involve n learn and others who do pls contribute.
!!IMPORTANT: BEFORE CONTINUING, you need to make sure that the MACRO SECURITY SETTING in Excel is set to LOW to allow the macro function to operate. you do this by going to "Tools">"Macro">"Security" and then select "Low" at the bottom. and then restart Excel. if you're concerned, set it back to where it was before after you've gone through this..IMPORTANT!!
i will start with the simple options pricing for the Black-Scholes call price in Excel VBA.. since u all should have VBA.. if you don't know what the B-S formula is about, read a book..
I assume you are at least familiar with Excels spreadsheet and know what A1 or G5 means..so lets open Excel first..
in A1, type "Underlying Price"
in A2, type "Strike Price"
in A3, type "Risk Free Rate"
in A4, type "Maturity"
in A5, type "Volatility"
in C1, type "Black Scholes Call Price"
that was pretty easy. well done. now with here with the fun part.
on Excel top menu bar, click "Tools", then "Macro" and then "Visual Basic Editor".. this opens up the VB..
now here we are going to create a FUNCTION that calculates the B-S call price.
let's call this function BlackScholes. so in that clear part of the screen we start typing away our code. for now copy and past the code below into the editor: (not the ----- lines)
----------------------------------------------
Function BlackScholes(Underlying, Strike, RiskFree, expTime, Volatility)
d1 = (Log(Underlying / Strike) + RiskFree * expTime) / (Volatility * Sqr(expTime)) + _
0.5 * Volatility * Sqr(expTime)
BlackScholes = Underlying * Application.NormSDist(d1) - Strike * Exp(-expTime * RiskFree) * _
Application.NormSDist(d1 - Volatility * Sqr(expTime))
End Function
------------------------------------------
so what is in this code? we see that the b-s function has a number of parameters shown inside the bracket.. what the hell is "d1"? well if you look at the actual formula it becomes clear (http://en.wikipedia.org/wiki/Black-Scholes).. but note how simply we type it in VBA when you compare it to the actual formula.. u can play around with it later.
so now that we have typed in our code, lets get back to our spreadsheet.
So we created a function. let's now use it.
In D1, type the following:
=BlackScholes(B1,B2,B3,B4,B5)
got an idea what the B's are? YES - they correspond to the parameters we identified in our coding for the BlackScholes function.
but look, on the spread sheet, B1 to B5 are blank. we have to fill them in now. (now you know why we created the labels in A1 to A5 earlier - so we know what data we are putting in.. nice..)
So lets fill them in. lets say that the underying price is $60. so we'll put "60" in B1. lets say the strike price is $50, so we'll put "50" in B2. lets also assume the risk free rate is 5.25%, so put "0.0525" in B3. and let's assume matury and volatility are 0.3 and 0.1 respectively. this means we type in "0.3 in B4 and 0.1 in B5.
Oh now look at D1, it gives us the call price.. it's $10.7814..how nice.
that's it! and that only took us 10 min. next time, we can look at how we can actually infer the "volatility" part of the model using a GARCH model.. or we'll do something else in C++ or Python.. I welcome everyone to join in, contribute and take part.
!!IMPORTANT: BEFORE CONTINUING, you need to make sure that the MACRO SECURITY SETTING in Excel is set to LOW to allow the macro function to operate. you do this by going to "Tools">"Macro">"Security" and then select "Low" at the bottom. and then restart Excel. if you're concerned, set it back to where it was before after you've gone through this..IMPORTANT!!
i will start with the simple options pricing for the Black-Scholes call price in Excel VBA.. since u all should have VBA.. if you don't know what the B-S formula is about, read a book..
I assume you are at least familiar with Excels spreadsheet and know what A1 or G5 means..so lets open Excel first..
in A1, type "Underlying Price"
in A2, type "Strike Price"
in A3, type "Risk Free Rate"
in A4, type "Maturity"
in A5, type "Volatility"
in C1, type "Black Scholes Call Price"
that was pretty easy. well done. now with here with the fun part.
on Excel top menu bar, click "Tools", then "Macro" and then "Visual Basic Editor".. this opens up the VB..
now here we are going to create a FUNCTION that calculates the B-S call price.
let's call this function BlackScholes. so in that clear part of the screen we start typing away our code. for now copy and past the code below into the editor: (not the ----- lines)
----------------------------------------------
Function BlackScholes(Underlying, Strike, RiskFree, expTime, Volatility)
d1 = (Log(Underlying / Strike) + RiskFree * expTime) / (Volatility * Sqr(expTime)) + _
0.5 * Volatility * Sqr(expTime)
BlackScholes = Underlying * Application.NormSDist(d1) - Strike * Exp(-expTime * RiskFree) * _
Application.NormSDist(d1 - Volatility * Sqr(expTime))
End Function
------------------------------------------
so what is in this code? we see that the b-s function has a number of parameters shown inside the bracket.. what the hell is "d1"? well if you look at the actual formula it becomes clear (http://en.wikipedia.org/wiki/Black-Scholes).. but note how simply we type it in VBA when you compare it to the actual formula.. u can play around with it later.
so now that we have typed in our code, lets get back to our spreadsheet.
So we created a function. let's now use it.
In D1, type the following:
=BlackScholes(B1,B2,B3,B4,B5)
got an idea what the B's are? YES - they correspond to the parameters we identified in our coding for the BlackScholes function.
but look, on the spread sheet, B1 to B5 are blank. we have to fill them in now. (now you know why we created the labels in A1 to A5 earlier - so we know what data we are putting in.. nice..)
So lets fill them in. lets say that the underying price is $60. so we'll put "60" in B1. lets say the strike price is $50, so we'll put "50" in B2. lets also assume the risk free rate is 5.25%, so put "0.0525" in B3. and let's assume matury and volatility are 0.3 and 0.1 respectively. this means we type in "0.3 in B4 and 0.1 in B5.
Oh now look at D1, it gives us the call price.. it's $10.7814..how nice.
that's it! and that only took us 10 min. next time, we can look at how we can actually infer the "volatility" part of the model using a GARCH model.. or we'll do something else in C++ or Python.. I welcome everyone to join in, contribute and take part.