aboutsummaryrefslogtreecommitdiffstats
path: root/python/handle_default.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/handle_default.py')
-rw-r--r--python/handle_default.py88
1 files changed, 52 insertions, 36 deletions
diff --git a/python/handle_default.py b/python/handle_default.py
index 1dcd18a5..4c001464 100644
--- a/python/handle_default.py
+++ b/python/handle_default.py
@@ -1,45 +1,61 @@
from db import serenitasdb
import datetime
+from sys import argv
+def affected_indices(company_id):
+ """returns the list of indices containing company_id"""
-company_id = 210065
-lastdate = datetime.date(2015, 2, 19)
-recovery = 15.875
-nissuers = 100
+ with serenitasdb.cursor() as c:
+ sqlstr = """SELECT * FROM
+ (SELECT unnest(index_list) AS basketid FROM bbg_issuers WHERE company_id=%s) a
+ JOIN index_version USING (basketid)
+ WHERE lastdate='infinity'"""
+ c.execute(sqlstr, (company_id,))
+ recordslist = c.fetchall()
+ return recordslist
-#get list of concerned indices
-with serenitasdb.cursor() as c:
- sqlstr = """SELECT * FROM
- (SELECT unnest(index_list) AS basketid FROM cds_issuers WHERE company_id=%s) a
- JOIN index_version USING (basketid)
- WHERE lastdate='infinity'"""
- c.execute(sqlstr, (ticker,))
- recordslist = c.fetchall()
+def create_newindices(recordslist, lastdate, nissuers):
+ """create the new indices versions and update the old"""
+ insertstr = """INSERT INTO index_version(Index, Series, Version, IndexFactor,
+ CumulativeLoss, lastdate) Values(%(index)s, %(series)s, %(version)s, %(indexfactor)s,
+ %(cumulativeloss)s, %(lastdate)s) RETURNING basketid"""
-insertstr = """INSERT INTO index_version(Index, Series, Version, IndexFactor,
-CumulativeLoss, lastdate) Values(%(index)s, %(series)s, %(version)s, %(indexfactor)s,
-%(cumulativeloss)s, %(lastdate)s) RETURNING basketid"""
+ updatestr = "UPDATE index_version SET lastdate=%s WHERE basketid=%s"
-with serenitasdb.cursor() as c:
- newids = {}
- for r in recordslist:
- r['indexfactor'] -= 1
- r['version'] += 1
- r['cumulativeloss'] += 1-recovery/float(nissuers)
- r['lastdate'] = 'infinity'
- c.execute(insertstr, r)
- newids[r['basketid']] = c.fetchone()[0]
+ with serenitasdb.cursor() as c:
+ newids = {}
+ for r in recordslist:
+ r['indexfactor'] -= 1
+ r['version'] += 1
+ r['cumulativeloss'] += 1-recovery/float(nissuers)
+ r['lastdate'] = 'infinity'
+ c.execute(insertstr, r)
+ newids[r['basketid']] = c.fetchone()[0]
+ for oldid in newids.keys():
+ c.execute(updatestr, (lastdate, oldid))
+ serenitasdb.commit()
+ return newids
-updatestr = "UPDATE index_version SET lastdate=%s WHERE basketid=%s"
-with serenitasdb.cursor() as c:
- for oldid in newids.keys():
- c.execute(updatestr, (lastdate, oldid))
+def update_indexmembers(newids, company_id):
+ with serenitasdb.cursor() as c:
+ for oldid, newid in newids.items():
+ c.execute("""update bbg_issuers set index_list=index_list||%s where company_id in
+ (select company_id from bbg_issuers where %s=Any(index_list)
+ and company_id != %s)""", (newid, oldid, company_id))
+ serenitasdb.commit()
-serenitasdb.commit()
-
-with serenitasdb.cursor() as c:
- for oldid, newid in newids.items():
- c.execute("""update cds_issuers set index_list=index_list||%s where company_id in
- (select company_id from cds_issuers where %s=Any(index_list)
- and company_id != %s)""", (newid, oldid, company_id))
-serenitasdb.commit()
+if __name__=="__main__":
+ if len(argv) == 1:
+ print("""Usage:
+ python handle_default.py <company_id> <date> <recovery> <n_issuers>
+For instance:
+ python handle_default.py 210065 2015-02-19 15.875 100""")
+ else:
+ company_id = int(argv[1])
+ lastdate = datetime.datetime.strptime(argv[2], "%Y-%m-%d")
+ recovery = float(argv[3])
+ n_issuers = float(argv[4])
+ recordslist = affected_indices(company_id)
+ newids = create_newindices(recordslist, lastdate, nissuers)
+ update_indexmbers(newids, company)
+ serenitasdb.close()