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
|
from serenitas.utils.db import dbconn
import datetime
import csv
from io import StringIO
from pathlib import Path
from process_queue import rename_keys
def process_upload(trades, asset_type):
buf = StringIO()
csvwriter = csv.writer(buf)
csvwriter.writerow(HEADERS[asset_type])
csvwriter.writerows(build_line(trade, asset_type) for trade in trades)
buf = buf.getvalue().encode()
dest = Path('/home/serenitas/flint/test.csv')
dest.write_bytes(buf)
def build_line(obj, asset_type):
return [obj.get(h, None) for h in HEADERS[asset_type]]
#variables
date = datetime.date(2021,10,28)
dawndb = dbconn('dawndb')
HEADERS = {
"bond":
[
"AccountNumber",
"COB Date",
"Prime Broker",
"SecurityType",
"CUSIP",
"ISIN",
"SEDOL",
"SecurityDescription",
"Position",
"MarketPrice",
"Currency",
"Base Market Value",
"Local Market Value",
"Fx Rate"
],
}
with dawndb.cursor() as c:
c.execute("SELECT * FROM risk_positions(%s, null, 'BOWDST') ", (date,))
trades = []
for row in c:
obj = row._asdict()
rename_keys(obj, {
'identifier': 'CUSIP',
'description': 'SecurityDescription',
'notional': 'Position',
'price': 'MarketPrice',
'local_market_value': 'Local Market Value',
'usd_market_value': 'Base Market Value',
})
try:
obj['Fx Rate'] = obj['Local Market Value'] / obj['Base Market Value']
except ZeroDivisionError:
obj['Fx Rate'] = 1
obj['AccountNumber'] = "TELHEEACPB"
obj['Prime Broker'] = 'TEST'
obj['COB Date'] = date
trades.append(obj)
process_upload(trades, 'bond')
|