aboutsummaryrefslogtreecommitdiffstats
path: root/python/strat_cash_realloc.py
blob: c2d8b6964083c16321be27c9344ff95011d9993c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from dataclasses import replace
from serenitas.ops.funds import Service
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


def generate_csv(date, fund):
    service = Service[fund]
    with WireDeal._conn.cursor() as c:
        c.execute(
            "SELECT 1 FROM wires WHERE trade_date=%s AND fund=%s AND author='auto'",
            (
                date,
                fund,
            ),
        )
        if c.fetchone():
            return
        c.execute(
            "SELECT * FROM list_orphaned_cash(%s, %s) WHERE abs(amount) > 1",
            (
                date,
                fund,
            ),
        )
        for row in c:
            if row.folder not in CASH_STRATEGY_MAPPING:
                obj = WireDeal(**row._asdict())
                offset = replace(
                    obj, folder=STRATEGY_CASH_MAPPING[obj.folder], amount=-obj.amount
                )
                obj.stage()
                offset.stage()
    if not WireDeal._insert_queue:
        return
    for wire in WireDeal.commit(returning=True):
        service.push(wire, "NEW")
    buf, dest = service.build_buffer("wire")
    service.upload(buf, dest.name)
    service().clear()


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "workdate",
        nargs="?",
        type=datetime.date.fromisoformat,
        default=prev_business_day(datetime.date.today()),
        help="working date",
    )
    args = parser.parse_args()

    generate_csv(args.workdate, "BOWDST")