print('maturity date : ', maturitydt) # maturitydt = to_ql_date(maturity_date) #print(maturitydt) # barrier = 2.8 barrier = 2.6 strike = 2.8 # optionType = ql.Option.Call # barrierType = ql.Barrier.UpOut optionType = ql.Option.Put barrierType = ql.Barrier.DownOut contractType = '50etf' engineType = 'BinomialBarrierEngine' barrier_type = 'downout' # barrier_type = 'upin' evaluation = Evaluation(evalDate, daycounter, calendar) spot, black_var_surface, const_vol = get_vol_data(evalDate, daycounter, calendar, contractType) # 收盘价 optionBarrierEuropean = OptionBarrierEuropean(strike, maturitydt, optionType, barrier, barrierType) barrier_option = optionBarrierEuropean.option_ql option_call = OptionPlainEuropean(strike, maturitydt, ql.Option.Call) plain_option = option_call.option_ql underlying = ql.SimpleQuote(S0) spot_range = np.arange( min(strike, barrier) - 0.1, max(strike, barrier) + 0.05, 0.0005) # barrier_prices = [] # plain_prices = []
maturitydt = calendar.advance(begin_date,ql.Period(3,ql.Months)) # Evaluation Settings begDate = begin_date svidata = svi_dataset.get(to_dt_date(begin_date)) strike = svidata.spot barrier = strike*(1+pct) endDate = calendar.advance(maturitydt,ql.Period(-1,ql.Days)) ################Barrier Option Info##################### #print('barrier/strike',barrier/strike) optionBarrierEuropean = OptionBarrierEuropean(strike,maturitydt,optionType,barrier,barrierType) barrier_option = optionBarrierEuropean.option_ql hist_spots = [] ######################################################### # construct vol surface on last day's close prices. evaluation = Evaluation(begDate, daycounter, calendar) daily_close, black_var_surface, const_vol = get_vol_data(begDate,daycounter,calendar,contractType) #print('init spot : ',daily_close) underlying = ql.SimpleQuote(daily_close) process_svi_h = evaluation.get_bsmprocess(daycounter, underlying, black_var_surface) process_bs_h = evaluation.get_bsmprocess_cnstvol(daycounter, calendar, underlying, const_vol) price_bs, delta_bs = exotic_util.calculate_barrier_price(evaluation, optionBarrierEuropean, hist_spots, process_bs_h, engineType) try: price_svi, delta_svi = exotic_util.calculate_barrier_price(evaluation, optionBarrierEuropean, hist_spots, process_svi_h, engineType) except: continue init_svi = price_svi init_bs = price_bs
with open( os.path.abspath('..') + '/intermediate_data/total_hedging_daily_svi_dataset_puts.pickle', 'rb') as f: daily_svi_dataset = pickle.load(f)[0] with open( os.path.abspath('..') + '/intermediate_data/total_hedging_daily_params_puts.pickle', 'rb') as f: daily_params_p = pickle.load(f)[0] date = datetime.date(2017, 7, 17) date_ql = util.to_ql_date(date) daycounter = ql.ActualActual() calendar = ql.China() evaluation = Evaluation(date_ql, daycounter, calendar) paramset_c = daily_params_c.get(date) paramset_p = daily_params_p.get(date) dataset = daily_svi_dataset.get(date) cal_vols, put_vols, maturity_dates, underlying, rfs = dataset index = 3 mdate = maturity_dates[index] maturitydt = util.to_ql_date(mdate) print(maturitydt) params_c = paramset_c[index] params_p = paramset_p[index] rf = util.get_rf_tbcurve(date_ql, daycounter, maturitydt) ttm = daycounter.yearFraction(date_ql, maturitydt) discount = math.exp(-rf * ttm)
"%15s %15s %15s %15s %15s %15s %15s %15s %15s %15s %15s" % ("eval date", "spot", "svi vol", "bs vol", "delta svi", "delta bs", 'price_svi', 'price_bs', 'portfolio_svi', 'portfolio_bs', 'transaction')) print('-' * 200) hist_spots = [] barriers = [] dates = [] rebalancings = [] pnls_svi = [] pnls_bs = [] results = {} ####################################################################################################### # Construct initial rebalancing portfolio begDate = begin_date evaluation = Evaluation(begDate, daycounter, calendar) daily_close, black_var_surface, const_vol = get_vol_data( begDate, daycounter, calendar, contractType) ttm = daycounter.yearFraction(begDate, maturitydt) price_svi, price_bs, delta_svi, delta_bs = 0.0, 0.0, 0.0, 0.0 try: price_svi, delta_svi, price_bs, delta_bs, svi_vol = exotic_util.calculate_matrics( evaluation, daycounter, calendar, optionBarrierEuropean, hist_spots, daily_close, black_var_surface, const_vol, engineType, ttm) except Exception as e: print(e) print('initial price unavailable') # init_svi = price_svi # init_bs = price_bs init_svi = init_bs = max(price_svi, price_bs)
evalDate = ql.Date(14, 7, 2017) calendar = ql.China() daycounter = ql.ActualActual() contractType = '50etf' engineType = 'AnalyticEuropeanEngine' facevalue = 10000 # i = 3 rf = 0.03 optionType = ql.Option.Call barrierType = ql.Barrier.DownOut evalDate = calendar.advance(evalDate, ql.Period(1, ql.Days)) evaluation = Evaluation(evalDate, daycounter, calendar) maturitydt = ql.Date(30, 9, 2017) endDate = ql.Date(25, 9, 2017) # maturitydt = calendar.advance(evalDate,ql.Period(3,ql.Months)) 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()