diff options
Diffstat (limited to 'python/insert_fx_id.py')
| -rw-r--r-- | python/insert_fx_id.py | 98 |
1 files changed, 47 insertions, 51 deletions
diff --git a/python/insert_fx_id.py b/python/insert_fx_id.py index 30b0548d..40053ff7 100644 --- a/python/insert_fx_id.py +++ b/python/insert_fx_id.py @@ -104,62 +104,58 @@ def get_forwards(cob): return df +def process_rows(fund, counterparty, workdate, dawn_trades, conn): + read_fun = globals()[f"read_{counterparty}"] + for row in read_fun(fund.name, workdate): + if row.cpty_id not in dawn_trades.cpty_id.values: + matching_candidates = dawn_trades.loc[ + (dawn_trades["cp_code"] == counterparty) + & (dawn_trades["fund"] == fund.value) + ] + filters = { + "trade_date": row.trade_date, + "settle_date": row.settle_date, + "buy_currency": row.buy_currency, + "sell_currency": row.sell_currency, + "buy_amount": row.buy_amount, + "sell_amount": row.sell_amount, + } + matching_candidates = matching_candidates.loc[ + (matching_candidates[list(filters.keys())] == pd.Series(filters)).all( + axis=1 + ) + ] + if not matching_candidates.empty: + matched_candidate = matching_candidates.iloc[0, :] + with conn.cursor() as c: + if matched_candidate.fx_type == "SPOT": + table = "spots" + column = "cpty_id" + elif matched_candidate.fx_type in ("NEAR", "FAR"): + table = "fx_swaps" + column = f"{matched_candidate.fx_type.lower()}_cpty_id" + c.execute( + f"UPDATE {table} SET {column}=%s WHERE id=%s", + ( + row.cpty_id, + matched_candidate.id, + ), + ) + print(f"MATCHED {matched_candidate.id}: {table} to {row.cpty_id}") + logger.info("UPDATED %", matched_candidate.id) + conn.commit() + else: + raise ValueError(f"This is an unknown trade from us. {row.cpty_id}") + else: + pass # Will later start verifying that our trades are matched right + + def main(workdate): conn = dbconn("dawndb") dawn_trades = get_forwards(workdate) for fund in FundEnum: for counterparty in ("MSCSNY", "BAMSNY"): - read_fun = globals()[f"read_{counterparty}"] - for row in read_fun(fund.name, workdate): - if row.cpty_id not in dawn_trades.cpty_id.values: - matching_candidates = dawn_trades.loc[ - (dawn_trades["cp_code"] == counterparty) - & (dawn_trades["fund"] == fund.value) - ] - filters = { - "trade_date": row.trade_date, - "settle_date": row.settle_date, - "buy_currency": row.buy_currency, - "sell_currency": row.sell_currency, - "buy_amount": row.buy_amount, - "sell_amount": row.sell_amount, - } - matching_candidates = matching_candidates.loc[ - ( - matching_candidates[list(filters.keys())] - == pd.Series(filters) - ).all(axis=1) - ] - if not matching_candidates.empty: - matched_candidate = matching_candidates.iloc[0, :] - with conn.cursor() as c: - if matched_candidate.fx_type == "SPOT": - table = "spots" - column = "cpty_id" - elif matched_candidate.fx_type in ( - "NEAR", - "FAR", - ): - table = "fx_swaps" - column = f"{matched_candidate.fx_type.lower()}_cpty_id" - c.execute( - f"UPDATE {table} SET {column}=%s WHERE id=%s", - ( - row.cpty_id, - matched_candidate.id, - ), - ) - print( - f"MATCHED {matched_candidate.id}: {table} to {row.cpty_id}" - ) - logger.info("UPDATED %", matched_candidate.id) - conn.commit() - else: - raise ValueError( - f"This is an unknown trade from us. {row.cpty_id}" - ) - else: - pass # Will later start verifying that our trades are matched right + process_rows(fund, counterparty, workdate, dawn_trades, conn) if __name__ == "__main__": |
