close = mktData[mktFlds.index('close')][mktindex] open = mktData[mktFlds.index('open')][mktindex] amount = mktData[mktFlds.index('amount')][mktindex] ttm = daycounter.yearFraction(evalDate, maturitydt) try: rf = curve.zeroRate(maturitydt, daycounter, ql.Continuous).rate() except: rf = 0.0 Ft = spot * math.exp(rf * ttm) moneyness = math.log(strike / Ft, math.e) optiontype = ql.Option.Call implied_vol, error = calculate_vol_BS(maturitydt, optiontype, strike, spot, dividend_ts, yield_ts, close, evalDate, calendar, daycounter, precision=0.05, maxVol=0.5, step=0.0001) totalvariance = (implied_vol**2) * ttm svi_dataset.update_data(mdate, strike, moneyness, implied_vol, ttm, totalvariance, close, open, amount) svi_dataset_ts.append(svi_dataset) print(svi_dataset_ts)
def get_call_put_impliedVols_tbcurve( evalDate, daycounter, calendar, maxVol=1.0, step=0.0001, precision=0.05, show=True): call_volatilities_0 = {} call_volatilities_1 = {} call_volatilities_2 = {} call_volatilities_3 = {} put_volatilites_0 = {} put_volatilites_1 = {} put_volatilites_2 = {} put_volatilites_3 = {} e_date0, e_date1, e_date2, e_date3 = 0, 0, 0, 0 try: curve = get_curve_treasury_bond(evalDate, daycounter) vols, spot, mktData, mktFlds, optionData, optionFlds, optionids = get_wind_data(evalDate) ql.Settings.instance().evaluationDate = evalDate yield_ts = ql.YieldTermStructureHandle(curve) dividend_ts = ql.YieldTermStructureHandle(ql.FlatForward(evalDate, 0.0, daycounter)) month_indexs = get_contract_months(evalDate) for idx, optionid in enumerate(optionids): optionDataIdx = optionData[optionFlds.index('wind_code')].index(optionid) mdate = pd.to_datetime(optionData[optionFlds.index('exercise_date')][optionDataIdx]) maturitydt = ql.Date(mdate.day, mdate.month, mdate.year) mktindex = mktData[mktFlds.index('option_code')].index(optionid) strike = optionData[optionFlds.index('exercise_price')][optionDataIdx] close = mktData[mktFlds.index('close')][mktindex] ttm = daycounter.yearFraction(evalDate, maturitydt) rf = curve.zeroRate(maturitydt, daycounter, ql.Continuous).rate() nbr_month = maturitydt.month() if nbr_month == month_indexs[0]: e_date0 = maturitydt Ft = spot * math.exp(rf * ttm) moneyness = math.log(strike / Ft, math.e) if optionData[optionFlds.index('call_or_put')][optionDataIdx] == '认购': optiontype = ql.Option.Call implied_vol, error = calculate_vol_BS(maturitydt, optiontype, strike, spot, dividend_ts, yield_ts, close, evalDate, calendar, daycounter, precision, maxVol, step) call_volatilities_0.update({moneyness: [implied_vol, strike, close]}) else: optiontype = ql.Option.Put implied_vol, error = calculate_vol_BS(maturitydt, optiontype, strike, spot, dividend_ts, yield_ts, close, evalDate, calendar, daycounter, precision, maxVol, step) put_volatilites_0.update({moneyness: [implied_vol, strike, close]}) elif nbr_month == month_indexs[1]: e_date1 = maturitydt Ft = spot * math.exp(rf * ttm) moneyness = math.log(strike / Ft, math.e) if optionData[optionFlds.index('call_or_put')][optionDataIdx] == '认购': optiontype = ql.Option.Call implied_vol, error = calculate_vol_BS(maturitydt, optiontype, strike, spot, dividend_ts, yield_ts, close, evalDate, calendar, daycounter, precision, maxVol, step) call_volatilities_1.update({moneyness: [implied_vol, strike, close]}) else: optiontype = ql.Option.Put implied_vol, error = calculate_vol_BS(maturitydt, optiontype, strike, spot, dividend_ts, yield_ts, close, evalDate, calendar, daycounter, precision, maxVol, step) put_volatilites_1.update({moneyness: [implied_vol, strike, close]}) elif nbr_month == month_indexs[2]: e_date2 = maturitydt Ft = spot * math.exp(rf * ttm) moneyness = math.log(strike / Ft, math.e) if optionData[optionFlds.index('call_or_put')][optionDataIdx] == '认购': optiontype = ql.Option.Call implied_vol, error = calculate_vol_BS(maturitydt, optiontype, strike, spot, dividend_ts, yield_ts, close, evalDate, calendar, daycounter, precision, maxVol, step) call_volatilities_2.update({moneyness: [implied_vol, strike, close]}) else: optiontype = ql.Option.Put implied_vol, error = calculate_vol_BS(maturitydt, optiontype, strike, spot, dividend_ts, yield_ts, close, evalDate, calendar, daycounter, precision, maxVol, step) put_volatilites_2.update({moneyness: [implied_vol, strike, close]}) else: e_date3 = maturitydt Ft = spot * math.exp(rf * ttm) moneyness = math.log(strike / Ft, math.e) if optionData[optionFlds.index('call_or_put')][optionDataIdx] == '认购': optiontype = ql.Option.Call implied_vol, error = calculate_vol_BS(maturitydt, optiontype, strike, spot, dividend_ts, yield_ts, close, evalDate, calendar, daycounter, precision, maxVol, step) call_volatilities_3.update({moneyness: [implied_vol, strike, close]}) else: optiontype = ql.Option.Put implied_vol, error = calculate_vol_BS(maturitydt, optiontype, strike, spot, dividend_ts, yield_ts, close, evalDate, calendar, daycounter, precision, maxVol, step) put_volatilites_3.update({moneyness: [implied_vol, strike, close]}) expiration_dates = [e_date0, e_date1, e_date2, e_date3] cal_vols = [call_volatilities_0, call_volatilities_1, call_volatilities_2, call_volatilities_3] put_vols = [put_volatilites_0, put_volatilites_1, put_volatilites_2, put_volatilites_3] except Exception as e: print('Error -- get_call_put_impliedVols failed') print(e) return return cal_vols, put_vols, expiration_dates, spot, curve