def scenario_2d_report(value_date, book_name, greeks, \ scens = [['COMFwd', 'SGXIRO', [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0], cmq_inst.CurveShiftType.Abs], \ ['COMVolATM', 'SGXIRO', [-0.02, -0.01, 0.0, 0.01, 0.02], cmq_inst.CurveShiftType.Abs]]): book = cmq_book.get_book_from_db(book_name, status=[2]) base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date) res = {} for greek in greeks: output = {} for shift_x in scens[0][2]: mkt_data_x = cmq_inst_risk.generate_scen(base_mkt, scens[0][0], scens[0][1], 'ALL', \ shift_size=shift_x, shift_type=scens[0][3]) x_key = scens[0][0] + '_' + str(shift_x) output[x_key] = {} for shift_y in scens[1][2]: mkt_data_y = cmq_inst_risk.generate_scen(mkt_data_x, scens[1][0], scens[1][1], 'ALL', \ shift_size = shift_y, shift_type = scens[1][3]) re = cmq_risk_engine.CMQRiskEngine(book, mkt_data_y, [greek]) re.run_risk() y_key = scens[1][0] + '_' + str(shift_y) output[x_key][y_key] = re.book_risks[greek] if greek in ['cmdelta', 'cmvega_atm', 'cmvega_v90', 'cmvega_v75', \ 'cmvega_v25', 'cmvega_v10', 'cmgamma']: output[x_key][y_key] = sum(output[x_key][y_key].values()) res[greek] = pd.DataFrame.from_dict(output, orient='index') return res
def scenario_2d_report(value_date, book, greeks, \ scens = [['COMFwd', 'SGXIRO', [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0], 0], \ ['COMVolATM', 'SGXIRO', [-0.02, -0.01, 0.0, 0.01, 0.02], 0]],\ base_mkt = {}, region = 'EOD'): if len(base_mkt) == 0: base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date, region = region) res = {} for greek in greeks: output = {} for shift_x in scens[0][2]: mkt_data_x = cmq_inst_risk.generate_scen(base_mkt, scens[0][0], scens[0][1], 'ALL', \ shift_size=shift_x, shift_type=scens[0][3]) x_key = scens[0][0] + '_' + str(shift_x) output[x_key] = {} for shift_y in scens[1][2]: mkt_data_y = cmq_inst_risk.generate_scen(mkt_data_x, scens[1][0], scens[1][1], 'ALL', \ shift_size = shift_y, shift_type = scens[1][3]) re = cmq_risk_engine.CMQRiskEngine(book, mkt_data_y, [greek]) re.run_risk() y_key = scens[1][0] + '_' + str(shift_y) output[x_key][y_key] = re.book_risks[greek] if greek in ['cmdelta', 'cmvega_atm', 'cmvega_v90', 'cmvega_v75', \ 'cmvega_v25', 'cmvega_v10', 'cmgamma']: output[x_key][y_key] = sum(output[x_key][y_key].values()) res[greek] = pd.DataFrame.from_dict(output, orient='index') return res
def greeks_ladder_report(value_date, book_name, \ req_greeks = ['cmdeltas', 'cmgammas', 'cmvegas_atm'], \ scens = ['COMFwd', 'SGXIRO', [-5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], cmq_inst.CurveShiftType.Abs]): book = cmq_book.get_book_from_db(book_name, status=[2]) base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date) output = dict([(greek, {}) for greek in req_greeks]) for shift in scens[2]: mkt_data = cmq_inst_risk.generate_scen(base_mkt, scens[0], scens[1], curve_tenor='ALL', shift_size=shift, shift_type=scens[3]) re = cmq_risk_engine.CMQRiskEngine(book, mkt_data, req_greeks) re.run_risk() for greek in req_greeks: for idx in re.book_risks[greek]: if idx not in output[greek]: output[greek][idx] = {} output[greek][idx][shift] = copy.deepcopy( re.book_risks[greek][idx]) res = dict([(greek, {}) for greek in req_greeks]) for greek in req_greeks: for idx in output[greek]: if idx not in res[greek]: res[greek][idx] = {} res[greek][idx] = pd.DataFrame.from_dict(output[greek][idx], orient='columns') return res
def book_greek_scen_report(value_date, book_name, \ req_greeks = ['pv', 'theta', 'cmdelta', 'cmgamma', 'cmvega_atm'], \ scens = ['COMFwd', 'SGXIRO', [- 5.0, -3.0, -1.0, 0.0, 1.0, 3.0, 5.0], cmq_inst.CurveShiftType.Abs]): book = cmq_book.get_book_from_db(book_name, status=[2]) base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date) output = {} for shift in scens[2]: mkt_data = cmq_inst_risk.generate_scen(base_mkt, scens[0], scens[1], curve_tenor='ALL', shift_size=shift, shift_type=scens[3]) re = cmq_risk_engine.CMQRiskEngine(book, mkt_data, req_greeks) re.run_risk() output[shift] = copy.deepcopy(re.book_risks) for greek in req_greeks: if greek in [ 'cmdelta', 'cmvega_atm', 'cmgamma', 'cmvega_v90', 'cmvega_v75', 'cmvega_v25', 'cmvega_v10' ]: output[shift][greek] = sum(output[shift][greek].values()) df = pd.DataFrame.from_dict(output, orient='index').round(2) df.index = numpy.round(scens[2], 2) return df
def book_greek_scen_report(value_date, book, \ req_greeks = ['pv', 'theta', 'cmdelta', 'cmgamma', 'cmvega_atm'], \ scens = ['COMFwd', [- 5.0, -3.0, -1.0, 0.0, 1.0, 3.0, 5.0], 0], \ base_mkt = {}, region = 'EOD', use_pool = False): if len(base_mkt) == 0: base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date, region = region) output = {} if use_pool: num_cpus = mp.cpu_count() - 1 pool = mp.Pool(num_cpus) else: pool = None re = {} fwd_list = book.mkt_deps[scens[0]].keys() for fwd_idx in fwd_list: for shift in scens[1]: mkt_data = cmq_inst_risk.generate_scen(base_mkt, scens[0], fwd_idx, curve_tenor = 'ALL', shift_size = shift, shift_type = scens[2]) key = (fwd_idx, shift) re[key] = cmq_risk_engine.CMQRiskEngine(book, mkt_data, req_greeks, pool) re[key].run_scenarios() for fwd_idx in fwd_list: for shift in scens[1]: key = (fwd_idx, shift) re[key].summerize_risks() output[key] = copy.deepcopy(re[key].book_risks) for greek in req_greeks: if greek in ['cmdelta', 'cmvega_atm', 'cmgamma', 'cmvega_v90', 'cmvega_v75', 'cmvega_v25', 'cmvega_v10']: if fwd_idx in output[key][greek]: output[key][greek] = output[key][greek][fwd_idx] else: output[key][greek] = 0.0 df = pd.DataFrame.from_dict(output, orient='index') return df
def book_greek_scen_report(value_date, book, \ req_greeks = ['pv', 'theta', 'cmdelta', 'cmgamma', 'cmvega_atm'], \ scens = ['COMFwd', 'SGXIRO', [- 5.0, -3.0, -1.0, 0.0, 1.0, 3.0, 5.0], 0], \ base_mkt = {}, use_pool = False): if len(base_mkt) == 0: base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date) output = {} if use_pool: num_cpus = mp.cpu_count() - 1 pool = mp.Pool(num_cpus) else: pool = None re = {} for shift in scens[2]: mkt_data = cmq_inst_risk.generate_scen(base_mkt, scens[0], scens[1], curve_tenor='ALL', shift_size=shift, shift_type=scens[3]) re[shift] = cmq_risk_engine.CMQRiskEngine(book, mkt_data, req_greeks, pool) re[shift].run_scenarios() for shift in scens[2]: re[shift].summerize_risks() output[shift] = copy.deepcopy(re[shift].book_risks) for greek in req_greeks: if greek in [ 'cmdelta', 'cmvega_atm', 'cmgamma', 'cmvega_v90', 'cmvega_v75', 'cmvega_v25', 'cmvega_v10' ]: output[shift][greek] = sum(output[shift][greek].values()) df = pd.DataFrame.from_dict(output, orient='index') return df
def update_risk_store(self): self.scen_keys = [] for inst in self.book.inst_dict: self.risk_store[inst] = cmq_inst_risk.CMQInstRiskStore(inst, self.base_market, self.req_greeks) self.risk_store[inst].get_scen_keys() for scen in self.risk_store[inst].scen_keys: if scen not in self.scen_keys: self.scen_keys.append(scen) self.scenarios[scen] = cmq_inst_risk.generate_scen(self.base_market, \ scen[0], scen[1], scen[2], scen[3])
def update_risk_store(self): self.scen_keys = [] for inst in self.book.inst_dict: self.risk_store[inst] = cmq_inst_risk.CMQInstRiskStore(inst, self.base_market, self.req_greeks, self.book.reporting_ccy) self.risk_store[inst].get_scen_keys() for scen in self.risk_store[inst].scen_keys: if scen not in self.scen_keys: self.scen_keys.append(scen) self.scenarios[scen] = cmq_inst_risk.generate_scen(self.base_market, \ scen[0], scen[1], scen[2], scen[3])
def greek_ladder_report(value_date, book, \ req_greeks = ['cmdeltas', 'cmgammas', 'cmvegas_atm'], \ scens = ['COMFwd', [-0.05, -0.03, -0.02, -0.01, 0.0, 0.01, 0.02, 0.03, 0.05], 1],\ base_mkt = {}, region = 'EOD', use_pool = False): if len(base_mkt) == 0: base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date, region=region) output = dict([(greek, {}) for greek in req_greeks]) if use_pool: num_cpus = mp.cpu_count() - 1 pool = mp.Pool(num_cpus) else: pool = None re = {} fwd_list = book.mkt_deps[scens[0]].keys() for fwd_idx in fwd_list: for shift in scens[1]: key = (fwd_idx, shift) mkt_data = cmq_inst_risk.generate_scen(base_mkt, scens[0], fwd_idx, curve_tenor='ALL', shift_size=shift, shift_type=scens[2]) re[key] = cmq_risk_engine.CMQRiskEngine(book, mkt_data, req_greeks, pool) re[key].run_scenarios() for fwd_idx in fwd_list: output[greek][fwd_idx] = {} for shift in scens[1]: key = (fwd_idx, shift) re[key].summerize_risks() if fwd_idx in re[key].book_risks[greek]: for greek in req_greeks: output[greek][fwd_idx][shift] = copy.deepcopy( re[key].book_risks[greek][fwd_idx]) res = dict([(greek, pd.DataFrame()) for greek in req_greeks]) for greek in req_greeks: for fwd_idx in fwd_list: df = pd.DataFrame.from_dict(output[greek][fwd_idx], orient='columns') df.loc['Total', :] = df.sum() df = df.reset_index() df.rename(columns={'index': 'tenor'}, inplace=True) cols = list(df.columns) df['fwd_idx'] = fwd_idx df = df[['fwd_idx'] + cols] res[greek] = res[greek].append(df) res[greek] = res[greek].set_index(['fwd_idx', 'tenor']) return res
def book_greek_scen_report(value_date, book, \ req_greeks = ['pv', 'theta', 'cmdelta', 'cmgamma', 'cmvega_atm'], \ scens = ['COMFwd', [- 5.0, -3.0, -1.0, 0.0, 1.0, 3.0, 5.0], 0], \ base_mkt = {}, region = 'EOD', use_pool = False): if len(base_mkt) == 0: base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date, region=region) output = {} if use_pool: num_cpus = mp.cpu_count() - 1 pool = mp.Pool(num_cpus) else: pool = None re = {} fwd_list = book.mkt_deps[scens[0]].keys() for fwd_idx in fwd_list: for shift in scens[1]: mkt_data = cmq_inst_risk.generate_scen(base_mkt, scens[0], fwd_idx, curve_tenor='ALL', shift_size=shift, shift_type=scens[2]) key = (fwd_idx, shift) re[key] = cmq_risk_engine.CMQRiskEngine(book, mkt_data, req_greeks, pool) re[key].run_scenarios() for fwd_idx in fwd_list: for shift in scens[1]: key = (fwd_idx, shift) re[key].summerize_risks() output[key] = copy.deepcopy(re[key].book_risks) for greek in req_greeks: if greek in [ 'cmdelta', 'cmvega_atm', 'cmgamma', 'cmvega_v90', 'cmvega_v75', 'cmvega_v25', 'cmvega_v10' ]: if fwd_idx in output[key][greek]: output[key][greek] = output[key][greek][fwd_idx] else: output[key][greek] = 0.0 df = pd.DataFrame.from_dict(output, orient='index') return df
def greek_ladder_report(value_date, book, \ req_greeks = ['cmdeltas', 'cmgammas', 'cmvegas_atm'], \ scens = ['COMFwd', [-0.05, -0.03, -0.02, -0.01, 0.0, 0.01, 0.02, 0.03, 0.05], 1],\ base_mkt = {}, region = 'EOD', use_pool = False): if len(base_mkt) == 0: base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date, region = region) output = dict([(greek, {}) for greek in req_greeks]) if use_pool: num_cpus = mp.cpu_count() - 1 pool = mp.Pool(num_cpus) else: pool = None re = {} fwd_list = book.mkt_deps[scens[0]].keys() for fwd_idx in fwd_list: for shift in scens[1]: key = (fwd_idx, shift) mkt_data = cmq_inst_risk.generate_scen(base_mkt, scens[0], fwd_idx, curve_tenor = 'ALL', shift_size = shift, shift_type = scens[2]) re[key] = cmq_risk_engine.CMQRiskEngine(book, mkt_data, req_greeks, pool) re[key].run_scenarios() for fwd_idx in fwd_list: output[greek][fwd_idx] = {} for shift in scens[1]: key = (fwd_idx, shift) re[key].summerize_risks() if fwd_idx in re[key].book_risks[greek]: for greek in req_greeks: output[greek][fwd_idx][shift] = copy.deepcopy(re[key].book_risks[greek][fwd_idx]) res = dict([(greek, pd.DataFrame()) for greek in req_greeks]) for greek in req_greeks: for fwd_idx in fwd_list: df = pd.DataFrame.from_dict(output[greek][fwd_idx], orient='columns') df.loc['Total', :] = df.sum() df = df.reset_index() df.rename(columns = {'index': 'tenor'}, inplace=True) cols = list(df.columns) df['fwd_idx'] = fwd_idx df = df[['fwd_idx'] + cols] res[greek] = res[greek].append(df) res[greek] = res[greek].set_index(['fwd_idx', 'tenor']) return res
def greeks_ladder_report(value_date, book, \ req_greeks = ['cmdeltas', 'cmgammas', 'cmvegas_atm'], \ scens = ['COMFwd', 'SGXIRO', [-5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], 0],\ base_mkt = {}, use_pool = False): if len(base_mkt) == 0: base_mkt = cmq_market_data.load_market_data(book.mkt_deps, value_date=value_date) output = dict([(greek, {}) for greek in req_greeks]) if use_pool: num_cpus = mp.cpu_count() - 1 pool = mp.Pool(num_cpus) else: pool = None re = {} for shift in scens[2]: mkt_data = cmq_inst_risk.generate_scen(base_mkt, scens[0], scens[1], curve_tenor='ALL', shift_size=shift, shift_type=scens[3]) re[shift] = cmq_risk_engine.CMQRiskEngine(book, mkt_data, req_greeks, pool) re[shift].run_scenarios() for shift in scens[2]: re[shift].summerize_risks() for greek in req_greeks: for idx in re[shift].book_risks[greek]: if idx not in output[greek]: output[greek][idx] = {} output[greek][idx][shift] = copy.deepcopy( re[shift].book_risks[greek][idx]) res = dict([(greek, {}) for greek in req_greeks]) for greek in req_greeks: for idx in output[greek]: if idx not in res[greek]: res[greek][idx] = {} res[greek][idx] = pd.DataFrame.from_dict(output[greek][idx], orient='columns') return res