diff options
Diffstat (limited to 'python/handle_default.py')
| -rw-r--r-- | python/handle_default.py | 76 |
1 files changed, 45 insertions, 31 deletions
diff --git a/python/handle_default.py b/python/handle_default.py index 426df961..85eb852b 100644 --- a/python/handle_default.py +++ b/python/handle_default.py @@ -2,26 +2,33 @@ from utils.db import serenitas_pool import datetime from sys import argv + def get_recovery(company_id: int, seniority: str, conn): with conn.cursor() as c: - c.execute("SELECT recovery * 100, auction_date FROM defaulted " - "WHERE id=%s AND seniority=%s", - (company_id, seniority)) + c.execute( + "SELECT recovery * 100, auction_date FROM defaulted " + "WHERE id=%s AND seniority=%s", + (company_id, seniority), + ) return c.fetchone() + def affected_indices(company_id: int, seniority: str, conn): """returns the list of indices containing company_id""" - sqlstr = ("SELECT b.*, a.curr_weight*b.indexfactor AS orig_weight " - "FROM basket_constituents_current a " - "JOIN (SELECT * FROM index_version WHERE lastdate='infinity') b " - "USING (basketid) " - "WHERE company_id=%s AND seniority=%s") + sqlstr = ( + "SELECT b.*, a.curr_weight*b.indexfactor AS orig_weight " + "FROM basket_constituents_current a " + "JOIN (SELECT * FROM index_version WHERE lastdate='infinity') b " + "USING (basketid) " + "WHERE company_id=%s AND seniority=%s" + ) print(sqlstr) with conn.cursor() as c: c.execute(sqlstr, (company_id, seniority)) recordslist = c.fetchall() return recordslist + def create_newindices(recordslist, recovery, lastdate, conn): """create the new indices versions and update the old""" insertstr = """INSERT INTO index_version(Index, Series, Version, IndexFactor, @@ -33,36 +40,40 @@ def create_newindices(recordslist, recovery, lastdate, conn): with conn.cursor() as c: newids = {} for r in recordslist: - r['indexfactor'] -= r['orig_weight'] - r['version'] += 1 - r['cumulativeloss'] += (100-recovery) * r['orig_weight'] /100 - r['lastdate'] = 'infinity' + r["indexfactor"] -= r["orig_weight"] + r["version"] += 1 + r["cumulativeloss"] += (100 - recovery) * r["orig_weight"] / 100 + r["lastdate"] = "infinity" c.execute(insertstr, r) - newids[r['basketid']] = c.fetchone()[0] + newids[r["basketid"]] = c.fetchone()[0] for oldid in newids.keys(): c.execute(updatestr, (lastdate, oldid)) conn.commit() return newids + def update_indexmembers(newids, company_id, seniority, conn): with conn.cursor() as c: for oldid, newid in newids.items(): - c.execute("INSERT INTO basket_constituents " - "(SELECT company_id, seniority, %s, weight " - "FROM basket_constituents " - "WHERE basketid=%s AND NOT (company_id=%s AND seniority=%s))", - (newid, oldid, company_id, seniority)) + c.execute( + "INSERT INTO basket_constituents " + "(SELECT company_id, seniority, %s, weight " + "FROM basket_constituents " + "WHERE basketid=%s AND NOT (company_id=%s AND seniority=%s))", + (newid, oldid, company_id, seniority), + ) conn.commit() + def update_redcodes(index_type, conn): - if index_type == 'HY': - index_subfamily = 'CDX.NA.HY' - elif index_type == 'IG': - index_subfamily = 'CDX.NA.IG' - elif index_type == 'EU': - index_subfamily = 'iTraxx Europe' - elif index_type == 'XO': - index_subfamily = 'iTraxx Europe Crossover' + if index_type == "HY": + index_subfamily = "CDX.NA.HY" + elif index_type == "IG": + index_subfamily = "CDX.NA.IG" + elif index_type == "EU": + index_subfamily = "iTraxx Europe" + elif index_type == "XO": + index_subfamily = "iTraxx Europe Crossover" with conn.cursor() as c: c.execute( @@ -72,16 +83,19 @@ def update_redcodes(index_type, conn): "AND index_version.index=%s " "AND index_version_markit.indexsubfamily=%s" "AND index_version.redindexcode IS NULL", - (index_type, index_subfamily)) + (index_type, index_subfamily), + ) conn.commit() -if __name__=="__main__": +if __name__ == "__main__": if len(argv) == 1: - print("""Usage: + print( + """Usage: python handle_default.py <company_id> <seniority> For instance: - python handle_default.py 210065 Senior""") + python handle_default.py 210065 Senior""" + ) else: conn = serenitas_pool.getconn() company_id = int(argv[1]) @@ -90,4 +104,4 @@ For instance: recordslist = affected_indices(company_id, seniority, conn) newids = create_newindices(recordslist, recovery, lastdate, conn) update_indexmembers(newids, company_id, seniority, conn) - serenitas_pool.putconn(conn,) + serenitas_pool.putconn(conn) |
