def get_NPVs(self, leg, value_date, instrument, s_date): """ Disc_cv_details is the specification of which discounting curve to use when calculating NPV as well as risk analysis disc_cv_details = { "type":..., "spread":...,} """ """ Step one cal of legx leg1 = { "currency":..., "balance_tb":..., "acc_cpn_detail":..., "pay_convention":...., "day_convention":....,} """ legx = instrument[leg] Day_Counter = Day_Count.Day_Counter("ACT/360") currency = legx["currency"] fx_instrument = self.cv_fx_instrument[currency] """ Discounting Curve settings below """ cv_dis = self.gen_swap_curve(value_date, currency, fx_instrument, Day_Counter) sdate_df = self.get_sdate_df(cv_dis, s_date) Day_Counter.set_convention(legx["day_convention"]) PRI_flow = legx["balance_tb"] NPV_PRI = Tools.cal_npv(PRI_flow, cv_dis, Day_Counter) return NPV_PRI / sdate_df
def get_NPVs(self, leg, curve_date, instrument, value_date=""): """ Disc_cv_details is the specification of which discounting curve to use when calculating NPV as well as risk analysis disc_cv_details = { "type":..., "spread":...,} """ """ Step one cal of legx leg1 = { "currency":..., "balance_tb":..., "acc_cpn_detail":..., "pay_convention":...., "day_convention":....,} """ if value_date == "": value_date = curve_date legx = instrument[leg] Day_Counter = Day_Count.Day_Counter(legx["day_convention"]) currency = legx["currency"] #convention = self.convention[currency] cv_instrument = self.curve_instrument[currency] fx_instrument = self.cv_fx_instrument[currency] """ Discounting Curve settings below """ if instrument["type"].upper() == "XCS": """ For XCS calculation we have to use dual curves method libor curve for coupon calculation and basis adjusted curve for discounting """ cv_dis = self.gen_swap_curve(curve_date, currency, fx_instrument, Day_Counter) instrument["type"] = "SWAP" cv_fwd = self.gen_swap_curve(curve_date, currency, cv_instrument, Day_Counter) instrument["type"] = "XCS" else: Day_Counter.set_convention_by_ccy(currency) cv_fwd = self.gen_swap_curve(curve_date, currency, cv_instrument, Day_Counter) cv_dis = cv_fwd Day_Counter.set_convention(legx["day_convention"]) cf_tb = CF_Gen(legx, cv_fwd, self.cv_keeper, Day_Counter) df0 = self.get_df0(cv_dis, value_date) INT_flow = [[ele["End_Time"], ele["Interests"]] for ele in cf_tb] NPV_INT = Tools.cal_npv(INT_flow, cv_dis, Day_Counter) PRI_flow = [[ele["End_Time"], ele["Principal"]] for ele in cf_tb] NPV_PRI = Tools.cal_npv(PRI_flow, cv_dis, Day_Counter) return NPV_INT / df0, NPV_PRI / df0
def __init__(self, curve_instrument, Val_Surface): """ Initial settings for OPTer opt_instrument will contain all information needed to do pricing as well as risk analysis opt_instrument = { "name":...., "type":...., "style":... } style = {"Euro","Amer","Berm"...} type = {"cap","floor","swaption","stock","forward","fx","irs"} opt_direction = {"Put","Call"} """ self.Day_Counter = counter.Day_Counter("ACT/360") self.val_surface = Val_Surface self.curve_instrument = curve_instrument self.answer_bk = OrderedDict() self.instruments = OrderedDict()
def cal_opt_rate(self, val_date, instr): # Calculate option value with vol surface Day_Counter = counter.Day_Counter("ACT/360") #Day_Counter.set_convention_by_ccy(instr["currency"]) curve = self.gen_curve(val_date, instr["currency"], self.curve_instrument, Day_Counter) # Curve smoothing curve = bt_tool.augument_by_frequency(curve, 1) if instr["style"].upper() in ("CAP", "FLOOR"): # Cap/Floor case val_tb = opt_t.cal_cf_val(instr, curve, self.val_surface, Day_Counter) elif instr["style"].upper() in ("CALL", "PUT"): val_tb = opt_t.cal_cp_val(instr, curve, self.val_surface, Day_Counter) elif instr["style"].upper() in ("SWAP PAY", "SWAP REC"): val_tb = opt_t.cal_swap_val(instr, curve, self.val_surface, Day_Counter) fv_li = [[ele["End_Time"], ele["PMT"]] for ele in val_tb] delta_li = [[ele["End_Time"], ele["delta"]] for ele in val_tb] gama_li = [[ele["End_Time"], ele["gama"]] for ele in val_tb] vega_li = [[ele["End_Time"], ele["vega"]] for ele in val_tb] rho_li = [[ele["End_Time"], ele["rho"]] for ele in val_tb] theta_li = [[ele["End_Time"], ele["theta"]] for ele in val_tb] NPV = opt_t.cal_npv(fv_li, curve) delta = opt_t.cal_npv(delta_li, curve) gama = opt_t.cal_npv(gama_li, curve) vega = opt_t.cal_npv(vega_li, curve) rho = opt_t.cal_npv(rho_li, curve) theta = opt_t.cal_npv(theta_li, curve) rate = opt_t.Cal_OPT_Rate(val_tb, curve, NPV, Day_Counter) return { "NPV": NPV, "Rate": rate, "delta": delta, "vega": vega, "rho": rho, "gamma": gama, "theta": theta }, instr["schedule"]
def get_raw_dfs(self, value_date, currency, freq, isEOM, Margin): """ Add on function to generate both libor discount factors and fx (basis adjusted) disocunt factors """ convention = self.convention[currency] Day_Counter = Day_Count.Day_Counter(convention["LIBOR_day_count"]) cv_instrument = self.curve_instrument[currency] fx_instrument = self.cv_fx_instrument[currency] if currency.upper() != "BRL": cv_dis = self.gen_swap_curve(value_date, currency, cv_instrument, Day_Counter) fx_dis = self.gen_swap_curve(value_date, currency, fx_instrument, Day_Counter) else: cv_dis = self.gen_zero_curve(value_date, currency, cv_instrument, Day_Counter) fx_dis = self.gen_zero_curve(value_date, currency, fx_instrument, Day_Counter) cv_dis = Tools.augument_by_frequency(cv_dis, freq, isEOM) fx_dis = Tools.augument_by_frequency(fx_dis, freq, isEOM) return cv_dis, fx_dis
import datetime as dt import matplotlib.pyplot as plt ################################### ####--- Below is for testing ---### sdate = "2019-02-14" output_name = "Curve_" + sdate + ".xlsx" col_num, row_num = 0, 0 currency = ["EUR"] spread = 0 # In % units writer = pd.ExcelWriter(output_name) CK = BS_CK.Curve_Keeper(sdate,"Yield_Curve") Day_Counter = Day_Count.Day_Counter("30/360") for ccy in currency: cvs = CK.gen_curve( ccy, -1, 0, spread, Day_Counter ) cv = cvs["LIBOR"] cv = Tools.augument_by_frequency( cv , 1) df = pd.DataFrame(cv) df.columns = ["Date", "LIBOR:"+ccy+" Spread:"+str(spread)] df.to_excel( writer, startrow = row_num, startcol = col_num, sheet_name = "Curves" ) col_num += 3