aboutsummaryrefslogtreecommitdiffstats
path: root/python/process_queue.py
blob: 4da8eab297121f363d713e6354b8071f34826f09 (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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import argparse
import csv
import datetime
import logging
import os
import psycopg2
import re
import socket
import sys
import task_server.config as config

from io import StringIO

from itertools import groupby
from pickle import loads
from ftplib import FTP
from sqlalchemy import create_engine
from bbg_helpers import init_bbg_session, retrieve_data, BBG_IP
from common import get_redis_queue
from analytics.utils import previous_twentieth
from db import dbconn
from send_email import EmailMessage
from tabulate import tabulate

HEADERS = {'bond_trades': [
    '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'],
           'cds_trades': [
               'Deal Type', 'Deal ID', 'Action', 'Client', 'Reserved', 'Reserved',
               'Folder', 'Custodian', 'Cash Account', 'Counterparty', 'Comments',
               'State', 'Trade Date', 'Reserved', 'Reserved', 'EffectiveDate', 'MaturityDate',
               'Currency', 'Notional', 'FixedRate', 'PaymentRollDateConvention', 'DayCount',
               'PaymentFrequency', 'FirstCouponRate', 'FirstCouponDate', 'ResetLag', 'Liquidation',
               'LiquidationDate', 'Protection', 'UnderlyingSecurityId',
               'UnderlyingSecurityDescription', 'CreditSpreadCurve',
               'CreditEvents', 'RecoveryRate', 'Settlement', 'InitialMargin',
	       'InitialMarginPercentage','InitialMarginCurrency', 'DiscountCurve',
	       'ClientReference', 'UpfrontFee', 'UpfrontFeePayDate', 'RegenerateCashFlow',
               'UpfrontFeeComment', 'GiveUpBroker','SwapType', 'OnPrice',
	       'OffPrice', 'AttachmentPoint', 'ExhaustionPoint', 'Fees', 'Fee Payment Dates',
	       'Fee Comments', 'Credit Event Occurred', 'Calendar',
               'Clearing Facility', 'Adjusted', 'CcpTradeRef', 'BlockId',
	       'BlockAmount', 'NettingId', 'AnnouncementDate', 'ExecTS',
	       'DefaultProbability', 'ClientMargin', 'Factor', 'ISDADefinition'],
           'swaption_trades': [
               'Deal Type', 'Deal ID','Action', 'Client', 'Fund', 'Portfolio',
	       'Folder', 'Custodian', 'Cash Account', 'Counterparty', 'Comments',
	       'State', 'Trade Date', 'Reserved', 'Reserved', 'Reserved',
	       'Notional', 'PremiumSettlementDate', 'ExpirationDate',
               'PremiumCurrency', 'PercentageOfPremium', 'ExerciseType', 'Reserved',
	       'SettlementMode', 'SettlementRate', 'Transaction Indicator',
               'InitialMargin', 'InitialMarginPercentage', 'InitialMarginCurrency',
               'ReceiveLegRateType', 'ReceiveFloatRate', 'ReceiveFirstCouponDate',
	       'ReceiveFirstCouponRate', 'ReceiveFixedRate', 'ReceiveDaycount',
               'ReceiveFrequency', 'ReceivePaymentRollConvention',
               'ReceiveEffectiveDate', 'ReceiveMaturityDate',
               'ReceiveNotional', 'ReceiveArrears', 'ReceiveAdjusted', 'ReceiveCompound',
	       'ReceiveCurrency',
               'PayLegRateType', 'PayFloatRate', 'PayFirstCouponDate',
               'PayFirstCouponRate', 'PayFixedRate', 'PayDaycount', 'PayFrequency',
	       'PayPaymentRollConvention', 'PayEffectiveDate', 'PayMaturityDate',
               'PayNotional', 'PayArrears', 'PayAdjusted', 'PayCompound', 'PayCurrency',
	       'RegenerateCashFlow', 'GiveUpBroker', 'ClientReference', 'ReceiveDiscountCurve',
	       'ReceiveForwardCurve', 'PayDiscountCurve', 'PayForwardCurve', 'ReceiveFixingFrequency',
	       'ReceiveInterestCalcMethod', 'ReceiveCompoundAverageFrequency',
	       'PayFixingFrequency', 'PayInterestCalcMethod', 'PayCompoundAverageFrequency',
               'SwapType', 'AttachmentPoint', 'ExhaustionPoint', 'UnderlyingInstrument',
	       'AssociatedDealType', 'AssociatedDealId', 'CounterpartyReference',
               'PremiumSettlementCurrency', 'PremiumSettlementAmount', 'ReceiveIMM Period',
               'PayIMMPeriod', 'Reserved', 'ClearingFacility', 'Strike', 'CcpTradeRef',
               'BreakClauseFrequency', 'BlockId', 'BlockAmount', 'Cross Currency Premium Payment',
	       'Premium Payment Amount', 'Netting Id', 'BreakClauseDate'],
           'future_trades': [
               "Deal Type", "Deal ID", "Action", "Client", "Reserved", "Reserved",
               "Folder", "Custodian", "Cash Account", "Counterparty", "Comments",
               "State", "Trade Date", "Settlement Date", "Reserved",
               "GlopeOp Security Identifier", "Reserved", "Reserved", "Reserved",
               "Bloomberg Ticker", "RIC", "Security Description",
               "Transaction Indicator", "SubTransaction Indicator",
	       "Quantity", "Price", "Commission", "Tax", "VAT",
               "Trade Currency", "Reserved", "Reserved", "Broker Short Name",
	       "MaturityDate",	"Exchange", "Client Reference", "Swap Type",
               "Initial Margin", "Initial Margin Currency", "Future Event",
               "Commission Entries", "BlockId", "Block Amount"],
           'wires': [
               "Deal Type", "Deal ID", "Action", "Client", "Reserved", "Reserved",
               "Folder", "Custodian", "Cash Account", "Counterparty" "Comments",
               "State", "Trade Date", "Settlement Date", "Reserved", "Reserved",
	       "Currency", "Amount", "Associated Deal Type", "Associated Deal Id",
	       "Transaction Type", "Instrument Type", "Yield", "Client Reference",
	       "ClearingFacility", "Deal Function", "Reset Price", "Reset Date",
	       "Ccp Trade Ref", "Margin Type", "Block Id", "Block Amount"]
}

def get_effective_date(d):
    return previous_twentieth(d + datetime.timedelta(days=1))

def get_trades(q, queue_name='bond_trades'):
    r = q.lrange(queue_name, 0, -1)
    df = [loads(e) for e in r]
    list_trades = []
    if df:
        for tradeid, v in groupby(df, lambda x: x['id']):
            trades = list(v)
            trades = sorted(trades, key = lambda x: x['lastupdate'])
            if len(trades) == 1:
                list_trades.append(trades[0])
            else:
                if trades[-1]['action'] == 'CANCEL':
                    continue
                if trades[0].action == 'NEW':
                    trades[-1]['action'] = 'NEW'
                    list_trades.append(trades[-1])
    return list_trades

def rename_keys(d, mapping):
    """ rename keys in dictionary according to mapping dict inplace"""
    for k, v in mapping.items():
        if k in d:
            d[v] = d.pop(k)

def build_line(obj, queue_name='bond_trades'):
    obj['Client'] = 'Serenitas'
    obj['Fund'] = 'SERCGMAST'
    obj['State'] = 'Valid'
    rename_cols = {'action': 'Action',
                   'dealid': 'Deal ID',
                   'folder': 'Folder',
                   'custodian': 'Custodian',
                   'cashaccount': 'Cash Account',
                   'cp_code': 'Counterparty',
                   'identifier': 'GlopeOp Security Identifier',
                   'cusip': 'CUSIP',
                   'isin': 'ISIN',
                   'description': 'Security Description',
                   'accrued': 'Accrued',
                   'price': 'Price',
                   'faceamount': 'FaceAmount',
                   'trade_date': 'Trade Date',
                   'settle_date': 'Settlement Date',
                   'effective_date': 'EffectiveDate',
                   'maturity': 'MaturityDate',
                   'currency': 'Currency',
                   'curr_notional': 'Notional',
                   'fixed_rate': 'FixedRate',
                   'payment_rolldate': 'PaymentRollDateConvention',
                   'day_count': 'DayCount',
                   'protection': 'Protection',
                   'security_id': 'UnderlyingSecurityId',
                   'security_desc': 'UnderlyingSecurityDescription',
                   'upfront': 'UpfrontFee',
                   'upfront_settle_date': 'UpfrontFeePayDate',
                   'swap_type': 'SwapType',
                   'attach':'AttachmentPoint',
                   'detach':'ExhaustionPoint',
                   'clearing_facility': 'Clearing Facility',
                   'isda_definition': 'ISDADefinition',
                   'expiration_date': 'ExpirationDate'}

    rename_keys(obj, rename_cols)
    if queue_name in ['bond_trades', 'swaption_trades', 'future_trades']:
        obj['Transaction Indicator'] = "Buy" if obj['buysell'] else "Sell"
    if queue_name == 'bond_trades':
        obj['Deal Type'] = 'MortgageDeal'
        obj['Portfolio'] = 'MORTGAGE'
        obj['Delivery'] = 'S'
        ## zero coupon bond
        if obj['CUSIP'] != obj['GlopeOp Security Identifier']:
            obj['CUSIP'] = None
    elif queue_name == 'swaption_trades':
        obj['Deal Type'] = 'SwaptionDeal'
        obj['Portfolio'] = 'OPTIONS'
        obj['ExerciseType'] = 'European'
        obj['SettlementMode'] = 'Delivery'
        rename_keys(obj, {'Settlement Date': 'PremiumSettlementDate',
                          'Price': 'PercentageOfPremium',
                          'notional': 'Notional',
                          'initial_margin_percentage': 'InitialMarginPercentage'})

        obj['RegenerateCashFlow'] = 'N'
        for direction in ['Pay', 'Receive']:
            obj[direction + 'Daycount'] = 'ACT/360'
            obj[direction + 'Frequency'] = 'Quarterly'
            obj[direction + 'PaymentRollConvention'] = 'Following'
            obj[direction + 'MaturityDate'] = obj['MaturityDate']
            obj[direction + 'Currency'] = obj['Currency']
            obj[direction + 'Notional'] = obj['Notional']
            obj[direction + 'EffectiveDate'] = get_effective_date(obj['Trade Date'])
        obj['PremiumCurrency'] = obj['Currency']
        if obj['InitialMarginPercentage']:
            obj['InitialMarginCurrency'] = obj['Currency']
        obj['SwapType'] = 'CD_INDEX_OPTION'
        obj['UnderlyingInstrument'] = obj.pop('UnderlyingSecurityId')
        obj['Strike'] = obj.pop('strike')
        if obj['swaption_type'] == 'PAYER':
            obj['ReceiveLegRateType'] = 'Float'
            obj['ReceiveFloatRate'] = 'US0003M'
            obj['PayLegRateType'] = 'Fixed'
            obj['PayFixedRate'] = obj['FixedRate']
        elif obj['swaption_type'] == 'RECEIVER':
            obj['PayLegRateType'] = 'Float'
            obj['PayFloatRate'] = 'US0003M'
            obj['ReceiveLegRateType'] = 'Fixed'
            obj['ReceiveFixedRate'] = obj['FixedRate']
    elif queue_name == 'cds_trades':
        freq = {4: 'Quarterly', 12: 'Monthly'}
        obj['Deal Type'] = 'CreditDefaultSwapDeal'
        obj['PaymentFrequency'] = freq[obj['frequency']]
        obj['InitialMarginPercentage'] = obj.pop('initial_margin_percentage')
        if obj['InitialMarginPercentage']:
            obj['InitialMarginCurrency'] = obj['Currency']
    elif queue_name == 'future_trades':
        obj['Deal Type'] = 'FutureDeal'
        rename_keys(obj, {'currency':'Trade Currency',
                          'commission': 'Commission',
                          'quantity': 'Quantity',
                          'swap_type': 'Swap Type',
                          'bbg_ticker': 'Bloomberg Ticker',
                          'Currency': 'Trade Currency',
                          'exchange': 'Exchange'})
    elif queue_name == 'wires':
        obj['Deal Type'] = 'CashFlowDeal'
        obj['Transaction Type'] = 'Transfer'
        obj['Instrument Type'] = 'Cashflow'
        obj['Settlement Date'] = obj['Trade Date']

    return [obj.get(h, None) for h in HEADERS[queue_name]]

def get_bbg_data(conn, session, identifier, cusip=None, isin = None, settle_date = None, asset_class=None,
                 **kwargs):
    fields = ["MTG_FACTOR_SET_DT", "INT_ACC"]
    fields_dict = {'Mtge': ["MTG_FACE_AMT", "START_ACC_DT"],
                   'Corp': ["AMT_ISSUED", "PREV_CPN_DT"]}
    with conn.cursor() as c:
        c.execute("SELECT identifier FROM securities WHERE identifier=%s",
                  (identifier,))
        if not c.fetchone():
            fields += ["MATURITY", "CRNCY", "NAME", "FLOATER", "FLT_SPREAD", "CPN",
                       "CPN_FREQ", "FIRST_CPN_DT", "MTG_PAY_DELAY", "DAY_CNT_DES"]

    cusip_or_isin = cusip or isin
    for bbg_type in ['Mtge', 'Corp']:
        bbg_id = cusip_or_isin + ' ' + bbg_type
        data = retrieve_data(session, [bbg_id], fields + fields_dict[bbg_type],
                             overrides={'SETTLE_DT': settle_date} if settle_date else None)
        if data[bbg_id]:
            break
    else:
        logging.error('{0} not in bloomberg'.format(cusip_or_isin))
        return

    bbg_data = data[bbg_id]
    if bbg_data.get('MTG_FACTOR_SET_DT', 0) == 0:
        bbg_data['MTG_FACTOR_SET_DT'] = 1
        bbg_data['INT_ACC'] = 0
    if len(fields) > 2: #we don't have the data in the securities table
        sql_fields = ['identifier', 'cusip', 'isin', 'description', 'face_amount',
                      'maturity', 'floater', 'spread', 'coupon', 'frequency',
                      'day_count', 'first_coupon_date', 'pay_delay', 'currency',
                      'bbg_type', 'asset_class', 'start_accrued_date']
        sqlstr = "INSERT INTO securities({0}) VALUES({1})".format(",".join(sql_fields),
                                                                  ",".join(["%s"]*17))
        isfloater =  bbg_data['FLOATER'] == 'Y'
        pay_delay = bbg_data.get('MTG_PAY_DELAY', 0)
        day_count = bbg_data.get('DAY_CNT_DES')
        m = re.match("[^(\s]+", day_count)
        if m:
            day_count = m.group(0)
        if isinstance(pay_delay, str):
            pay_delay = int(pay_delay.split(' ')[0])
        with conn.cursor() as c:
            c.execute(sqlstr, (identifier, cusip, isin, bbg_data['NAME'],
                               bbg_data.get('MTG_FACE_AMT') or bbg_data.get('AMT_ISSUED'),
                               bbg_data.get('MATURITY'), isfloater,
                               bbg_data.get('FLT_SPREAD') if isfloater else None,
                               bbg_data.get('CPN') if not isfloater else None,
                               bbg_data.get('CPN_FREQ'), day_count,
                               bbg_data.get('FIRST_CPN_DT'), pay_delay,
                               bbg_data.get('CRNCY'), bbg_type, asset_class,
                               bbg_data.get('START_ACC_DT') or bbg_data.get('PREV_CPN_DT')))
        conn.commit()
    return bbg_data

def bond_trade_process(conn, session, trade):
    bbg_data = get_bbg_data(conn, session, **trade)
    currentface = trade['faceamount'] * bbg_data['MTG_FACTOR_SET_DT']
    accrued_payment = bbg_data['INT_ACC'] * currentface /100.
    principal_payment = currentface * trade['price'] / 100.
    with conn:
        with conn.cursor() as c:
            c.execute("UPDATE bonds SET principal_payment = %s, accrued_payment = %s "
                      "WHERE id = %s", (principal_payment, accrued_payment, int(trade['id'])))
    #mark it at buy price
    if trade['buysell']:
        sqlstr = "INSERT INTO marks VALUES(%s, %s, %s)"
        try:
            with conn:
                with conn.cursor() as c:
                    c.execute(sqlstr, (trade['trade_date'], trade['identifier'], trade['price']))
        except psycopg2.IntegrityError:
            logging.error('We already have a mark')
            conn.rollback()

    # send out email with trade content
    email = EmailMessage(print_trade(trade))
    email['to'] = 'nyops@lmcg.com'
    email['subject'] = email_subject(trade)
    email.send()

def cds_trade_process(serenitasdb, dawndb, session, trade):
    sqlstr = 'SELECT indexfactor/100 FROM index_version WHERE redindexcode=%(security_id)s'
    try:
        with serenitasdb:
            with serenitasdb.cursor() as c:
                c.execute(sqlstr, trade)
                factor, = c.fetchone()
    except ValueError:
        bbg_data = get_bbg_data(dawndb, session, trade['security_id'], isin=trade['security_id'],
                                asset_class='Subprime')

        factor = bbg_data['MTG_FACTOR_SET_DT']
    trade['curr_notional'] = trade['notional'] * factor
    return trade

def wire_process(dawndb, trade):
    sql_str = "SELECT cash_account, custodian FROM accounts WHERE code = %(code)s"
    with dawndb.cursor() as c:
        c.execute(sql_str, trade)
        trade['cashaccount'], trade['custodian'] = c.fetchone()

def generate_csv(l, queue_name='bond_trades'):
    output = StringIO()
    csvwriter = csv.writer(output)
    csvwriter.writerow(HEADERS[queue_name])
    for trade in l:
        csvwriter.writerow(build_line(trade.copy(), queue_name))
    if sys.version_info.major == 3:
        return output.getvalue().encode()
    else:
        return output.getvalue()

def get_filename(timestamp, queue_name):
    d = {'bond_trades': 'Mortgages',
         'cds_trades':'CreditDefaultSwapDeal',
         'swaption_trades': 'SwaptionDeal',
         'future_trades': 'Future',
         'wires': 'CashFlowDeal'}
    return 'Serenitas.ALL.{0:%Y%m%d.%H%M%S}.{1}.csv'.format(timestamp, d[queue_name])

def upload_file(timestamp, queue_name='bond_trades'):
    ftp = FTP('ftp.globeop.com')
    ftp.login('srntsftp', config.ftp_password)
    ftp.cwd('incoming')
    filename = get_filename(timestamp, queue_name)
    cmd = 'STOR {0}'.format(filename)
    try:
        with open(os.path.join(os.environ['DAILY_DIR'], str(timestamp.date()), filename), 'rb') as fh:
            ftp.storbinary(cmd, fh)
    except KeyError:
        logging.error("Please set daily directory in DAILY_DIR")

def write_buffer(buf, queue_name='bond_trades'):
    timestamp = datetime.datetime.now()
    filename = get_filename(timestamp, queue_name)
    try:
        with open(os.path.join(os.environ['DAILY_DIR'], str(timestamp.date()), filename), 'wb') as fh:
            fh.write(buf)
        return timestamp
    except KeyError:
        logging.error("Please set daily directory in DAILY_DIR")

def email_subject(trade):
    return "[{0}] {1} {2} {3}".format(trade['asset_class'], trade['action'],
                                      "Buy" if trade['buysell'] else "Sell",
                                      trade['description'])
def print_trade(trade):
    d = trade.copy()
    d['buysell'] = "Buy" if d["buysell"] else "Sell"
    return tabulate((k, v) for k, v in d.items())

if __name__=="__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-n", "--no-upload", action="store_true", help="do not upload to Globeop")
    args = parser.parse_args()
    q = get_redis_queue()
    serenitasdb = dbconn('serenitasdb')
    dawndb = dbconn('dawndb')
    for queue_name in ['bond_trades', 'cds_trades', 'swaption_trades', 'future_trades', 'wires']:
        list_trades = get_trades(q, queue_name)
        if list_trades:
            if queue_name == 'bond_trades':
                with init_bbg_session(BBG_IP) as session:
                    for trade in list_trades:
                        bond_trade_process(dawndb, session, trade)
            elif queue_name == 'cds_trades':
                with init_bbg_session(BBG_IP) as session:
                    for trade in list_trades:
                        cds_trade_process(serenitasdb, dawndb, session, trade)
            elif queue_name == 'wires':
                for trade in list_trades:
                    wire_process(dawndb, trade)

            buf = generate_csv(list_trades, queue_name)
            timestamp = write_buffer(buf, queue_name)
            if not args.no_upload:
                upload_file(timestamp, queue_name)
            q.delete(queue_name)
    serenitasdb.close()
    dawndb.close()