简单粗暴的方法,就用poboquant平台计算,参数改起来也方便,更好的是可以方便地进行期权策略回测,帮助你找到合适的greeks计算参数,节约了更多coding工作量。
范例代码qmhedging/poboquant
比如计算历史波动率
option.EndDate=datetime.datetime(yearstring,monthstring,daystring)
option.Count = 31 #获取过去31条K线 你自己可以定义是交易日还是自然日以及取多少天
klinedata0 = GetHisData(g.code0, BarType.Day, option)
其他参数,比如一年是365天还是交易日总数,以及无风险利率等参数都是可以自己调整的,你可以充分定制计算参数
计算greeks的范例可参考
#Get BS Price 这里开始计算BS 理论价格,可以用于和期权市场价格对比
BSPrice=CalOBJ.GetOptionBSPrice(OptDirection,AssetType,AssetPrice,StrikePrice,HisVola,InterestRate,ExpireinYear)
#GetOptionBSPrice(direct, type, stockprice, strikeprice, volatility, r, t)
print "BS Theory Price is "+ str(BSPrice) +" vs option market price "+str(dyndata1)
#Cal Opt Greeks #这里开始计算希腊值 Delta
#GetOptionDelta(direct,type,stockprice,strikeprice,volatility,r,t)
OptDelta=CalOBJ.GetOptionDelta(OptDirection,AssetType,AssetPrice,StrikePrice,HisVola,InterestRate,ExpireinYear)
print "Option Delta is "+ str(OptDelta)
#Theta
OptTheta=CalOBJ.GetOptionTheta(OptDirection,AssetType,AssetPrice,StrikePrice,HisVola,InterestRate,ExpireinYear)
print "Option Theta is "+ str(OptTheta)
#Gamma
OptGamma=CalOBJ.GetOptionGamma(OptDirection,AssetType,AssetPrice,StrikePrice,HisVola,InterestRate,ExpireinYear)
print "Option Gamma is "+ str(OptGamma)
#Vega
OptVega=CalOBJ.GetOptionVega(OptDirection,AssetType,AssetPrice,StrikePrice,HisVola,InterestRate,ExpireinYear)
print "Option Vega is "+str(OptVega)
#Rho
OptRho=CalOBJ.GetOptionRho(OptDirection,AssetType,AssetPrice,StrikePrice,HisVola,InterestRate,ExpireinYear)
print "Option Rho is "+str(OptRho)
当然一些变量及计算参数需要事先定义
完整代码可参考 https://github.com/qmhedging/pob ... %20and%20BS%20Price
更多范例代码可见 https://github.com/qmhedging/poboquant
Poboquant注册网址 https://quant.pobo.net.cn/quant/login#/
邀请码 814685
|