diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/globeop_reports.py | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/python/globeop_reports.py b/python/globeop_reports.py index 2de3f13e..965649e1 100644 --- a/python/globeop_reports.py +++ b/python/globeop_reports.py @@ -116,7 +116,7 @@ def calc_trade_performance_stats(): df[df.days_held.notnull()]['days_held'].groupby(pd.Grouper(freq='A')).mean() -def hist_pos(date=None, asset_class = 'rmbs'): +def hist_pos(asset_class = 'rmbs', dm=False): end_date = pd.datetime.today() - MonthEnd(1) dates = pd.date_range(datetime.date(2013,1,31), end_date, freq='M') @@ -124,20 +124,28 @@ def hist_pos(date=None, asset_class = 'rmbs'): calc_df = pd.DataFrame() for d in dates: if asset_class == 'rmbs': - calc_df = calc_df.append(rmbs_pos(d)) + if d.date() == datetime.date(2018,11,30): + d_1 = datetime.date(2018,12,3) + elif d.date() == datetime.date(2016,2,29): + d_1 = datetime.date(2016,2,29) + else: + d_1 = None + calc_df = calc_df.append(rmbs_pos(d, d_1, dm)) else: calc_df = calc_df.append(clo_pos(d), sort=True) return calc_df -def rmbs_pos(date): +def rmbs_pos(date, model_date=None, dm=False): date = date.date() if isinstance(date, pd.Timestamp) else date + #just non-zero factor bonds for now, need to incorporate that + pos = get_portfolio(date) pos = pos[(pos.port == 'MORTGAGES') & (pos.endbookmv > 0) & (pos.custacctname == 'V0NSCLMAMB') & - (pos['invid'].str.len() >= 9)] + (pos['invid'].str.len() == 9)] pos = pos[['endbookmv', 'endlocalmarketprice', 'identifier']] sql_string = ("SELECT distinct timestamp FROM priced where " @@ -147,12 +155,11 @@ def rmbs_pos(date): "order by timestamp desc") timestamps = pd.read_sql_query(sql_string, dawnengine, parse_dates=['timestamp'], params=[date, date - DateOffset(15, 'D')]) - model_date = (timestamps.loc[0][0]).date() + if model_date is None: + model_date = (timestamps.loc[0][0]).date() yc = YieldTermStructure() - libor1m = USDLibor(Period(1, Months), yc) yc.link_to(YC(evaluation_date=model_date)) - libor = libor1m.fixing(libor1m.fixing_calendar.adjust(Date.from_datetime(date))) sql_string = ("SELECT date(timestamp) as timestamp, cusip, model_version, " "pv, moddur, delta_yield, delta_ir " "FROM priced where date(timestamp) = %s " @@ -167,21 +174,27 @@ def rmbs_pos(date): model = pd.read_sql_query(sql_string, dawnengine, parse_dates=['timestamp'], params=params_list) model = model[model['pv'] != 0] - comb_g = pos.loc[date].groupby('identifier').agg({'endbookmv': 'sum', - 'endlocalmarketprice': 'mean'}) + pos = pos.assign(curr_ntl = pos.endbookmv/pos.endlocalmarketprice *100) + comb_g = pos.groupby('identifier').agg({'endbookmv': 'sum', + 'curr_ntl': 'sum'}) + comb_g['date'] = pd.to_datetime(date) model = pd.merge(comb_g, model, left_on='identifier', right_on='cusip') positions = model.set_index(['cusip', 'model_version']).unstack(1).dropna() v1 = positions.xs(1, level='model_version', axis=1) v3 = positions.xs(3, level='model_version', axis=1) - v3 = v3.assign(curr_ntl = v3.endbookmv/v3.endlocalmarketprice *100) - v3 = v3.assign(b_yield = v3.moddur.apply(lambda x: - float(yc.zero_rate(x)) - libor)) - v3.b_yield += np.minimum((v1.pv / v1.endlocalmarketprice * 100) + if dm is True: + libor1m = USDLibor(Period(1, Months), yc) + libor = libor1m.fixing(libor1m.fixing_calendar.adjust(Date.from_datetime(model_date))) + v3 = v3.assign(b_yield = v3.moddur.apply(lambda x: float(yc.zero_rate(x))) - + libor) + else: + v3 = v3.assign(b_yield = v3.moddur.apply(lambda x: float(yc.zero_rate(x)))) + v3.b_yield += np.minimum((v1.pv * v1.curr_ntl/ v1.endbookmv) ** (1/v1.moddur) - 1, 1).dropna() v3.delta_yield *= v3.endbookmv / v3.pv v3.delta_ir *= np.minimum(1, 1/v3.moddur) * \ - (v3.endlocalmarketprice/100)/ v3.pv * v3.curr_ntl - return v3.reset_index().set_index('timestamp') + (v3.endbookmv/v3.curr_ntl)/ v3.pv * v3.curr_ntl + return v3.reset_index().set_index('date') def clo_pos(date): |
