diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/strat_cash_realloc.py | 79 |
1 files changed, 28 insertions, 51 deletions
diff --git a/python/strat_cash_realloc.py b/python/strat_cash_realloc.py index 2bfc9f15..5a670ba3 100644 --- a/python/strat_cash_realloc.py +++ b/python/strat_cash_realloc.py @@ -1,68 +1,45 @@ from serenitas.utils.db import dbconn from serenitas.ops.funds import Bowdst +from serenitas.ops.trade_dataclasses import WireDeal import datetime from serenitas.analytics.dates import prev_business_day from collateral.common import CASH_STRATEGY_MAPPING, STRATEGY_CASH_MAPPING from serenitas.utils.misc import rename_keys -def generate_csv(date, conn): - with conn.cursor() as c, conn.cursor() as d: +def generate_csv(date, conn, fund="BOWDST"): + wire_deals = [] + with conn.cursor() as c: c.execute( - "SELECT vr.*, accounts2.custodian, accounts2.cp_code, pfm.folder AS dirty_strat FROM (SELECT sum(endqty) AS endqty, port, strat, custacctname, invid FROM valuation_reports v WHERE periodenddate =%s AND fund='BOWDST' AND invid IN ('USD', 'EUR') AND port NOT IN ('GFS_HELPER_BUSINESS_UNIT', 'CASH') GROUP BY (port, strat,custacctname, invid) HAVING sum(endqty) !=0) vr LEFT JOIN accounts2 ON custacctname=cash_account LEFT JOIN portfolio_folder_mapping pfm ON vr.strat::text=pfm.clean_folder where strat is not NULL;", - (date,), + "SELECT count(*) FROM wires WHERE trade_date=%s AND fund=%s AND author='auto';", + ( + date, + fund, + ), + ) + (count,) = c.fetchone() + c.execute( + "SELECT * FROM list_orphaned_cash(%s, %s) ", + ( + date, + fund, + ), ) for row in c: if row.strat not in CASH_STRATEGY_MAPPING: - d.execute( - "INSERT INTO strat_cash_realloc (portfolio, folder, trade_date, amount, currency, fund, cash_account) VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING dealid", - ( - row.port, - row.strat, - date, - row.endqty, - row.invid, - "BOWDST", - row.custacctname, - ), - ) - (dealid,) = d.fetchone() - obj = row._asdict() - obj["dirty_strat"] = row.dirty_strat if row.dirty_strat else row.strat - rename_keys( - obj, - { - "invid": "Currency", - "custacctname": "Cash Account", - "custodian": "Custodian", - "cp_code": "Counterparty", - "dirty_strat": "Folder", - }, - ) - data = { - "Deal Type": "CashFlowDeal", - "Deal Id": dealid, - "Action": "NEW", - "Client": "HEDGEMARK", - "Fund": "BOS_PAT_BOWDOIN", - "State": "Valid", - "Trade Date": date, - "Settlement Date": date, - "Transaction Type": "Transfer", - "Instrument Type": "Cashflow", - "Amount": -obj["endqty"], - } - obj.update(data) - offset = obj.copy() - # create second leg - offset["Deal Id"] = obj["Deal Id"] + "_O" - offset["Amount"] = -obj["Amount"] - offset["Folder"] = STRATEGY_CASH_MAPPING[obj["Folder"]] - Bowdst.staging_queue.extend([obj, offset]) - conn.commit() - if Bowdst.staging_queue: + obj = WireDeal.from_db_line(row._asdict()) + offset = obj.offset(STRATEGY_CASH_MAPPING[obj.folder]) + obj.stage() + offset.stage() + wire_deals.extend([obj, offset]) + if wire_deals: + WireDeal.commit() + for wire in wire_deals: + wire.set_id() + Bowdst.staging_queue.append(wire.to_globeop("NEW")) buf, dest = Bowdst.build_buffer("wire") Bowdst.upload(buf, dest.name) + Bowdst().clear() if __name__ == "__main__": |
