-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathblack_scholes.py
38 lines (32 loc) · 1.09 KB
/
black_scholes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
# @Author: boyac
# @Date: 2016-05-02 18:28:28
# @Last Modified by: boyac
# @Last Modified time: 2016-05-04 00:27:52
from __future__ import division
from scipy.stats import norm
from math import *
# Cumulative normal distribution
def CND(X):
return norm.cdf(X)
# Black Sholes Function
def BlackScholes(CallPutFlag,S,K,t,r,s):
"""
S = Current stock price
t = Time until option exercise (years to maturity)
K = Option striking price
r = Risk-free interest rate
N = Cumulative standard normal distribution
e = Exponential term
s = St. Deviation (volatility)
Ln = NaturalLog
"""
d1 = (log(S/K) + (r + (s ** 2)/2) * t)/(s * sqrt(t))
d2 = d1 - s * sqrt(t)
if CallPutFlag=='c':
return S * CND(d1) - K * exp(-r * t) * CND(d2) # call option
else:
return K * exp(-r * t) * CND(-d2) - S * CND(-d1) # put option
if __name__ == "__main__":
# Number taken from: http://wiki.mbalib.com/wiki/Black-Scholes期权定价模型
print BlackScholes('c', S=164.0, K=165.0, t=0.0959, r=0.0521, s=0.29) # 5.788529972549341