DolphinDB计算期权希腊字母

期权中的希腊字母包括Delta、Gamma、Theta、Vega、Rho在内,共五个。一个期权非常宝贵的一个特性,这个特性就叫正Gamma,赚钱的时候速度是越来越快的,亏钱的速度是越来越慢的。期权的价格和三个变量有关系,第一个变量是标的的价格(Delte),第二个是市场的隐含波动率(vega),第三个是时间(Theta)的流逝。

期权中的希腊字母包括Delta、Gamma、Theta、Vega、Rho在内,共五个。一个期权非常宝贵的一个特性,这个特性就叫正Gamma,赚钱的时候速度是越来越快的,亏钱的速度是越来越慢的。期权的价格和三个变量有关系,第一个变量是标的的价格(Delte),第二个是市场的隐含波动率(vega),第三个是时间(Theta)的流逝。期权交易员主要关注期权的这些特性。

通过dolphindb,计算50etf期权执行价为3的看涨和看跌期权的希腊字母。耗时在0.9ms

use options_BS
spot=3.018////现货价格
strike_price=3.//行权价格
maturity=(2022.07.27-2022.06.30)\365.////剩余时间
r=0.0//无风险利率
options_type="C"////看涨期权
options_price=0.08////期权价格
timer greaks=bs_greaks(spot,strike_price,maturity, r,options_type,options_price=options_price)
//////Time elapsed: 0.994 ms
print(round(greaks,4))
/////[0.2164,0.08,0.5521,0.3247,-0.4748,0.0012,2.2269,0.5288,1.6664,0.2028,-0.0013,0.0032]
 


spot=3.018////现货价格
strike_price=3.//行权价格
maturity=(2022.07.27-2022.06.30)\365.////剩余时间
r=0.0//无风险利率
options_type="P"////看涨期权
options_price=0.0574////期权价格
timer greaks=bs_greaks(spot,strike_price,maturity, r,options_type,options_price=options_price)
//////Time elapsed: 0.995 ms
print(round(greaks,4))
/////[0.2022,0.0574,-0.4458,0.3244,-0.4434,-0.001,2.3814,0.5324,-1.3454,0.2169,-0.0012,0.0032]

BS模型的脚本代码如下

module options_BS
/////
def pdfNorm(mean,var,x){
	return exp(-square(x -mean) / (2 * square(var))) / (sqrt(2 * pi) * var)
 }
def bs_price(options_type, spot, strike, maturity, r, vol, q=0.0){
	/*
	 * options_type:'期权类型 ,C' or 'P'
	 * spot:现货价,float:
	 * strike:行权价,float 
	 * maturity:剩余天数,float、int
	 * r:无风险利率,float
	 * vol:波动率,float
	*/
	 if (options_type == 'C'){
	 	phi = 1}
	 else if(options_type == 'P'){
	 	phi = -1
	 	}
	 else{
	 	print("The Options Type is Wrong!")
	 	return
	 }
	if (vol == 0 or maturity == 0){
		return max(0.0, phi * (spot - strike))
		
	}
	if (strike == 0){
		print('The strike_price price cannot be zero')
		}
	d1 = (log(spot \strike) + (r + 0.5 * vol * vol) * maturity) \vol \sqrt(maturity)
	d2 = d1 - vol * sqrt(maturity)
    	return phi * spot * cdfNormal(0,1,phi * d1) - phi * strike * exp(-r * maturity) * cdfNormal(0,1,phi * d2)
}

def bs_vega(spot, strike, maturity, r, vol, q=0.0){
	/*
	 * spot:现货价,float:
	 * strike:行权价,float 
	 * maturity:剩余天数,float、int
	 * r:无风险利率,float
	 * vol:波动率,float
	*/
	d1 = (log(spot \ strike) + (r + vol * vol \ 2.) * maturity) \ (vol * sqrt(maturity))
	return spot * sqrt(maturity) * pdfNorm(0,1,d1)
}

def implied_volatility_Newton(options_price, options_type, spot, strike, maturity, r){
	/*
	 * 牛顿法实现期权隐含波动率计算
	 *  *options_price,期权价格,float:
	 * spot:现货价,float:
	 * strike:行权价,float 
	 * maturity:剩余天数,float、int
	 * r:无风险利率,float
	 * vol:波动率,float
	 * options_type:'期权类型 ,C' or 'P'
	 * 
	*/
	 if (options_type == 'C'){
	 	phi = 1}
	 else if(options_type == 'P'){
	 	phi = -1
	 	}
	 else{
	 	print("The Options Type is Wrong!")
	 	return
	 }
	max_iterations= 100
	precision= 1.0e-5
	sigma = 0.5
	for (i in 1..max_iterations){
	    price = bs_price(options_type, spot, strike, maturity, r, sigma)
	    vega = bs_vega( spot, strike, maturity, r, sigma)
	    diff = options_price - price
	    if (abs(diff) < precision){
	        return sigma
	        }
	    sigma = sigma + diff / vega 
	}
	return sigma
}
def bs_greaks(spot,strike_price,maturity, r,options_type,vol=NULL,options_price=NULL){
	//////
	/*
	 * Black-Scholes
	 * 针对无分红股票欧式期权,使用dolphindb写的Black - Scholes公式
	 * 参数:
	 * spot:现货价,float:
	 * strike_price:行权价,float
	 * maturity:剩余天数,折算到年,float、int
	 * r:无风险利率,float
	 * volatility:波动率,float
	 * options_type:期权类型('C'、'P')
	 * 
	*/
	if (options_type == 'C'){
		phi = 1}
	else if(options_type == 'P'){
		phi = -1}
	else{
		print("The Options Type is Wrong!")
		return 
	}
	if (strike_price==0){
		print('The strike_price price cannot be zero')	
		return  
	}
	if (isValid(options_price)){
		/////the option iv 
	            volatility = implied_volatility_Newton(options_price, options_type, spot, strike_price, maturity, r)
	            ///option price: [Call price, Put price]
		price=options_price
            }
            if (isValid(vol)){
	            /////the option iv 
		volatility=vol
            }
       	a_ = volatility * sqrt(maturity)
       	d1_ = (log(spot \strike_price) + (r + 0.5 * volatility * volatility) * maturity) \ a_
       	d2_ =d1_ - a_
       	b_ = exp(-(r * maturity))
       	
       	if (isValid(volatility)){
	            ///option price: [Call price, Put price]
		price=iif((volatility == 0 || maturity == 0),max(0.0, phi * (spot - strike_price)),
		phi * spot * cdfNormal(0,1,phi * d1_) - phi * strike_price * exp(-r * maturity) *cdfNormal(0,1,phi * d2_))
            }
       	/////the option delta
       	delta=iif((volatility == 0 || maturity == 0),iif(spot>strike_price,phi,0),phi * cdfNormal(0,1,phi * d1_))
       	/////the option vega
       	vega=iif((volatility == 0 || maturity == 0),0,spot * pdfNorm(0,1,d1_) * sqrt(maturity))
       	/////the option theta: [Call theta, Put theta]
       	theta= -spot * pdfNorm(0,1,d1_) * volatility \ (2 * sqrt(maturity)) - phi * r * strike_price * b_ * pdfNorm(0,1,d2_)
       	/////the option rho
       	rho=phi * strike_price * maturity * b_ * cdfNormal(0,1,phi * d2_)\ 100
       	
       	/////the option gamma
       	gamma=pdfNorm(0,1,d1_) \ (spot * a_)
       	/////exercice_probability
             exercice_probability =cdfNormal(0,1,d2_)
             //////Delta_money
             Delta_money =delta * spot
             /////Gamma_money 
             Gamma_money = gamma * spot * spot * 0.01
             ////Theta_money
             Theta_money =theta \ 365.
             /////Vega_money
              Vega_money = vega * 0.01
	return [volatility,price,delta,vega,theta,rho,gamma,exercice_probability,Delta_money,Gamma_money,Theta_money,Vega_money]
 }



0 条评论

请先 登录 后评论
mhxiang
mhxiang

4 篇文章

作家榜 »

  1. Junxi 73 文章
  2. wfHuang 6 文章
  3. liang.lin 5 文章
  4. mhxiang 4 文章
  5. admin 3 文章
  6. alex 2 文章
  7. 柏木 1 文章
  8. 丘坤威 1 文章