aboutsummaryrefslogtreecommitdiffstats
path: root/python/load_refentity.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/load_refentity.py')
-rw-r--r--python/load_refentity.py33
1 files changed, 25 insertions, 8 deletions
diff --git a/python/load_refentity.py b/python/load_refentity.py
index 837b5dfb..98387b6a 100644
--- a/python/load_refentity.py
+++ b/python/load_refentity.py
@@ -1,4 +1,7 @@
from db import dbconn
+from pathlib import Path
+import datetime
+import re
import lxml.etree as etree
parser = etree.XMLParser(remove_blank_text=True)
from psycopg2.extras import Json
@@ -71,11 +74,15 @@ def insert_refobligation(fname):
'type', 'isconvert', 'isperp', 'coupontype', 'ccy',
'maturity', 'issuedate', 'coupon', 'isin', 'cusip', 'event']
- redpair_insert = ("INSERT INTO RedPairMapping VALUES({})".
- format(",".join(["%s"] * len(names_redpair))))
- refobligation_insert = ("INSERT INTO RefObligation({}) VALUES({})".
+ redpair_insert = ("""INSERT INTO RedPairMapping VALUES({})
+ ON CONFLICT(redpaircode) DO UPDATE SET {}""".
+ format(",".join(["%s"] * len(names_redpair)),
+ ",".join(f"{name}=EXCLUDED.{name}" for name in names_redpair[1:])))
+ refobligation_insert = ("""INSERT INTO RefObligation({}) VALUES({})
+ ON CONFLICT(obligationname) DO UPDATE SET {}""".
format(",".join(names_refobligation),
- ",".join(["%s"] * len(names_refobligation))))
+ ",".join(["%s"] * len(names_refobligation)),
+ ",".join(f"{name}=EXCLUDED.{name}" for name in names_refobligation[1:])))
skipfirst = True
with conn.cursor() as c:
for child in tree.getroot():
@@ -95,8 +102,18 @@ def insert_refobligation(fname):
c.execute(refobligation_insert, [d.get(name) for name in names_refobligation])
conn.commit()
+def get_date(f):
+ m = re.search("(\d*)\.", f.name)
+ if m:
+ timestamp = int(m.groups(0)[0])
+ return datetime.datetime.fromtimestamp(timestamp/1000)
+
if __name__ == "__main__":
- #insert_refobligation('/home/serenitas/guillaume/V9 Red Obligation1503420865581.xml')
- from pathlib import Path
- base_dir = Path('/home/serenitas/CorpCDOs/Tranche_data/RED_reports')
- insert_refentity((base_dir / 'V10 Red Entity Delta1503687306161.xml').as_posix())
+ from markit_red import download_report
+ base_dir = Path('/home/serenitas/CorpCDOs/Tranche_data/RED_reports/Deltas')
+ for report in ['REDEntityDelta', 'REDObligationDelta']:
+ f = base_dir / download_report(report)[0]
+ if "Entity" in report:
+ insert_refentity(f.as_posix())
+ else:
+ insert_refobligation(f.as_posix())