自己算期权希腊值Python代码分享给需要的集友

Python新手,方家莫笑。貌似有现成的库可以引用,但是咱还是想自己动手,解决各种期权软件拉稀值计算口径不一致的问题,另外也可以自己略做发挥。gamma以及gaga没有分上下方向,想要的朋友自己改吧。
发表时间 2022-10-03 13:31     来自上海

赞同来自: bravolam 上升不会 dongbeisong dhhlys hyftctct更多 »

0

上升不会

赞同来自:

非常感谢
2022-10-13 09:42 来自上海 引用
0

扫地小僧

赞同来自:

貌似不错,学习下。。
2022-10-05 23:06 来自广东 引用
0

hyftctct

赞同来自:

谢谢分享
2022-10-03 18:03 来自河北 引用
10

yyttcc705

赞同来自: 废梁 集思集想 aladdin898 ryanxzqn dingo49 却无浩然气 至味清欢 chineseumi zyynull 数据矿工更多 »

import numpy as np

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

以上希腊值函数全部ok!!!

2022-10-03 14:45 来自上海 引用

要回复问题请先登录注册

发起人

问题状态

  • 最新活动: 2022-10-13 09:42
  • 浏览: 2409
  • 关注: 26