以50ETF为例,只要输入标的交易日期及定义历史范围(30、60或90天),即可通过CalOBJ.GetVolatility(历史价格数组)方便地取得标的历史波动率。
完整计算波动率及策略代码可参考 https://github.com/qmhedging/poboquant/blob/master/IH%E4%B8%8E50ETF%E5%AF%B9%E5%86%B2-%E6%94%B9%E8%BF%9B%E5%8E%86%E5%8F%B2%E6%B3%A2%E5%8A%A8%E7%8E%87%E8%AE%A1%E7%AE%97%20IH-50ETF%20Opt%20hedge%20HV%20cal%20fix
#计算历史波动率代码,注意一些变量在其他地方已经定义,可参考完整代码
CalOBJ = CreateCalcObj() #创建一个计算对象
option = PBObj()
yearstring=int(tradingtime.year) #tradingtime 是用户取得的标的资产交易日期
monthstring=int(tradingtime.month)
daystring=int(tradingtime.day)
#准备计算50ETF的历史波动率
option.EndDate=datetime.datetime(yearstring,monthstring,daystring)
option.Count = 31 #获取过去31条K线
klinedata0 = GetHisData(g.code0, BarType.Day, option)
#print "len(klinedata) ",len(klinedata)
klist = []
i=0
while i<len(klinedata0)-1:
#print "previous trading day" +str(klinedata0.tradedate)
#print "previous prices "+str(klinedata0.close)
klist.append(klinedata0.close) #
i+=1
if len(klist)>0:
Kl = np.array(klist, dtype=np.double)
HisVola=CalOBJ.GetVolatility(Kl) #得到历史波动率
print "30D HisVola "+str(HisVola)
更多策略范例代码 可以参考 https://github.com/qmhedging/poboquant
云量化平台 可访问
https://quant.pobo.net.cn/quant/login#/