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
|
import os
import pandas as pd
def create_trigger_function(db):
# need to escape % with %%
auto_dealid = db.DDL("""
CREATE OR REPLACE FUNCTION auto_dealid()
RETURNS TRIGGER AS $$
DECLARE stub text;
sqlstr text;
BEGIN
sqlstr:= 'UPDATE '|| TG_TABLE_NAME ||' SET %%s WHERE id = %%L AND dealid is NULL';
IF (TG_TABLE_NAME = 'bonds') THEN
stub := 'SC_';
sqlstr := format(sqlstr, 'dealid = $1||upper(left(asset_class::text,3))||id,
identifier = COALESCE(identifier, cusip, isin)', NEW.id);
ELSE
CASE TG_TABLE_NAME
WHEN 'cds' THEN
stub := 'SCCDS';
WHEN 'repo' THEN
stub := 'SC_REP';
WHEN 'swaptions' THEN
stub := 'SWPTN';
WHEN 'futures' THEN
stub := 'SCFUT';
WHEN 'wires' THEN
stub := 'SCCSH';
WHEN 'capfloors' THEN
stub := 'CAP';
WHEN 'spots' THEN
stub := 'SCFX';
END CASE;
sqlstr := format(sqlstr, 'dealid = $1||id', NEW.id);
END IF;
EXECUTE sqlstr USING stub;
RETURN NEW;
END;
$$ language plpgsql""")
db.event.listen(db.metadata, 'before_create', auto_dealid)
def create_triggers(db):
trigger = db.DDL("""CREATE TRIGGER dealid
AFTER INSERT ON %(fullname)s
FOR EACH ROW
EXECUTE PROCEDURE auto_dealid()""")
for name, tb in db.metadata.tables.items():
if not name.endswith('counterparties') and not name.endswith('accounts'):
db.event.listen(tb, 'after_create', trigger)
def load_counterparties(engine):
counterparties = pd.read_excel("/home/share/Daily/blotter.xlsm", 'Counterparties')
counterparties[['city', 'state']] = counterparties.Location.str.split(", ", expand=True)
counterparties.drop(['Location', 'Valuation Contact4', 'Valuation Contact4 Email'], axis=1, inplace=True)
counterparties.rename(columns={'CODE': 'code',
'DTC Number': 'dtc_number',
'Email1': 'sales_email',
'FIRM': 'name',
'Phone': 'sales_phone',
'Sales Contact': 'sales_contact',
'Valuation Contact1': 'valuation_contact1',
'Valuation Contact1 Email': 'valuation_email1',
'Valuation Contact2': 'valuation_contact2',
'Valuation Contact2 Email': 'valuation_email2',
'Valuation Contact3': 'valuation_contact3',
'Valuation Contact3 Email': 'valuation_email3',
'Valuation Note': 'notes'}, inplace=True)
counterparties.to_sql('counterparties', engine, if_exists='append', index=False)
def load_trades(engine, schema=None):
blotter = pd.read_excel("/home/serenitas/Daily/Blotter.xlsm", 'Bonds',
skiprows=[0, 1, 2, 3, 4])
blotter.dropna(axis=0, subset=['Deal ID'], inplace=True)
blotter = blotter.iloc[:,2:]
blotter.drop(['Unnamed: %s' % (i,) for i in range(19, 28)] +
['Comments', 'Acc Int $', 'Counterparty'], axis=1, inplace=True)
blotter.rename(columns={'Date': 'trade_date',
'Settle Date': 'settle_date',
'Strategy': 'folder',
'Custodian': 'custodian',
'Cash Account': 'cashaccount',
'CP Alias': 'cp_code',
'CUSIP': 'cusip',
'ISIN': 'isin',
'Description': 'description',
'Buy/Sell': 'buysell',
'Notional': 'faceamount',
'Price': 'price',
'Acc Int': 'accrued',
'Asset Class': 'asset_class',
'Deal ID': 'id'}, inplace=True)
blotter.buysell = blotter.buysell.apply(lambda x: x=='Buy')
blotter['action'] = 'NEW'
blotter['cashaccount'] = 'V0NSCLMAMB'
blotter['id'] = blotter['id'].str.replace('[A-Z_]', '').astype('int')
blotter.loc[blotter.asset_class == 'CLO','id'] = blotter.loc[blotter.asset_class == 'CLO','id'] + 5
blotter.to_sql('bonds', engine, if_exists='append', index=False, schema=schema)
return blotter
def load_trades_futures(engine, schema=None):
blotter = pd.read_excel("/home/serenitas/Daily/Blotter.xlsm", 'Futures',
skiprows=[0, 1, 2, 3, 4])
blotter.dropna(axis=0, subset=['Deal ID'], inplace=True)
blotter = blotter.iloc[:,2:]
blotter.drop(['Unnamed: %s' % (i,) for i in range(25, 27)] + \
['Deal Type', 'Asset Class', 'Initial Margin Currency', 'Client', 'State'],
axis=1, inplace=True)
blotter.rename(columns = {'Trade Date': 'trade_date',
'Settlement Date': 'settle_date',
'Cash Account': 'cashaccount',
'Strategy': 'folder',
'Quantity': 'quantity',
'Commission': 'commission',
'CP Alias': 'cp_code',
'Description': 'security_desc',
'Buy/Sell': 'buysell',
'Price': 'price',
'Deal ID': 'id',
'Exchange': 'exchange',
'Ticker': 'bbg_ticker',
'Maturity': 'maturity',
'Action': 'action',
'Custodian': 'custodian',
'Trade Currency': 'currency',
'Swap Type': 'swap_type',
'Counterparty': 'cp_code'}, inplace=True)
blotter.buysell = blotter.buysell.apply(lambda x: x=='Buy')
blotter['action'] = 'NEW'
blotter['cashaccount'] = 'IANSCLMAFU'
blotter['swap_type'] = 'FUTURE'
blotter['id'] = blotter['id'].str.replace('[A-Z_]', '').astype('int')
blotter.to_sql('futures', engine, if_exists='append', index=False, schema=schema)
return blotter
def load_wires(engine, schema=None):
blotter = pd.read_excel("/home/serenitas/Daily/Blotter_gh.xlsm", 'Wires',
skiprows=[0, 1, 2, 3, 4])
blotter.dropna(axis=0, subset=['Deal ID'], inplace=True)
blotter = blotter.iloc[:,2:]
blotter.drop(['Unnamed: %s' % (i,) for i in range(19, 25)] + \
['Deal Type', 'Asset Class', 'Client', 'State', "Counterparty",
"Settlement Date", "Instrument Type", "Transaction Type",
"Custodian", "CashAccount"],
axis=1, inplace=True)
blotter.rename(columns = {'Date': 'trade_date',
'Account': 'code',
'Cash Account': 'cashaccount',
'Strategy': 'folder',
'Deal ID': 'id',
'Action': 'action',
'Currency': 'currency',
'Amount': 'amount'}, inplace=True)
blotter.folder = blotter.folder.str.rstrip()
blotter['action'] = 'NEW'
blotter['id'] = blotter['id'].str.replace('[A-Z_]', '').astype('int')
blotter.to_sql('wires', engine, if_exists='append', index=False, schema=schema)
return blotter
def load_spots(engine, schema=None):
blotter = pd.read_excel("/home/serenitas/Daily/Blotter_gh.xlsm", 'FXSpot',
skiprows=[0, 1, 2, 3, 4])
blotter.dropna(axis=0, subset=['Deal ID'], inplace=True)
blotter = blotter.iloc[:,2:]
blotter.drop(['Deal Type', 'Asset Class', 'Client', 'State', "Counterparty",
"Custodian", "Cash Account", "Account"],
axis=1, inplace=True)
blotter.rename(columns= {'Date': 'trade_date',
'Settlement Date': 'settlement_date',
'Cash Account': 'cashaccount',
'Strategy': 'folder',
'Deal ID': 'id',
'Action': 'action',
'Sell Currency': 'sell_currency',
'Sell Amount': 'sell_amount',
'Buy Currency': 'buy_currency',
'Buy Amount': 'buy_amount',
'Spot - Buy/Sold Rate': 'spot_rate',
'Commission Currency': 'commission_currency',
'commission note': 'commission'}, inplace=True)
blotter.folder = blotter.folder.str.rstrip()
blotter['action'] = 'NEW'
blotter['id'] = blotter['id'].str.replace('[A-Z_]', '').astype('int')
blotter['custodian'] = 'INTBR'
blotter['cashaccount'] = 'IANSCLMAFU'
blotter['cp_code'] = 'IBKRNY'
blotter.to_sql('spots', engine, if_exists='append', index=False, schema=schema)
return blotter
if __name__ == "__main__":
""" This script will create the tables and triggers for the Dawn app.
If schema is not None, it will create it under a specific schema"""
#conn.execute("CREATE SCHEMA IF NOT EXISTS {}".format(schema))
#conn.execute("SET search_path TO %s", (schema,))
os.environ['CONF'] = 'config.ini'
from Dawn import db, app
create_trigger_function(db)
create_triggers(db)
db.create_all()
#load_wires(db.engine, app.config['SCHEMA'])
|