diff options
| -rw-r--r-- | python/reallocate_cash.py | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/python/reallocate_cash.py b/python/reallocate_cash.py index 2b85ce2e..fc218c3d 100644 --- a/python/reallocate_cash.py +++ b/python/reallocate_cash.py @@ -25,7 +25,7 @@ _custodian = {"GOLDNY": "GS"} _cash_account = {"GOLDNY": "057363418ICE-CDS"} -def iam_process(obj, action, trade_date): +def iam_serialize(obj, action, trade_date): rename_keys( obj, { @@ -61,7 +61,7 @@ def iam_process(obj, action, trade_date): return obj -def offset_helper(totals, trade_date): +def gen_offset(totals, trade_date): offsets = [ (trade_date, "NEW", "CSH_CASH", broker, None, -amount, "USD", True) for broker, amount in totals.items() @@ -70,7 +70,7 @@ def offset_helper(totals, trade_date): def get_strategy_alloc(conn, trade_date): - iam_sql_data = [] + new_iam = [] totals = defaultdict(int) # Need to figure out how much to offset with conn.cursor() as c: strategy_allocation = ( @@ -80,7 +80,7 @@ def get_strategy_alloc(conn, trade_date): ) c.execute(strategy_allocation, (trade_date,)) for row in c: - iam_sql_data.append( + new_iam.append( ( trade_date, "NEW", @@ -94,12 +94,13 @@ def get_strategy_alloc(conn, trade_date): ) if row.broker != "GOLDNY": # HM doesn't book FCM, no need to offset totals[_brokers[row.broker]] += row.amount - iam_sql_data += offset_helper(totals, trade_date) - return iam_sql_data + new_iam += gen_offset(totals, trade_date) + return new_iam def process_trades(conn, trade_date): - csv_lines = [] + # We will be grabbing the IAM deals from the DB + deals = [] actions = { "NEW": ( "UPDATE iam_tickets set uploaded=True where maturity is null and trade_date =%s and trade_date =%s " @@ -113,12 +114,12 @@ def process_trades(conn, trade_date): with conn.cursor() as c: c.execute(query, (trade_date, trade_date)) for row in c: - csv_lines.append(iam_process(row._asdict(), action, trade_date)) - return csv_lines + deals.append(iam_serialize(row._asdict(), action, trade_date)) + return deals def insert_iam_sql(conn, trade_date): - new_trades = get_strategy_alloc(conn, trade_date) + new_iam = get_strategy_alloc(conn, trade_date) iam_deals = [] with conn.cursor() as c: insert_query = ( @@ -126,21 +127,23 @@ def insert_iam_sql(conn, trade_date): """VALUES (%s, %s, %s, %s, %s, %s, %s, %s);""" ) try: - c.executemany(insert_query, new_trades) + c.executemany(insert_query, new_iam) except UniqueViolation: # We already uploaded the IAM tickets today in that case, we need to update and cancel the old uploads conn.rollback() c.execute( "DELETE FROM iam_tickets where trade_date=%s returning *", (trade_date,) ) - for row in c: - iam_deals.append(iam_process(row._asdict(), "CANCEL", trade_date)) - c.executemany(insert_query, new_trades) + cancel_iam_deals = [ + iam_serialize(row._asdict(), "CANCEL", trade_date) for row in c + ] + iam_deals += cancel_iam_deals + c.executemany(insert_query, new_iam) iam_deals += process_trades(conn, trade_date) return iam_deals -def process_upload(conn, trade_date, iam_deals, upload=True): +def process_upload(conn, trade_date, iam_deals): buf = StringIO() csvwriter = csv.writer(buf) csvwriter.writerow(columns) @@ -152,10 +155,9 @@ def process_upload(conn, trade_date, iam_deals, upload=True): / f"Bowdst.ALL.{datetime.datetime.now():%Y%m%d.%H%M%S}.IamDeal.csv" ) dest.write_bytes(buf) - if upload: - sftp = SftpClient.from_creds("hm_globeop") - sftp.client.chdir("incoming") - sftp.put(buf, dest.name) + sftp = SftpClient.from_creds("hm_globeop") + sftp.client.chdir("incoming") + sftp.put(buf, dest.name) if __name__ == "__main__": @@ -171,7 +173,6 @@ if __name__ == "__main__": default=(datetime.date.today() - bus_day).date(), ) args = parser.parse_args() - iam_deals = insert_iam_sql(conn, args.date) process_upload(conn, args.date, iam_deals, True) |
