diff options
| -rw-r--r-- | python/reallocate_iam.py | 105 |
1 files changed, 52 insertions, 53 deletions
diff --git a/python/reallocate_iam.py b/python/reallocate_iam.py index d162be97..4789c239 100644 --- a/python/reallocate_iam.py +++ b/python/reallocate_iam.py @@ -1,87 +1,81 @@ import datetime +import argparse from serenitas.ops.funds import Service from serenitas.ops.trade_dataclasses import IAMDeal from serenitas.analytics.dates import prev_business_day +from serenitas.utils.db import dbconn -def gen_old_iam(fund, cob, conn): - """'Finds and cancels old IAM uploads""" - with conn.cursor() as c: - c.execute( +def cancel_old_iam_trades(fund: str, cob: datetime.date, conn) -> "Iterable": + """Finds and cancels old IAM trades""" + with conn.cursor() as cursor: + cursor.execute( "DELETE FROM iams WHERE trade_date=%s AND fund=%s RETURNING *", - ( - cob, - fund, - ), + (cob, fund), ) - for row in c: - d = row._asdict() - yield IAMDeal.from_dict(**d) + for row in cursor: + trade_data = row._asdict() + yield IAMDeal.from_dict(**trade_data) conn.commit() -def gen_new_iam(fund, cob, conn): +def generate_new_iam_trades(fund: str, cob: datetime.date, conn) -> "Iterable": """Generates new IAM deals""" - with conn.cursor() as c: - c.execute( + with conn.cursor() as cursor: + cursor.execute( "SELECT * FROM list_iam(%s, %s)", - ( - cob, - fund, - ), + (cob, fund), ) - for row in c: - d = row._asdict() | {"trade_date": cob} - yield IAMDeal.from_dict(**d) + for row in cursor: + trade_data = row._asdict() | {"trade_date": cob} + yield IAMDeal.from_dict(**trade_data) -def gen_new_iam_offsets(fund, cob, conn): - """'Generates offsets if the sma has already updated these iam deals""" +def generate_new_iam_offset_trades(fund: str, cob: datetime.date, conn) -> "Iterable": + """Generates offsets if the sma has already updated these IAM deals""" _ignore = {"BOWDST": ("GS_FCM",)} - with conn.cursor() as c: - c.execute( + with conn.cursor() as cursor: + cursor.execute( "SELECT broker, currency, fund, sum(-start_money) AS start_money " "FROM list_iam(%s, %s) GROUP BY (broker, currency, fund);", - ( - cob, - fund, - ), + (cob, fund), ) - for row in c: + for row in cursor: if row.broker not in _ignore[fund]: - d = row._asdict() | { + trade_data = row._asdict() | { "is_offset": True, "folder": "M_CSH_CASH", "trade_date": cob, } - yield IAMDeal.from_dict(**d) + yield IAMDeal.from_dict(**trade_data) -def gen_matured_iam(fund, cob, conn): +def update_matured_iam_trades(fund: str, cob: datetime.date, conn) -> "Iterable": """Sets previous days as matured""" - with conn.cursor() as c: - c.execute( + with conn.cursor() as cursor: + cursor.execute( "UPDATE iams SET maturity=%s WHERE maturity is NULL AND trade_date<=%s AND fund=%s RETURNING *", (cob, prev_business_day(cob), fund), ) - for row in c: - d = row._asdict() | {} - yield IAMDeal.from_dict(**d) + for row in cursor: + trade_data = row._asdict() | {} + yield IAMDeal.from_dict(**trade_data) conn.commit() -def build_iam(fund, cob, conn, upload): +def build_iam(fund: str, cob: datetime.date, conn, upload: bool): + """Generates IAM file for globeop""" service = Service[fund] - for old_iam in gen_old_iam(fund, cob, conn): + for old_iam in cancel_old_iam_trades(fund, cob, conn): service.push_trade(old_iam, "CANCEL") - for new_iam in gen_new_iam(fund, cob, conn): + for new_iam in generate_new_iam_trades(fund, cob, conn): new_iam.stage() - if fund in ("BOWDST"): - for new_iam_offset in gen_new_iam_offsets(fund, cob, conn): + if fund == "BOWDST": + for new_iam_offset in generate_new_iam_offset_trades(fund, cob, conn): new_iam_offset.stage() for iam in IAMDeal.commit(returning=True): service.push_trade(iam, "NEW") - for update_iam in gen_matured_iam(fund, cob, conn): + for update_iam in update_matured_iam_trades(fund, cob, conn): service.push_trade(update_iam, "UPDATE") buf, dest = service.build_buffer(trade_type="iam") if upload: @@ -89,11 +83,8 @@ def build_iam(fund, cob, conn, upload): service().clear() -if __name__ == "__main__": - import argparse - from serenitas.utils.db import dbconn - - conn = dbconn("dawndb") +def parse_args(): + """Parses command line arguments""" parser = argparse.ArgumentParser(description="Generate IAM file for globeop") parser.add_argument( "cob", @@ -101,9 +92,17 @@ if __name__ == "__main__": type=datetime.date.fromisoformat, default=prev_business_day(datetime.date.today()), ) - parser.add_argument( - "-n", "--no-upload", action="store_true", help="do not upload to CTM" - ) - args = parser.parse_args() + parser.add_argument("-n", "--no-upload", action="store_true", help="do not upload") + return parser.parse_args() + + +def main(): + """Generates IAM files for globeop""" + conn = dbconn("dawndb") + args = parse_args() for fund in ("BOWDST",): build_iam(fund, args.cob, conn, not args.no_upload) + + +if __name__ == "__main__": + main() |
