'// Author's blog: '// Heart of the Finance '// http://heartofthefinance.blogspot.com/ '// '// Black-Scholes Option Princing Model for dividend paying underlying assets VBA code (Premium only) '// version 1.0.0 '// Last update: 1/15/2011 Public Function BlackScholes(CallPutFlag As String, S As Double, K As Double, T As Double, r As Double, q As Double, v As Double) As Double 'S: spot price of an underlying asset 'K: strike price 'T: (T-t); time to maturity from today. i.e. T: time at maturity, t: today 'r: risk-free rate (annualized, continuous compounding) 'q: dividend yield (annualized, continuous compounding) 'v:volatility (standard deviation of return) of an underlying asset (annualized) Dim d1 As Double, d2 As Double d1 = (Log(S / K) + (r - q + v ^ 2 / 2) * T) / (v * Sqr(T)) d2 = d1 - v * Sqr(T) If CallPutFlag = "c" Then BlackScholes = Exp(-q * T) * S * CND(d1) - K * Exp(-r * T) * CND(d2) ElseIf CallPutFlag = "p" Then 'BlackScholes = K * Exp(-r * T) * CND(-d2) - S * Exp(-q * T) * CND(-d1) BlackScholes = K * Exp(-r * T) * (1 - CND(d2)) - S * Exp(-q * T) * (1 - CND(d1)) End If End Function '// The cumulative normal distribution function Public Function CND(X As Double) As Double Dim L As Double, K As Double Const a1 = 0.31938153: Const a2 = -0.356563782: Const a3 = 1.781477937: Const a4 = -1.821255978: Const a5 = 1.330274429 L = Abs(X) K = 1 / (1 + 0.2316419 * L) CND = 1 - 1 / Sqr(2 * Application.Pi()) * Exp(-L ^ 2 / 2) * (a1 * K + a2 * K ^ 2 + a3 * K ^ 3 + a4 * K ^ 4 + a5 * K ^ 5) If X < 0 Then CND = 1 - CND End If End Function |
'// Author's blog: '// Heart of the Finance '// http://heartofthefinance.blogspot.com/ '// '// Black-Scholes Option Princing Model for dividend paying underlying assets VBA code (Premium and Greeks) '// version 1.0.0 '// Last update: 1/15/2011 '**************************************************************************** '* Cumulative Standard Normal Distribution * '* (This function provides similar result as NORMSDIST( ) on Excel) * '**************************************************************************** Function SNorm(z) c1 = 2.506628 c2 = 0.3193815 c3 = -0.3565638 c4 = 1.7814779 c5 = -1.821256 c6 = 1.3302744 If z > 0 Or z = 0 Then w = 1 Else: w = -1 End If y = 1 / (1 + 0.2316419 * w * z) SNorm = 0.5 + w * (0.5 - (Exp(-z * z / 2) / c1) * _ (y * (c2 + y * (c3 + y * (c4 + y * (c5 + y * c6)))))) End Function '********************************************************************** '* Black-Scholes European Call Price Computation * '********************************************************************** Function Call_Eur(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single 's: spot price of an underlying asset 'k: strike price 't: (T-t); time to maturity. i.e. T: maturity, t: current 'r: risk-free rate (annual rate, continuous compounding) 'q: dividend yield (annual rate, continuous compounding) 'sd:volatility (standard deviation of return) of an underlying asset a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Call_Eur = Exp(-q * t) * s * SNorm(d1) - k * Exp(-r * t) * SNorm(d2) End Function '********************************************************************* '* Black-Scholes European Put Price Computation * '********************************************************************* Function Put_Eur(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) 'Put-call parity 'Call_Eur_tmp = Exp(-q * t) * s * SNorm(d1) - k * Exp(-r * t) * SNorm(d2) 'Put_Eur = -s * Exp(-q * t) + Call_Eur_tmp + k * Exp(-r * t) Put_Eur = k * Exp(-r * t) * (1 - SNorm(d2)) - s * Exp(-q * t) * (1 - SNorm(d1)) End Function '********************************************************************** '* Black-Scholes European Call Delta Computation * '********************************************************************** Function Call_Eur_Delta(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Call_Eur_Delta = Exp(-q * t) * SNorm(d1) End Function '********************************************************************* '* Black-Scholes European Put Delta Computation * '********************************************************************* Function Put_Eur_Delta(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Put_Eur_Delta = -Exp(-q * t) * (1 - SNorm(d1)) End Function '********************************************************************** '* Black-Scholes European Call Gamma Computation * '********************************************************************** Function Call_Eur_Gamma(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Call_Eur_Gamma = Exp(-q * t) / (s * sd * t ^ 0.5) * Exp(-d1 ^ 2 / 2) / (2 * Application.WorksheetFunction.Pi()) ^ 0.5 End Function '********************************************************************** '* Black-Scholes European Put Gamma Computation * '********************************************************************** Function Put_Eur_Gamma(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Put_Eur_Gamma = Exp(-q * t) / (s * sd * t ^ 0.5) * Exp(-d1 ^ 2 / 2) / (2 * Application.WorksheetFunction.Pi()) ^ 0.5 End Function '********************************************************************** '* Black-Scholes European Call Vega Computation * '********************************************************************** Function Call_Eur_Vega(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Call_Eur_Vega = Exp(-q * t) * s * (t ^ 0.5) * Exp(-d1 ^ 2 / 2) / (2 * Application.WorksheetFunction.Pi()) ^ 0.5 End Function '********************************************************************** '* Black-Scholes European Put Vega Computation * '********************************************************************** Function Put_Eur_Vega(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Put_Eur_Vega = Exp(-q * t) * s * (t ^ 0.5) * Exp(-d1 ^ 2 / 2) / (2 * Application.WorksheetFunction.Pi()) ^ 0.5 End Function '********************************************************************** '* Black-Scholes European Call Theta Computation * '********************************************************************** Function Call_Eur_Theta(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Call_Eur_Theta = q * Exp(-q * t) * s * SNorm(d1) - Exp(-q * t) * s * sd / (2 * (t) ^ 0.5) * Exp(-d1 ^ 2 / 2) / (2 * Application.WorksheetFunction.Pi()) ^ 0.5 - k * r * Exp(-r * t) * SNorm(d2) End Function '********************************************************************** '* Black-Scholes European Put Theta Computation * '********************************************************************** Function Put_Eur_Theta(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Put_Eur_Theta = q * Exp(-q * t) * s * (-1) * SNorm(-d1) - Exp(-q * t) * s * sd / (2 * (t) ^ 0.5) * Exp(-d1 ^ 2 / 2) / (2 * Application.WorksheetFunction.Pi()) ^ 0.5 + k * r * Exp(-r * t) * SNorm(-d2) End Function '********************************************************************** '* Black-Scholes European Call Rho Computation * '********************************************************************** Function Call_Eur_Rho(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Call_Eur_Rho = k * t * Exp(-r * t) * SNorm(d2) End Function '********************************************************************** '* Black-Scholes European Put Rho Computation * '********************************************************************** Function Put_Eur_Rho(s, k, t, r, q, sd) Dim a As Single Dim b As Single Dim c As Single Dim d1 As Single Dim d2 As Single a = Log(s / k) b = (r - q + 0.5 * sd ^ 2) * t c = sd * (t ^ 0.5) d1 = (a + b) / c d2 = d1 - sd * (t ^ 0.5) Put_Eur_Rho = -k * t * Exp(-r * t) * SNorm(-d2) End Function |
Ref.
http://en.wikipedia.org/wiki/Black-Scholes
http://www.worldscibooks.com/etextbook/p556/p556_chap04.pdf
http://www.espenhaug.com/black_scholes.html
No comments:
Post a Comment