Python新手,方家莫笑。貌似有现成的库可以引用,但是咱还是想自己动手,解决各种期权软件拉稀值计算口径不一致的问题,另外也可以自己略做发挥。gamma以及gaga没有分上下方向,想要的朋友自己改吧。
10
赞同来自: 废梁 、集思集想 、aladdin898 、ryanxzqn 、dingo49 、 、 、 、 、更多 »
import numpy as np
# BS模型计算购权理论价格
d1 = (np.log(s / k) + (r+pow(sigma, 2)/2)*t)/(sigma*np.sqrt(t))
d2 = d1 -sigma*np.sqrt(t)
B_S_C = s*norm.cdf(d1)-k*np.exp(-r*t)*norm.cdf(d2)
B_S_C = round(B_S_C, 4)
return B_S_C
def B_S_P(s, k, sigma, r, t):
# BS模型计算沽权理论价格
d1 = (np.log(s / k) + (r+pow(sigma, 2)/2)*t)/(sigma*np.sqrt(t))
d2 = d1 -sigma*np.sqrt(t)
B_SC = s*norm.cdf(d1)-k*np.exp(-r*t)*norm.cdf(d2)
B_SC = round(B_SC, 4)
B_S_P = k+B_SC-s
return B_S_P
def B_S_delta(s, k, sigma, r, t):
d1 = (np.log(s / k) + (r+pow(sigma, 2)/2)*t)/(sigma*np.sqrt(t))
d2 = d1 - sigma * np.sqrt(t)
B_S_delta = scipy.stats.norm.cdf(d1)
B_S_delta = round(B_S_delta, 6)
return B_S_delta
def B_S_gama(s, k, sigma, r, t):
d001 = B_S_delta(s, k, sigma, r, t)
if s < 10:
s = s + 0.01
d002 = B_S_delta(s, k, sigma, r, t)
B_S_gama = (d002-d001)*100
elif s < 100:
s = s + 0.1
d002 = B_S_delta(s, k, sigma, r, t)
B_S_gama = (d002-d001)*10
else:
s = s + 1
d002 = B_S_delta(s, k, sigma, r, t)
B_S_gama = (d002-d001)*1
B_S_gama = round(B_S_gama, 6)
return B_S_gama
def B_S_gaga(s, k, sigma, r, t):
# BS模型计算gama的gama,衡量标的变动引起的gama预期变动.
g001 = B_S_gama(s, k, sigma, r, t)
if s < 10:
s = s + 0.01
g002 = B_S_gama(s, k, sigma, r, t)
B_S_gaga = (g002-g001)*1000000
elif s < 100:
s = s + 0.1
g002 = B_S_gama(s, k, sigma, r, t)
B_S_gaga = (g002-g001)*100000
else:
s = s + 1
g002 = B_S_gama(s, k, sigma, r, t)
B_S_gaga = (g002-g001)*10000
B_S_gaga = round(B_S_gaga, 6)
return B_S_gaga
def B_S_theta(s, k, sigma, r, t):
c001 =B_S_C(s, k, sigma, r, t)
t = t + 1/365
c002 =B_S_C(s, k, sigma, r, t)
B_S_theta = c001-c002
B_S_theta = round(B_S_theta, 5)
return B_S_theta
def B_S_Vega(s, k, sigma, r, t):
V001 = B_S_C(s, k, sigma, r, t)
sigma = sigma + 0.01
V002 = B_S_C(s, k, sigma, r, t)
B_S_theta = V002-V001
B_S_theta = round(B_S_theta, 5)
return B_S_theta
BS模型 注意gama,以及gama的gama 计算时都对标的价格变化值作了区别对待。theta表述的是每个自然日时间值消耗。
(s, k, sigma, r, t)参数分别是:标的价格S 行权价K 年化波动率sigma 无风险年利率r 按年计算的剩余到期日t
def B_S_C(s, k, sigma, r, t):# BS模型计算购权理论价格
d1 = (np.log(s / k) + (r+pow(sigma, 2)/2)*t)/(sigma*np.sqrt(t))
d2 = d1 -sigma*np.sqrt(t)
B_S_C = s*norm.cdf(d1)-k*np.exp(-r*t)*norm.cdf(d2)
B_S_C = round(B_S_C, 4)
return B_S_C
def B_S_P(s, k, sigma, r, t):
# BS模型计算沽权理论价格
d1 = (np.log(s / k) + (r+pow(sigma, 2)/2)*t)/(sigma*np.sqrt(t))
d2 = d1 -sigma*np.sqrt(t)
B_SC = s*norm.cdf(d1)-k*np.exp(-r*t)*norm.cdf(d2)
B_SC = round(B_SC, 4)
B_S_P = k+B_SC-s
return B_S_P
def B_S_delta(s, k, sigma, r, t):
d1 = (np.log(s / k) + (r+pow(sigma, 2)/2)*t)/(sigma*np.sqrt(t))
d2 = d1 - sigma * np.sqrt(t)
B_S_delta = scipy.stats.norm.cdf(d1)
B_S_delta = round(B_S_delta, 6)
return B_S_delta
def B_S_gama(s, k, sigma, r, t):
d001 = B_S_delta(s, k, sigma, r, t)
if s < 10:
s = s + 0.01
d002 = B_S_delta(s, k, sigma, r, t)
B_S_gama = (d002-d001)*100
elif s < 100:
s = s + 0.1
d002 = B_S_delta(s, k, sigma, r, t)
B_S_gama = (d002-d001)*10
else:
s = s + 1
d002 = B_S_delta(s, k, sigma, r, t)
B_S_gama = (d002-d001)*1
B_S_gama = round(B_S_gama, 6)
return B_S_gama
def B_S_gaga(s, k, sigma, r, t):
# BS模型计算gama的gama,衡量标的变动引起的gama预期变动.
g001 = B_S_gama(s, k, sigma, r, t)
if s < 10:
s = s + 0.01
g002 = B_S_gama(s, k, sigma, r, t)
B_S_gaga = (g002-g001)*1000000
elif s < 100:
s = s + 0.1
g002 = B_S_gama(s, k, sigma, r, t)
B_S_gaga = (g002-g001)*100000
else:
s = s + 1
g002 = B_S_gama(s, k, sigma, r, t)
B_S_gaga = (g002-g001)*10000
B_S_gaga = round(B_S_gaga, 6)
return B_S_gaga
def B_S_theta(s, k, sigma, r, t):
c001 =B_S_C(s, k, sigma, r, t)
t = t + 1/365
c002 =B_S_C(s, k, sigma, r, t)
B_S_theta = c001-c002
B_S_theta = round(B_S_theta, 5)
return B_S_theta
def B_S_Vega(s, k, sigma, r, t):
V001 = B_S_C(s, k, sigma, r, t)
sigma = sigma + 0.01
V002 = B_S_C(s, k, sigma, r, t)
B_S_theta = V002-V001
B_S_theta = round(B_S_theta, 5)
return B_S_theta