aboutsummaryrefslogtreecommitdiffstats
path: root/python/markit/import_quotes.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/markit/import_quotes.py')
-rw-r--r--python/markit/import_quotes.py79
1 files changed, 45 insertions, 34 deletions
diff --git a/python/markit/import_quotes.py b/python/markit/import_quotes.py
index abcf3831..a8d05a29 100644
--- a/python/markit/import_quotes.py
+++ b/python/markit/import_quotes.py
@@ -17,6 +17,14 @@ from yieldcurve import get_curve
logger = logging.getLogger(__name__)
+__all__ = (
+ "copy_curves_forward",
+ "remove_curves",
+ "insert_cds",
+ "insert_index",
+ "insert_tranche",
+)
+
def convert(x):
try:
@@ -112,17 +120,6 @@ def get_current_tickers(database, workdate):
return get_markit_bbg_mapping(database, basketids, workdate)
-def get_defaulted(mappings, default_table, workdate):
- for bbg_id, _ in mappings:
- if event_date := default_table.get(bbg_id, False):
- if workdate >= event_date:
- defaulted = event_date
- break
- else:
- defaulted = None
- return defaulted
-
-
def csv_file_gen(workdate):
CDS_DIR = BASE_DIR / "Tranche_data" / "CDS" / f"{workdate:%Y}"
csv_file = CDS_DIR / f"{workdate}_fixed.csv.lz4"
@@ -218,29 +215,32 @@ def insert_cds(database, workdate: datetime.date):
coupon_rates = np.array(
[convert(line[c]) / 100 for c in col_spread]
)
- defaulted = get_defaulted(mappings, default_table, workdate)
- try:
- sc = SpreadCurve(
- workdate,
- yc_dict[k.currency],
- None,
- None,
- None,
- tenors,
- coupon_rates,
- upfront_rates,
- recovery_rates,
- ticker=k.ticker,
- seniority=seniority_mapping[k.tier],
- doc_clause=DocClause[k.short_code],
- defaulted=defaulted,
- )
- except ValueError:
- logging.error(f"couldn't build curve for {k.ticker}")
- else:
- buf = sc.as_buffer(True)
- for (cid, sen), curves in mappings:
+ for (cid, sen), curves in mappings:
+ defaulted = None
+ if event_date := default_table.get(cid, False):
+ if workdate >= event_date:
+ defaulted = event_date
+ try:
+ sc = SpreadCurve(
+ workdate,
+ yc_dict[k.currency],
+ None,
+ None,
+ None,
+ tenors,
+ coupon_rates,
+ upfront_rates,
+ recovery_rates,
+ ticker=k.ticker,
+ seniority=seniority_mapping[k.tier],
+ doc_clause=DocClause[k.short_code],
+ defaulted=defaulted,
+ )
+ except ValueError:
+ logging.error(f"couldn't build curve for {k.ticker}")
+ else:
+ buf = sc.as_buffer(True)
c.execute(
"INSERT INTO cds_curves VALUES(%s, %s, %s, %s, %s) "
"ON CONFLICT (date, company_id, seniority) "
@@ -263,7 +263,7 @@ def insert_cds(database, workdate: datetime.date):
for t, upf in zip(curves, upfront_rates)
],
)
- tickers_found.add(k)
+ tickers_found.add(k)
database.commit()
# handle missing tickers
@@ -288,6 +288,17 @@ def insert_cds(database, workdate: datetime.date):
logger.error(f"{curve_key.full_ticker} never existed")
else:
if (workdate - date).days < 20: # we copy over the old curve
+ # check if there was an event of default
+ # in that case, mark the curve as defaulted
+ sc = SpreadCurve.from_bytes(curve, True)
+ if not sc.defaulted:
+ defaulted = None
+ if event_date := default_table.get(cid, False):
+ if workdate >= event_date:
+ defaulted = event_date
+ if defaulted:
+ sc.default_date = defaulted
+ curve = sc.as_buffer(True)
c.execute(
"INSERT INTO cds_curves VALUES(%s, %s, %s, %s, %s) "
"ON CONFLICT (date, company_id, seniority) "