def get_black_var_surface_50etf2(self, maturity_dates, spot, dS=0.0, strikes=np.arange(1.0, 5.0, 0.1 / 100)): volset = [] for idx_mdt, mdt in enumerate(maturity_dates): params = self.calibrated_params[idx_mdt] a_star, b_star, rho_star, m_star, sigma_star = params ttm = self.daycounter.yearFraction(self.evalDate, mdt) rf = util.get_rf_tbcurve(self.evalDate, self.daycounter, mdt) Ft = (spot + dS) * math.exp(rf * ttm) x_svi = np.log(strikes / Ft) vol = np.sqrt( np.maximum( 0, a_star + b_star * (rho_star * (x_svi - m_star) + np.sqrt( (x_svi - m_star)**2 + sigma_star**2)))) volset.append(vol) implied_vols = ql.Matrix(len(strikes), len(maturity_dates)) for i in range(implied_vols.rows()): for j in range(implied_vols.columns()): implied_vols[i][j] = volset[j][i] black_var_surface = ql.BlackVarianceSurface(self.evalDate, self.calendar, maturity_dates, strikes, implied_vols, self.daycounter) return black_var_surface
def get_black_var_surface_50etf(self, spot, dS=0.0, strikes=np.arange(1.0, 5.0, 0.1 / 100)): volset = [] maturity_dates = self.calibrated_params.keys() #print(maturity_dates) maturity_dates = sorted(maturity_dates) maturity_dates = maturity_dates[1:] #print(maturity_dates) for mdt in maturity_dates: params = self.calibrated_params.get(mdt) a_star, b_star, rho_star, m_star, sigma_star = params maturitydt = ql.Date(mdt.day, mdt.month, mdt.year) ttm = self.daycounter.yearFraction(self.evalDate, maturitydt) rf = util.get_rf_tbcurve(self.evalDate, self.daycounter, maturitydt) Ft = (spot + dS) * math.exp(rf * ttm) x_svi = np.log(strikes / Ft) vol = np.sqrt( np.maximum( 0, a_star + b_star * (rho_star * (x_svi - m_star) + np.sqrt( (x_svi - m_star)**2 + sigma_star**2)))) volset.append(vol) implied_vols = ql.Matrix(len(strikes), len(maturity_dates)) for i in range(implied_vols.rows()): for j in range(implied_vols.columns()): implied_vols[i][j] = volset[j][i] black_var_surface = ql.BlackVarianceSurface( self.evalDate, self.calendar, util.to_ql_dates(maturity_dates), strikes, implied_vols, self.daycounter) return black_var_surface
def get_black_variance_surface_cmd(calibrated_params,calibrate_date,daycounter,calendar,underlying_prices,contractType,strikes): #strikes = np.arange(2400, 3200, 1) volset = [] maturity_dates = [] contract_ids = sorted(calibrated_params.keys()) for contractId in contract_ids: params = calibrated_params.get(contractId) a_star, b_star, rho_star, m_star, sigma_star = params mdt = util.get_mdate_by_contractid(contractType,contractId,calendar) maturity_dates.append(mdt) ttm = daycounter.yearFraction(calibrate_date,mdt) rf = util.get_rf_tbcurve(calibrate_date,daycounter,mdt) spot = underlying_prices.get(contractId) Ft = spot * math.exp(rf * ttm) x_svi = np.log(strikes/Ft) vol = np.sqrt(np.maximum(0,a_star + b_star * (rho_star * (x_svi - m_star) + np.sqrt((x_svi - m_star) ** 2 + sigma_star ** 2)))) volset.append(vol) implied_vols = ql.Matrix(len(strikes), len(maturity_dates)) for i in range(implied_vols.rows()): for j in range(implied_vols.columns()): implied_vols[i][j] = volset[j][i] #maturity_dates = [maturity_dates[1],maturity_dates[0]] #print(type(strikes)) #print(type(implied_vols)) #print(strikes) #print(implied_vols) black_var_surface = ql.BlackVarianceSurface(calibrate_date, calendar,maturity_dates, strikes,implied_vols, daycounter) return black_var_surface
def knock_out_call(self, portfolio, instrument_ratio, evaluation, spot, vol_ts, call_strikes=[], put_strikes=[]): barrier = self.exoticOption.barrier strike = self.exoticOption.strike maturitydt = self.exoticOption.maturitydt evalDate = evaluation.evalDate daycounter = evaluation.daycounter rf = util.get_rf_tbcurve(evalDate, daycounter, maturitydt) ttm = daycounter.yearFraction(evalDate, maturitydt) underlying = ql.SimpleQuote(spot) process = evaluation.get_bsmprocess(daycounter, underlying, vol_ts) european_engine = ql.AnalyticEuropeanEngine(process) exercise = ql.EuropeanExercise(maturitydt) if len(call_strikes) == 0: k_call = strike else: k_call = util.get_closest_strike(call_strikes, strike) payoff = ql.PlainVanillaPayoff(ql.Option.Call, k_call) call = ql.EuropeanOption(payoff, exercise) call.setPricingEngine(european_engine) instrument_ratio.update({'call-' + str(k_call): 1.0}) portfolio.add(call) b_forward = ((barrier * np.exp(rf * ttm))**2) / k_call if len(put_strikes) == 0: k_put = b_forward else: k_put = util.get_closest_strike(put_strikes, b_forward) put_payoff = ql.PlainVanillaPayoff(ql.Option.Put, k_put) put = ql.EuropeanOption(put_payoff, exercise) put.setPricingEngine(european_engine) underlying.setValue(barrier) callprice_at_barrier = call.NPV() putprice_at_barrier = put.NPV() put_ratio = callprice_at_barrier / putprice_at_barrier portfolio.subtract(put, put_ratio) instrument_ratio.update({'put-' + str(k_put): -put_ratio}) return portfolio, instrument_ratio
'rb') as f: daily_params_p = pickle.load(f)[0] date = datetime.date(2017, 7, 17) date_ql = util.to_ql_date(date) 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, s, rfs = dataset index = 0 mdate = maturity_dates[index] maturitydt = util.to_ql_date(mdate) 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) # Example strike = 2.4 dS = 0.001 iscall = True curve = get_curve_treasury_bond(date_ql, daycounter) yield_ts = util.get_yield_ts(date_ql, curve, maturitydt, daycounter) dividend_ts = util.get_dividend_ts(date_ql, daycounter) print('strike = ', strike, ', option type : call') print('=' * 100) print("%10s %25s %25s %25s %25s" % ("Spot", "delta_total", "delta_eff", "delta_constant_vol ", "diff"))