aboutsummaryrefslogtreecommitdiffstats
path: root/python/ops/file_gen.py
blob: 1986ba0d67d4a044d78e1575de52920ceb3110ba (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
import datetime
from serenitas.utils.misc import rename_keys

from pyisda.date import previous_twentieth
from quantlib.time.api import pydate_from_qldate, UnitedStates, Days, Date
from serenitas.analytics.dates import bus_day
from .headers import get_headers


def get_effective_date(d, swaption_type):
    if swaption_type == "CD_INDEX_OPTION":
        return previous_twentieth(d + datetime.timedelta(days=1))
    else:
        cal = UnitedStates()
        return pydate_from_qldate(cal.advance(Date.from_datetime(d), 2, Days))


_client_name = {"SERCGMAST": "Serenitas", "BOWDST": "HEDGEMARK", "BRINKER": "LMCG"}


def build_line(obj, trade_type="bond", fund="SERCGMAST"):
    obj["Client"] = _client_name[fund]
    # Bowdst Globeop has a special short name
    obj["fund"] = "BOS_PAT_BOWDOIN" if fund == "BOWDST" else fund
    obj["State"] = "Valid"
    rename_cols = {
        "fund": "Fund",
        "action": "Action",
        "dealid": "Deal Id",
        "folder": "Folder",
        "custodian": "Custodian",
        "cash_account": "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",
        "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",
        "orig_attach": "AttachmentPoint",
        "orig_detach": "ExhaustionPoint",
        "clearing_facility": "Clearing Facility",
        "isda_definition": "ISDADefinition",
        "expiration_date": "ExpirationDate",
        "portfolio": "Portfolio",
        "settlement_type": "SettlementMode",
        "principal_payment": "PrincipalPayment",
        "accrued_payment": "AccruedPayment",
        "current_face": "CurrentFace",
    }
    rename_cols[
        "curr_notional" if fund in ("SERCGMAST", "BOWDST") else "notional"
    ] = "Notional"
    rename_keys(obj, rename_cols)
    if trade_type in ("bond", "swaption", "future"):
        obj["Transaction Indicator"] = "Buy" if obj["buysell"] else "Sell"
    if trade_type == "bond":
        obj["Deal Type"] = "MortgageDeal"
        obj["Portfolio"] = "MORTGAGES"
        obj["Delivery"] = "S"
        # zero coupon bond
        if obj["CUSIP"] != obj["GlopeOp Security Identifier"]:
            obj["CUSIP"] = None
    elif trade_type == "swaption":
        obj["Deal Type"] = "SwaptionDeal"
        obj["ExerciseType"] = "European"
        rename_keys(
            obj,
            {
                "Settlement Date": "PremiumSettlementDate",
                "notional": "Notional",
                "initial_margin_percentage": "InitialMarginPercentage",
            },
        )
        obj["PremiumSettlementAmount"] = (
            obj["Price"] * obj["Notional"] * obj.get("factor", 1.0) * 0.01
        )
        obj["PremiumSettlementCurrency"] = obj["Currency"]
        obj["RegenerateCashFlow"] = "N"
        for direction in ["Pay", "Receive"]:
            obj[direction + "MaturityDate"] = obj["MaturityDate"]
            obj[direction + "Currency"] = obj["Currency"]
            obj[direction + "Notional"] = obj["Notional"]
            obj[direction + "EffectiveDate"] = get_effective_date(
                obj["ExpirationDate"], obj["SwapType"]
            )
        if obj["SwapType"] == "CD_INDEX_OPTION":
            for direction in ["Pay", "Receive"]:
                obj[direction + "Daycount"] = "ACT/360"
                obj[direction + "Frequency"] = "Quarterly"
                obj[direction + "PaymentRollConvention"] = "Following"

            for leg_type in ["Receive", "Pay"]:
                obj[leg_type + "LegRateType"] = "Fixed"
            if obj["option_type"] == "PAYER":
                obj["ReceiveFixedRate"] = 0.0
                obj["PayFixedRate"] = obj["FixedRate"]
            elif obj["option_type"] == "RECEIVER":
                obj["PayFixedRate"] = 0.0
                obj["ReceiveFixedRate"] = obj["FixedRate"]
        elif obj["SwapType"] == "SWAPTION":
            for direction in ["Pay", "Receive"]:
                obj[direction + "PaymentRollConvention"] = "ModifiedFollowing"
            if obj["option_type"] == "RECEIVER":
                fixed, floating = "Receive", "Pay"
            else:
                fixed, floating = "Pay", "Receive"
            # fixed leg
            obj[fixed + "Frequency"] = "Yearly"
            obj[fixed + "Daycount"] = "ACT/360"
            obj[fixed + "FixedRate"] = obj["strike"]
            obj[fixed + "LegRateType"] = "Fixed"
            obj[fixed + "InterestCalcMethod"] = "Simple Interest"
            # floating leg
            obj[floating + "Frequency"] = "Yearly"
            obj[floating + "Daycount"] = "ACT/360"
            obj[floating + "LegRateType"] = "Float"
            obj[floating + "FloatRate"] = "SOFRINDX"
            obj[floating + "InterestCalcMethod"] = "Simple Interest"

        else:
            raise ValueError(
                "'SwapType' needs to be one of 'CD_INDEX_OPTION' or 'SWAPTION'"
            )

        obj["PremiumCurrency"] = obj["Currency"]
        if obj["InitialMarginPercentage"]:
            obj["InitialMarginCurrency"] = obj["Currency"]
        obj["UnderlyingInstrument"] = obj.pop("UnderlyingSecurityId")
        if obj["SwapType"] == "CD_INDEX_OPTION":
            obj["Strike"] = obj.pop("strike")

    elif trade_type == "cds":
        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"]
        if obj["Clearing Facility"] is None:
            obj["Clearing Facility"] = "NOT CLEARED"

    elif trade_type == "future":
        obj["Deal Type"] = "FutureDeal"
        rename_keys(
            obj,
            {
                "commission": "Commission",
                "quantity": "Quantity",
                "swap_type": "Swap Type",
                "bbg_ticker": "Bloomberg Ticker",
                "Currency": "Trade Currency",
                "exchange": "Exchange",
            },
        )
    elif trade_type == "wire":
        obj["Deal Type"] = "CashFlowDeal"
        obj["Transaction Type"] = "Transfer"
        obj["Instrument Type"] = "Cashflow"
        obj["Settlement Date"] = obj["Trade Date"]
        strat_portfolio_map = {
            "IGOPTDEL": "OPTIONS",
            "COCSH": "OPTIONS",
            "IGINX": "TRANCHE",
            "BSPK": "TRANCHE",
            "TCSH": "TRANCHE",
            "SER_ITRXCURVE": "SERG__CURVE",
            "XCURVE": "SERG__CURVE",
            "M_CSH_CASH": "CASH",
            "CVECSH": "SERG__CURVE",
            "SER_ITRXCVCSH": "SERG__CURVE",
        }
        obj["Portfolio"] = strat_portfolio_map.get(obj["Folder"])
        rename_keys(obj, {"amount": "Amount"})

    elif trade_type == "spot":
        standard_settle = (obj["Trade Date"] + 2 * bus_day).date()
        if obj["Settlement Date"] > standard_settle:
            obj["Deal Type"] = "ForwardDeal"
            fx_rate = "Forward Rate"
        else:
            obj["Deal Type"] = "SpotDeal"
            fx_rate = "Spot Rate"
        rename_keys(
            obj,
            {
                "commission": "Commission",
                "commission_currency": "Commission Currency",
                "sell_currency": "Sell Currency",
                "sell_amount": "Sell Amount",
                "buy_currency": "Buy Currency",
                "buy_amount": "Buy Amount",
                "spot_rate": fx_rate,
            },
        )
    elif trade_type == "fx_swap":
        obj["Deal Type"] = "FxSwapDeal"
        obj["Action"] = "NEW"
        rename_keys(
            obj,
            {
                "near_rate": "Near Side Currency Rate",
                "near_settle_date": "Near Side Settlement Date",
                "near_buy_currency": "Near Side Buy Currency",
                "near_buy_amount": "Near Side Buy Amount",
                "near_sell_currency": "Near Side Sell Currency",
                "near_sell_amount": "Near Side Sell Amount",
                "far_rate": "Far Side Rate",
                "far_settle_date": "Far Side Settlement Date",
                "far_buy_currency": "Far Side Buy Currency",
                "far_buy_amount": "Far Side Buy Amount",
                "far_sell_currency": "Far Side Sell Currency",
                "far_sell_amount": "Far Side Sell Amount",
            },
        )
    elif trade_type == "repo":
        obj["Deal Type"] = "RepoDeal"
        obj["OpenRepo"] = "Y" if obj["open_repo"] else "N"
        rename_keys(
            obj,
            {
                "weighted_amount": "WeightedAmount",
                "repo_rate": "RepoRate",
                "transaction_indicator": "TransactionIndicator",
            },
        )
    elif trade_type == "capfloor":
        obj["Deal Type"] = "CapFloorDeal"
        obj["PaymentBDC"] = obj["bdc_convention"]
        obj["AccrualBDC"] = obj["bdc_convention"]
        obj["MaturityBDC"] = "NONE"
        obj["TransactionIndicator"] = "Buy" if obj["buysell"] else "Sell"
        # missing data : 'Adjusted',  'RollConvention', 'Calendar', 'Arrears', 'Collateralized', 'MaturityBDC'
        rename_keys(
            obj,
            {
                "comments": "Comments",
                "floating_rate_index_desc": "FloatingRateIndex",
                "cap_or_floor": "CapOrFloor",
                "amount": "Notional",
                "strike": "Strike",
                "value_date": "ValueDate",
                "expiration_date": "MaturityDate",
                "premium_percent": "PremiumPercent",
                "pricing_type": "PricingType",
                "payment_frequency": "PaymentFrequency",
                "fixing_frequency": "FixingFrequency",
                "day_count_convention": "Basis",
                "bdc_convention": "PaymentBDC",
                "payment_mode": "PaymentMode",
                "payment_at_beginning_or_end": "PaymentAtBeginningOrEnd",
                "initial_margin_percentage": "InitialMarginPercentage",
                "intial_margin_currency": "InitialMarginCurrency",
                "reset_lag": "ResetLag",
                "swap_type": "SwapType",
            },
        )
    return obj