diff options
| -rw-r--r-- | python/process_queue.py | 79 |
1 files changed, 55 insertions, 24 deletions
diff --git a/python/process_queue.py b/python/process_queue.py index 9b6566dc..439b2b36 100644 --- a/python/process_queue.py +++ b/python/process_queue.py @@ -15,6 +15,7 @@ from pickle import loads from ftplib import FTP from bbg_helpers import init_bbg_session, retrieve_data, BBG_IP from common import get_redis_queue +from pandas.tseries.offsets import BDay from pyisda.date import previous_twentieth from db import dbconn from send_email import EmailMessage @@ -117,12 +118,14 @@ def get_trades(q, queue_name='bond_trades'): 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_termination(obj): headers = ['DealType', 'DealId', 'Action', 'Client', 'SubAction', 'PartialTermination', 'TerminationAmount', 'TerminationDate', 'FeesPaid', 'FeesReceived', @@ -131,6 +134,7 @@ def build_termination(obj): ['GoTradeId'] + ['Reserved'] * 8 + ['InMoney', 'FeeCurrency'] return ['SwaptionDeal', obj['dealid'], 'Update', 'Serenitas', 'Termination'] + def build_line(obj, queue_name='bond_trades'): obj['Client'] = 'Serenitas' obj['Fund'] = 'SERCGMAST' @@ -163,11 +167,13 @@ def build_line(obj, queue_name='bond_trades'): 'upfront': 'UpfrontFee', 'upfront_settle_date': 'UpfrontFeePayDate', 'swap_type': 'SwapType', - 'attach':'AttachmentPoint', - 'detach':'ExhaustionPoint', + 'attach': 'AttachmentPoint', + 'detach': 'ExhaustionPoint', 'clearing_facility': 'Clearing Facility', 'isda_definition': 'ISDADefinition', - 'expiration_date': 'ExpirationDate'} + 'expiration_date': 'ExpirationDate', + 'portfolio': 'Portfolio', + 'settlement_type': 'SettlementMode'} rename_keys(obj, rename_cols) if queue_name in ['bond_trades', 'swaption_trades', 'future_trades']: @@ -181,9 +187,7 @@ def build_line(obj, queue_name='bond_trades'): 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', @@ -191,29 +195,56 @@ def build_line(obj, queue_name='bond_trades'): 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']) + if obj['SwapType'] == 'CD_INDEX_OPTION': + for direction in ['Pay', 'Receive']: + obj[direction + 'Daycount'] = 'ACT/360' + obj[direction + 'Frequency'] = 'Quarterly' + obj[direction + 'PaymentRollConvention'] = 'Following' + obj[direction + 'EffectiveDate'] = get_effective_date(obj['Trade Date']) + if obj['option_type'] == 'PAYER': + obj['ReceiveLegRateType'] = 'Float' + obj['ReceiveFloatRate'] = 'US0003M' + obj['PayLegRateType'] = 'Fixed' + obj['PayFixedRate'] = obj['FixedRate'] + elif obj['option_type'] == 'RECEIVER': + obj['PayLegRateType'] = 'Float' + obj['PayFloatRate'] = 'US0003M' + obj['ReceiveLegRateType'] = 'Fixed' + obj['ReceiveFixedRate'] = obj['FixedRate'] + else: + for direction in ['Pay', 'Receive']: + obj[direction + 'EffectiveDate'] = (obj['ExpirationDate'] + 2 * BDay()).date() + obj[direction + 'PaymentRollConvention'] = 'ModifiedFollowing' + if (obj['buysell'] and obj['option_type'] == 'RECEIVER') or\ + (not obj['buysell'] and obj['option_type'] == 'PAYER'): + obj['ReceiveFrequency'] = 'Half-Yearly' + obj['ReceiveDaycount'] = '30/360' + obj['PayFrequency'] = 'Quarterly' + obj['PayDaycount'] = 'ACT/360' + obj['ReceiveFixedRate'] = obj['strike'] + obj['ReceiveLegRateType'] = 'Fixed' + obj['PayLegRateType'] = 'Float' + obj['PayFloatRate'] = 'US0003M' + else: + obj['ReceiveFrequency'] = 'Quarterly' + obj['ReceiveDaycount'] = 'ACT/360' + obj['PayFrequency'] = 'Half-Yearly' + obj['PayDaycount'] = '30/360' + obj['ReceiveFloatRate'] = 'US0003M' + obj['ReceiveLegRateType'] = 'Float' + obj['PayLegRateType'] = 'Fixed' + obj['PayFixedRate'] = obj['strike'] + 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'] + if obj['SwapType'] == 'CD_INDEX_OPTION': + obj['Strike'] = obj.pop('strike') + elif queue_name == 'cds_trades': freq = {4: 'Quarterly', 12: 'Monthly'} obj['Deal Type'] = 'CreditDefaultSwapDeal' @@ -223,7 +254,7 @@ def build_line(obj, queue_name='bond_trades'): obj['InitialMarginCurrency'] = obj['Currency'] elif queue_name == 'future_trades': obj['Deal Type'] = 'FutureDeal' - rename_keys(obj, {'currency':'Trade Currency', + rename_keys(obj, {'currency': 'Trade Currency', 'commission': 'Commission', 'quantity': 'Quantity', 'swap_type': 'Swap Type', @@ -239,8 +270,8 @@ def build_line(obj, queue_name='bond_trades'): 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): +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"]} |
