aboutsummaryrefslogtreecommitdiffstats
path: root/python/position_file_bowdst.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/position_file_bowdst.py')
-rw-r--r--python/position_file_bowdst.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/python/position_file_bowdst.py b/python/position_file_bowdst.py
new file mode 100644
index 00000000..534f5a9b
--- /dev/null
+++ b/python/position_file_bowdst.py
@@ -0,0 +1,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')
+