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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
import os
import os.path
from ftplib import FTP
import gnupg
from task_server import config
import re
import logging
import shutil
import sys
import pandas as pd
from sqlalchemy import create_engine
sys.path.append('..')
import load_globeop_report
logger = logging.getLogger(__name__)
try:
import pandas as pd
from pandas.tseries.offsets import BDay
except ImportError:
pass
def get_ped(s):
regex = re.search("PED=([^.]+)", s)
if regex:
PED = pd.datetime.strptime(regex.group(1), "%Y-%m-%d").date()
else:
regex = re.search("([^.]+)", s)
PED = pd.to_datetime(regex.group(1), format="%Y%m%d") - BDay(1)
PED = PED.date()
return PED
def key_fun(s):
PED = get_ped(s)
regex = re.search("KD=([^.]+)", s)
if regex:
KD = pd.datetime.strptime(regex.group(1), "%Y-%m-%d-%H-%M-%S")
else:
regex = re.search("([^.]+\.[^.]+)", s)
KD = pd.datetime.strptime(regex.group(1), "%Y%m%d.%H%M%S")
return (PED, KD)
def run_date(s):
if 'SWO' in s:
date_string = s.split("_", 5)[4]
else:
date_string = s.split("_", 3)[2]
return pd.datetime.strptime(date_string, "%Y%m%d.%H%M%S")
def get_ftp(folder):
ftp = FTP('ftp.globeop.com')
ftp.login('srntsftp', config.ftp_password)
ftp.cwd(folder)
return ftp
def get_gpg():
if os.name=='nt':
gpg = gnupg.GPG(gpgbinary=r'"c:\\Program Files (x86)\\GNU\\GnuPG\\gpg2.exe"',
gnupghome=os.path.join(os.getenv('APPDATA'), "gnupg"))
elif os.name == 'posix':
gpg = gnupg.GPG(gnupghome=os.path.join(os.environ['HOME'], '.gnupg'))
gpg.encoding = 'utf8'
return gpg
def convert_to_csv(f):
if os.path.exists(f + ".xls"):
df = pd.read_excel(f + ".xls", sheetname=0, skiprows=[0,1,2,3])
df.to_csv(f + ".csv", index=False)
os.remove(f + ".xls")
def download_data(workdate):
ftp = get_ftp('outgoing')
files = ftp.nlst()
pnlfiles = [filename for filename in files if "csv" in filename and \
"Profit" in filename if get_ped(filename) < workdate]
valuationfiles = [filename for filename in files if "csv" in filename and \
"Valuation_TradeID" in filename if get_ped(filename) < workdate]
cdsfiles = [filename for filename in files if "TradeSearch" in filename \
if run_date(filename).date() <= workdate]
available_files = []
if pnlfiles:
available_files.append(max(pnlfiles, key=key_fun))
if valuationfiles:
available_files.append(max(valuationfiles, key=key_fun))
if cdsfiles:
available_files.append(max(cdsfiles, key=run_date))
if not available_files:
logger.error("no file available for date: %s" % str(workdate))
return
reports_dir = os.path.join(os.environ['DAILY_DIR'], str(workdate), "Reports")
if not os.path.exists(reports_dir):
os.makedirs(reports_dir)
for filename in available_files:
with open(os.path.join(reports_dir, filename), "wb") as fh:
ftp.retrbinary('RETR ' + filename, fh.write)
logger.info("downloaded {0}".format(filename))
gpg = get_gpg()
for filename in available_files:
if "Profit" in filename:
newfilename = "Pnl_Report.csv"
elif "Valuation" in filename:
newfilename = "Valuation_Report.csv"
else:
newfilename = "CDS_Report.xls"
with open(os.path.join(reports_dir, filename), "rb") as fh:
dec = gpg.decrypt_file(fh, output = os.path.join(reports_dir, newfilename),
passphrase=config.key_password,
always_trust=True)
logger.info('{0}: {1}'.format(filename, dec.status))
os.remove(os.path.join(reports_dir, filename))
## convert xls to csv
convert_to_csv(os.path.join(reports_dir, "CDS_Report"))
insert_todb(workdate)
def insert_todb(workdate):
reports_dir = os.path.join(os.environ['DAILY_DIR'], str(workdate), "Reports")
engine = create_engine('postgresql://dawn_user@debian/dawndb')
for report in ["Valuation", "Pnl", "CDS"]:
fun = getattr(load_globeop_report, "read_{}_report".format(report.lower()))
table = "{}_reports".format(report.lower())
report_file = os.path.join(reports_dir, "{}_Report.csv".format(report))
if not os.path.exists(report_file):
continue
df = fun(report_file)
if report == "Valuation":
period_end_date = pd.Timestamp(df.periodenddate[0])
sql_str = "DELETE FROM valuation_reports WHERE periodenddate=%s"
else:
df['date'] = period_end_date
sql_str = "DELETE FROM {} WHERE date=%s".format(table)
df['row'] = df.index
engine.execute(sql_str, (period_end_date,))
df.to_sql(table, engine, if_exists='append', index=False)
def upload_bond_marks(engine, workdate):
df = pd.read_sql_query("SELECT * from list_marks(%s)", engine, params = (workdate.date(),))
df.rename(columns = {'identifier': 'IDENTIFIER',
'price': 'Price'}, inplace=True)
filename = 'securitiesNpv{0:%Y%m%d_%H%M%S}.csv'.format(workdate)
fullpath = os.path.join(os.environ['DAILY_DIR'], str(workdate.date()), filename)
df.to_csv(fullpath, index=False)
ftp = get_ftp('incoming')
with open(fullpath, "rb") as fh:
ftp.storbinary('STOR ' + filename, fh)
logger.info("upload bond marks done")
def upload_cds_marks(engine, workdate):
df = pd.read_sql_query("""SELECT cds.dealid AS "DealID", 'CREDIT_SWAP' AS "Instrument Type",
(a.clean_nav+a.accrued) AS "NPV" from list_abscds_marks(%s) a
JOIN cds USING (security_id)""", engine, params = (workdate.date(),))
filename = 'otcNpv{0:%Y%m%d}.csv'.format(workdate)
fullpath = os.path.join(os.environ['DAILY_DIR'], str(workdate.date()), filename)
df.to_csv(fullpath, index=False)
ftp = get_ftp('incoming')
with open(fullpath, "rb") as fh:
ftp.storbinary('STOR ' + filename, fh)
logger.info("upload cds marks done")
def upload_data(engine, workdate):
upload_bond_marks(engine, workdate)
upload_cds_marks(engine, workdate)
def back_fill(start_date=pd.datetime(2017,7,20)):
date_rng = pd.date_range(start=start_date, end=pd.Timestamp.today(), freq='B')
for date in date_rng:
insert_todb(date.date())
|