diff options
Diffstat (limited to 'python/cds_curve.py')
| -rw-r--r-- | python/cds_curve.py | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/python/cds_curve.py b/python/cds_curve.py index ab13a9ef..c833ab0b 100644 --- a/python/cds_curve.py +++ b/python/cds_curve.py @@ -31,15 +31,17 @@ def all_curves_pv(curves, today_date, jp_yc, start_date, step_in_date, value_dat def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr'], start_date=None): index = MarkitBasketIndex(index_type, series, tenors) - r = {} + r = [] if start_date: index.index_quotes = index.index_quotes[start_date:] - for trade_date in index.index_quotes.index.get_level_values(0): - index.value_date = trade_date + for value_date, v in index.index_quotes.groupby('date')['id']: + index.value_date = value_date index.tweak() - r[trade_date] = pd.DataFrame({'duration': index.duration(), - 'theta': index.theta(), - 'tweak': index.tweaks}, index=tenors) + df = pd.concat([index.theta(), + index.duration(), + pd.Series(index.tweaks, index=tenors, name='tweak')], axis=1) + df.index = v.values + r.append(df) return pd.concat(r) @@ -67,11 +69,10 @@ if __name__ == "__main__": df = calibrate_portfolio(index, series, ['3yr', '5yr', '7yr', '10yr'], start_date) - with conn.cursor() as c: - for k, s in df.iterrows(): - c.execute("UPDATE index_quotes SET duration2=%s, theta2=%s " - "WHERE date=%s AND tenor=%s AND index=%s AND series=%s", - (s.duration, s.theta, k[0], k[1], index, series)) + for t in df.itertuples(): + c.execute("INSERT INTO index_risk2 VALUES(%s, %s, %s, %s) ON CONFLICT (id) " + "DO UPDATE SET duration=%s, theta=%s, tweak=%s", + t + t[1:]) conn.commit() conn.close() |
