aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--python/reallocate_iam.py44
1 files changed, 23 insertions, 21 deletions
diff --git a/python/reallocate_iam.py b/python/reallocate_iam.py
index 05070ac0..e947faf6 100644
--- a/python/reallocate_iam.py
+++ b/python/reallocate_iam.py
@@ -5,9 +5,10 @@ from serenitas.analytics.dates import prev_business_day
def gen_old_iam(fund, cob, conn):
+ """'Finds and cancels old IAM uploads"""
with conn.cursor() as c:
c.execute(
- "SELECT * FROM iam_tickets WHERE trade_date=%s AND fund=%s",
+ "DELETE FROM iam_tickets WHERE trade_date=%s AND fund=%s RETURNING *",
(
cob,
fund,
@@ -15,11 +16,11 @@ def gen_old_iam(fund, cob, conn):
)
for row in c:
d = row._asdict()
- yield IAMDeal.from_dict(**d).to_globeop("CANCEL")
- c.execute("DELETE FROM iam_tickets WHERE trade_date=%s AND fund=%s")
+ yield IAMDeal.from_dict(**d)
def gen_new_iam(fund, cob, conn):
+ """Generates new IAM deals"""
with conn.cursor() as c:
c.execute(
"SELECT * FROM list_iam(%s, %s)",
@@ -30,10 +31,11 @@ def gen_new_iam(fund, cob, conn):
)
for row in c:
d = row._asdict()
- yield IAMDeal.from_dict(**d).to_globeop("NEW")
+ yield IAMDeal.from_dict(**d)
def gen_new_iam_offsets(fund, cob, conn):
+ """'Generates offsets if the sma has already updated these iam deals"""
with conn.cursor() as c:
c.execute(
"SELECT trade_date, broker, currency, fund, sum(start_money) AS start_money "
@@ -45,36 +47,36 @@ def gen_new_iam_offsets(fund, cob, conn):
)
for row in c:
d = row._asdict() | {"offset": True, "folder": "M_CSH_CASH"}
- yield IAMDeal.from_dict(**d).to_globeop("NEW")
+ yield IAMDeal.from_dict(**d)
def gen_matured_iam(fund, cob, conn):
+ """Sets previous days as matured"""
with conn.cursor() as c:
c.execute(
- "SELECT * FROM iam_tickets WHERE maturity is NULL AND trade_date=%s AND fund=%s",
- (prev_business_day(cob), fund),
+ "UPDATE iam_tickets 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:
- yield IAMDeal.from_dict(**d).to_globeop("UPDATE")
- c.execute(
- "UPDATE iam_tickets SET maturity =%s WHERE maturity is NULL AND trade_date=%s AND fund=%s",
- (cob, fund),
- )
+ yield IAMDeal.from_dict(**d)
def main(fund, cob, conn):
service = Service[fund]
- for iam in gen_old_iam(fund, cob, conn):
- iam.stage()
- for iam in gen_iam(fund, cob, conn):
- iam.stage()
+ for old_iam in gen_old_iam(fund, cob, conn):
+ service.push_trade(old_iam, "CANCEL")
+ for new_iam in gen_iam(fund, cob, conn):
+ new_iam.stage()
if fund in ("BOWDST"):
- for iam in gen_iam_offsets(fund, cob, conn):
- iam.stage()
- for iam in IamDeal.commit():
- service.push(iam)
- buf, dest = service.build_buffer()
+ for new_iam_offset in gen_iam_offsets(fund, cob, conn):
+ new_iam_offset.stage()
+ for iam in IAMDeal.commit(returning=True):
+ service.push(iam, "NEW")
+ for update_iam in gen_matured_iam(fund, cob, conn):
+ service.push(update_iam, "UPDATE")
conn.commit()
+ buf, dest = service.build_buffer()
+ service.upload(buf, dest.name)
if __name__ == "__main__":