February 12th, 2024, 5:02 pm
The Black Scholes option pricer in Fortran95
// DJD
real*8 Function pdf(x)
real*8 x,A
A = 1.0/Sqrt(2.0*3.1415)
pdf = A * Exp(-0.5*x*x)
return
end
real*8 Function cdf(x)
real*8 DPI,x,L,k,a1,a2,a3,tmp,pdf
a1 = 0.4361836
a2 = -0.1201676
a3 = 0.9372980
DPI = 3.1415926535897932
L = Abs(x)
k = 1. / (1. + 0.33267*x)
tmp = a1*k+ a2 * k**2. + a3 * k**3.
cdf = 1.0 - pdf(x)*tmp
if(x.lt.0.) then
cdf = pdf(x)*tmp
end if
return
end
real*8 Function BlackScholes(S, X, T, r, v)
! Put
real*8 S,X,T,r,v
real*8 d1, d2
d1 = (Log(S / X) + (r + v**2. / 2.) * T) / (v * Sqrt(T))
d2 = d1 - v * Sqrt(T)
BlackScholes = X * Exp(-r * T) * cdf(-d2) - S * cdf(-d1)
Return
End
! A fortran95 program for BS option
! By djd
!
program main
implicit none
integer anyKey
real*8 S,K,T,r,v
real*8 BlackScholes
real*8 price
S = 60.0
K = 65.0
T = 0.25
r = 0.08
v = 0.3
price = BlackScholes(S,K,T,r,v)
write(*,*) price
anyKey = system("pause")
end