def get_vol_data(evalDate,daycounter,calendar,contractType): svidata = svi_dataset.get(to_dt_date(evalDate)) paramset = calibrered_params_ts.get(to_dt_date(evalDate)) volSurface = SviVolSurface(evalDate, paramset, daycounter, calendar) spot = svidata.spot maturity_dates = sorted(svidata.dataSet.keys()) svi = SviPricingModel(volSurface, spot, daycounter, calendar, to_ql_dates(maturity_dates), ql.Option.Call, contractType) black_var_surface = svi.black_var_surface() const_vol = estimated_vols.get(to_dt_date(evalDate)) return spot, black_var_surface, const_vol
def get_vol_data(evalDate,daycounter,calendar,contractType): svidata = svi_dataset.get(to_dt_date(evalDate)) paramset = calibrered_params_ts.get(to_dt_date(evalDate)) volSurface = SviVolSurface(evalDate, paramset, daycounter, calendar) underlyings = {} dataset = svidata.dataSet for mdate in svidata.dataSet.keys(): underlyings.update({mdate:dataset.get(mdate).spot}) maturity_dates = sorted(svidata.dataSet.keys()) svi = SviPricingModel(volSurface, underlyings, daycounter, calendar, to_ql_dates(maturity_dates), ql.Option.Call, contractType) black_var_surface = svi.black_var_surface() const_vol = estimated_vols.get(to_dt_date(evalDate)) return black_var_surface, const_vol
def get_vol_data_MA(evalDate, daycounter, calendar, contractType): black_var_surfaces = [] startdt = calendar.advance(evalDate, ql.Period(-5, ql.Days)) while startdt < dt: svidata = svi_dataset.get(to_dt_date(evalDate)) paramset = calibrered_params_ts.get(to_dt_date(evalDate)) volSurface = SviVolSurface(evalDate, paramset, daycounter, calendar) spot = svidata.spot maturity_dates = sorted(svidata.dataSet.keys()) svi = SviPricingModel(volSurface, spot, daycounter, calendar, to_ql_dates(maturity_dates), ql.Option.Call, contractType) black_var_surface = svi.black_var_surface() black_var_surfaces.append(black_var_surface) const_vol = estimated_vols.get(to_dt_date(evalDate)) spot = svi_dataset.get(to_dt_date(evalDate)).spot return spot, black_var_surfaces, const_vol
#plt.figure() #plt.plot(logMoneynesses, vol, 'ro') #plt.plot(x_svi, vol_svi, 'b--') #plt.title('vol, ' + str(evalDate) + ', ' + str(mdate)) #plt.figure() #plt.plot(logMoneynesses, totalvariance, 'ro') #plt.plot(x_svi, tv_svi2, 'b--') #plt.title('tv, ' + str(evalDate) + ', ' + str(mdate)) #plt.show() #print(calibrered_params) maturity_dates = sorted(calibrered_params.keys()) #print(maturity_dates) calibrered_params_ts.update({evalDate: calibrered_params}) volSurface = SviVolSurface(evalDate, calibrered_params, daycounter, calendar) svi = SviPricingModel(volSurface, underlyings, daycounter, calendar, to_ql_dates(maturity_dates), ql.Option.Put, 'm') black_var_surface = svi.black_var_surface() #local_vol_surface = ql.LocalVolSurface(ql.BlackVolTermStructureHandle(black_var_surface), # yield_ts,dividend_ts,spot) dt = black_var_surface.maxDate() t = daycounter.yearFraction(evalDate, dt) # Plot print('plot') plt.rcParams['font.sans-serif'] = ['STKaiti'] plt.rcParams.update({'font.size': 13}) plot_years = np.arange(0.05, t - 0.05, 0.01) plot_strikes = np.arange(float(min_k), float(max_k), 10.0) fig = plt.figure()
engineType = 'AnalyticEuropeanEngine' print('strike = ', strike, ', option type : call') print('=' * 100) print("%10s %25s %25s %25s %25s %25s" % ("Spot", "barrier_delta", "european_delta", "delta_total", "delta_eff", "diff")) print('-' * 100) barrier = 2.1 #barrier = 2.6 option_call = OptionPlainEuropean(strike, maturitydt, ql.Option.Call) optionql_call = option_call.option_ql barrier_option = OptionBarrierEuropean(strike, maturitydt, ql.Option.Call, barrier, ql.Barrier.DownOut) barrierql = barrier_option.option_ql volSurface_call = SviVolSurface(date_ql, paramset_c, daycounter, calendar) svi_call = SviPricingModel(volSurface_call, underlying, daycounter, calendar, util.to_ql_dates(maturity_dates), ql.Option.Call, contractType) vol_surface_call = svi_call.black_var_surface() call_delta_total = [] call_delta_cnst = [] call_delta_eff = [] call_diff = [] call_barrierdelta = [] #index=['data_total','data_const'] result = pd.DataFrame() underlying_ql = ql.SimpleQuote(0.0) process = evaluation.get_bsmprocess(daycounter, underlying_ql, vol_surface_call)
portfolio_values = [] pnls_value = [] pnls_price = [] callprices = [] putprices = [] spots = [] while evalDate < endDate: try: evaluation = Evaluation(evalDate, daycounter, calendar) if daily_close <= barrier: break put_price = put_data[put_230.Times.index(to_dt_date(evalDate))] call_price = call_data[call_265.Times.index(to_dt_date(evalDate))] portfolio_price = call_price - put_ratio * put_price svidata = svi_dataset.get(to_dt_date(evalDate)) paramset = calibrered_params_ts.get(to_dt_date(evalDate)) volSurface = SviVolSurface(evalDate, paramset, daycounter, calendar) daily_close = svidata.spot maturity_dates = sorted(svidata.dataSet.keys()) svi = SviPricingModel(volSurface, daily_close, daycounter, calendar, to_ql_dates(maturity_dates), ql.Option.Call, contractType) black_var_surface = svi.black_var_surface() # const_vol = estimated_vols.get(to_dt_date(evalDate)) # print(black_var_surface.blackVol(ttm,daily_close)) underlying = ql.SimpleQuote(daily_close) # process = evaluation.get_bsmprocess(daycounter, underlying, black_var_surface) # vol = estimated_vols.get(to_dt_date(evalDate)) ttm = daycounter.yearFraction(evalDate, maturitydt)