aboutsummaryrefslogtreecommitdiffstats
path: root/python/markit_download.py
blob: 4b34608b48aef2170ee6e914982982f5f92b5469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import requests
import common
import os
import datetime
import csv

legal = 'serecap'
username = 'serecapuser'
password = 'Welcome1'

workdate = str(datetime.date.today())

def convertToNone(v):
    return v if v else None

r = requests.get('https://loans.markit.com/loanx/LoanXMarks.csv?LEGALENTITY={0}&USERNAME={1}&PASSWORD={2}&EOD=Y'.format(legal, username, password))
marks_filename = os.path.join(common.root, "data", "markit", "markit_data_{0}.csv".format(workdate))
with open(marks_filename, "wb") as fh:
    fh.write(r.content)

r = requests.get('https://loans.markit.com/loanx/LoanXFacilityUpdates.csv?LEGALENTITY={0}&USERNAME={1}&PASSWORD={2}'.format(legal, username, password))

facility_filename = os.path.join(common.root, "data", "Facility files", "facility_{0}.csv".format(workdate))
with open( facility_filename, "wb") as fh:
    fh.write(r.content)

sqlstring = "INSERT INTO markit_prices2 VALUES( {0} )".format( ",".join([ "%s" ] * 5))
with open(marks_filename, "r") as fh:
    reader = csv.reader(fh)
    reader.next()               # we skip the headers
    for line in reader:
        if line[4] == "implied":
            line[4] = 0
        common.cursor.execute(sqlstring, (line[0], line[2], line[3], line[4], line[1]))
common.conn.commit()

sqlstring = "INSERT INTO markit_facility VALUES( {0} )".format( ",".join(["%s"] * 13))
with open( facility_filename, "r") as fh:
    reader = csv.reader(fh)
    reader.next() # we skip the headers
    for line in reader:
        newline = [convertToNone(v) for v in line]
        common.cursor.execute(sqlstring, newline)
common.conn.commit()

#we update the missing facility loanxids
sqlstring = "SELECT loanxid FROM markit_prices2 EXCEPT SELECT loanxid FROM markit_facility";
common.cursor.execute(sqlstring)
facility_diff_filename = os.path.join(common.root, "data", "Facility files",
                                      "facility_diff_{0}.csv".formfaat(workdate))
with open( facility_diff_filename, "wb") as fh:
    flag = False
    for loanxid in common.cursor.fetchall():
        r = requests.get('https://loans.markit.com/loanx/LoanXOneFacility.csv?LEGALENTITY={0}&USERNAME={1}&PASSWORD={2}&LOANXID={3}'.format(legal, username, password, loanxid[0]))
        if flag:
            fh.write(r.content.split('\n')[1] + "\n")
        else:
            fh.write(r.content.split('\n')[0] + "\n")
            fh.write(r.content.split('\n')[1] + "\n")
            flag = True

sqlstring = "INSERT INTO markit_facility(LoanXID, PMDID, IssuerName, dealname, facility_type," \
    "loanx_facility_type, initial_amount, initial_spread, maturity, industry, modified_time)" \
    "VALUES( {0} )".format( ",".join( ["%s"] * 11))
if os.path.getsize(facility_diff_filename):
    with open(facility_diff_filename, "r") as fh:
        reader = csv.reader(fh)
        reader.next()
        for line in reader:
            newline = [convertToNone(v) for v in line] + [workdate]
            newline.pop(9)          # remove the spread to maturity value
            common.cursor.execute(sqlstring, newline)
    common.conn.commit()

common.cursor.close()
common.conn.close()