diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/position.py | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/python/position.py b/python/position.py index 79b10559..10931d0a 100644 --- a/python/position.py +++ b/python/position.py @@ -121,18 +121,18 @@ def init_swap_rates(conn, session, tenors=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20 def init_swaption_vol(session, tenors=['A', 'C', 'F', 'I'] + list(range(1, 11)) + [15, 20, 25, 30], - source='BBIR'): + source='BBIR', + vol_type='N'): tickers = [] for t1 in tenors: for t2 in tenors[4:]: - tickers.append(f"USSN{t1:0>2}{t2} {source} Curncy") - import pdb;pdb.set_trace() + tickers.append(f"USS{vol_type}{t1:0>2}{t2} {source} Curncy") data = retrieve_data(session, tickers, ['PX_LAST'], - start_date=datetime.date(1998, 10, 7)) + start_date=datetime.date(1990, 1, 1)) return data -def split_tenor_expiry(ticker): - m = re.match("USSN(.{2})([^\s]*) ([^\s]*) Curncy", ticker) +def split_tenor_expiry(ticker, vol_type='N'): + m = re.match(f"USS{vol_type}(.{{2}})([^\s]*) ([^\s]*) Curncy", ticker) expiry, tenor, _ = m.groups() if expiry[0] == '0': expiry = expiry[1:] @@ -141,17 +141,18 @@ def split_tenor_expiry(ticker): tenor = int(tenor) return expiry, tenor -def insert_swaption_vol(data, conn, source): +def insert_swaption_vol(data, conn, source, vol_type="N"): if source == "ICPL": tenors = ['A', 'C', 'F'] + list(range(1, 11)) + [15, 20, 25, 30] else: tenors = ['A', 'C', 'F', 'I'] + list(range(1, 11)) + [15, 20, 25, 30] df = pd.concat(data, axis=1) df.columns = df.columns.get_level_values(0) - df.columns = pd.MultiIndex.from_tuples([split_tenor_expiry(c) for c in df.columns]) + df.columns = pd.MultiIndex.from_tuples([split_tenor_expiry(c, vol_type) for c in df.columns]) + table_name = "swaption_normal_vol" if vol_type == "N" else "swaption_lognormal_vol" for t in tenors[-14:]: - sql_str = f'INSERT INTO swaption_vol(date, "{t}y", source) ' + \ + sql_str = f'INSERT INTO {table_name}(date, "{t}y", source) ' + \ 'VALUES(%s, %s, %s) ON CONFLICT (date, source)' + \ f' DO UPDATE SET "{t}y" = %s, source = %s' with conn.cursor() as c: @@ -162,22 +163,29 @@ def insert_swaption_vol(data, conn, source): conn.commit() def update_swaption_vol(conn, session, - tenors=['A', 'C', 'F', 'I'] + list(range(1, 11)) + [15, 20, 25, 30]): + tenors=['A', 'C', 'F', 'I'] + list(range(1, 11)) + [15, 20, 25, 30], + vol_type="N"): + """ + Parameters + ---------- + vol_type : one of 'N' or 'V' (normal or log-normal) + """ + table_name = "swaption_normal_vol" if vol_type == "N" else "swaption_lognormal_vol" for source in ['BBIR', 'CMPN', 'ICPL']: tickers = [] for expiry in tenors: for tenor in tenors: - tickers.append(f"USSN{expiry:0>2}{tenor} {source} Curncy") + tickers.append(f"USS{vol_type}{expiry:0>2}{tenor} {source} Curncy") data = retrieve_data(session, tickers, ['PX_YEST_CLOSE', 'PX_CLOSE_DT']) for t in tenors[4:]: - sql_str = f'INSERT INTO swaption_vol(date, "{t}y", source) ' + \ + sql_str = f'INSERT INTO {table_name}(date, "{t}y", source) ' + \ 'VALUES(%s, %s, %s) ON CONFLICT (date, source)' + \ f' DO UPDATE SET "{t}y" = %s, source = %s' r = [] dates = [] for expiry in tenors: - ticker = f"USSN{expiry:0>2}{t} {source} Curncy" + ticker = f"USS{vol_type}{expiry:0>2}{t} {source} Curncy" if data[ticker]: r.append(data[ticker]['PX_YEST_CLOSE']) dates.append(data[ticker]['PX_CLOSE_DT']) @@ -262,9 +270,10 @@ if __name__=="__main__": populate_cashflow_history(dawn_engine, session, workdate) update_fx(dawn_conn, session, ['EURUSD', 'CADUSD']) update_swap_rates(serenitas_conn, session) - update_swaption_vol(serenitas_conn, session) + for vol_type in ["N", "V"]: + update_swaption_vol(serenitas_conn, session, vol_type) # with init_bbg_session(BBG_IP) as session: # init_fx(session, engine, pd.datetime(2013, 1, 1)) # with init_bbg_session(BBG_IP) as session: - # data = init_swaption_vol(session, source="CMPN") - # insert_swaption_vol(data, serenitas_conn, "CMPN") + # data = init_swaption_vol(session, source="BBIR", vol_type="V") + # insert_swaption_vol(data, serenitas_conn, "BBIR", vol_type="V") |
