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
|
import redis
import pandas as pd
import csv
from io import BytesIO, StringIO
import datetime
from pickle import loads
def get_trades():
q = redis.Redis(host = 'debian')
p = q.pipeline()
p.lrange('trades', 0, -1).delete('trades')
r = p.execute()
return pd.DataFrame([loads(e) for e in r[0]])
def aux(v):
if v.action.iat[-1] == 'CANCEL':
return None
if v.action.iat[0] == 'NEW':
v.action.iat[-1] = 'NEW'
return v.iloc[-1]
def build_line(obj):
line = ["Mortgage", obj.dealid, obj.action ,"Serenitas", None, None , obj.folder,
obj.custodian, obj.cashaccount, obj.cp_code, None, 'Valid',
str(obj.trade_date), str(obj.settle_date), None, None, obj.cusip, obj['isin'],
None, None, None, obj['description'], "Buy" if obj.buysell else "Sell", None,
obj.accrued, obj.price, None, None, 'SERCGMAST', 'MORTGAGE',
None, None, None, None, obj.faceamount, None, None, 'S']
return line
def delete_trade(tradeid):
pass
def generate_csv(df):
output = StringIO()
csvwriter = csv.writer(output)
headers = ['Deal Type', 'Deal ID', 'Action', 'Client', 'Reserved', 'Reserved',
'Folder', 'Custodian', 'Cash Account', 'Counterparty', 'Comments',
'State', 'Trade Date', 'Settlement Date', 'Reserved', 'GlopeOp Security Identifier',
'CUSIP', 'ISIN', 'Reserved', 'Reserved',
'Reserved', 'Security Description', 'Transaction Indicator',
'SubTransaction Indicator', 'Accrued', 'Price', 'BlockId', 'BlockAmount',
'Fund', 'Portfolio', 'Reserved', 'Reserved', 'ClientReference', 'ClearingMode',
'FaceAmount', 'Pool Factor', 'FactorAsOfDate', 'Delivery']
csvwriter.writerow(headers)
for tradeid, v in df.sort('lastupdate').groupby('id'):
trade = aux(v)
if trade is None:
delete_trade(tradeid)
else:
csvwriter.writerow(build_line(trade))
#convert to bytes
output = BytesIO(output.getvalue().encode('utf-8'))
return output
def upload_buffer(buf):
# ftp = FTP('ftp.globeop.com')
# ftp.login('srntsftp', config.ftp_password)
# ftp.cwd('incoming')
filename = ('Serenitas.ALL.{0}.Mortgages.csv'
.format(pd.datetime.strftime(pd.datetime.now(),
"%Y%m%d.%H%M%S")))
# cmd = 'STOR {0}'.format(filename)
# ftp.storbinary(cmd, buf)
# buf.seek()
with open(filename, 'wb') as fh:
fh.write(buf.getbuffer())
if __name__=="__main__":
df = get_trades()
buf = generate_csv(df)
upload_buffer(buf)
|