From e090067631255ddfeb042545f62a8e95c26c7946 Mon Sep 17 00:00:00 2001 From: uvok Date: Fri, 18 Apr 2025 19:35:55 +0200 Subject: Get rid of premature sorting trade queue handles itself. Deposit and withdrawal can be handles afterwards. --- ledger_process.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'ledger_process.py') diff --git a/ledger_process.py b/ledger_process.py index d6b31fa..8b7a614 100644 --- a/ledger_process.py +++ b/ledger_process.py @@ -21,14 +21,11 @@ class LedgerProcess: # don't make any assumtptions about ledger sorting # groupby requires sorted inputs actions_sorted = sorted(read_actions, key=lambda a: (a.refid, a.timestamp)) - _grouped_actions = groupby(actions_sorted, lambda a: a.refid) - _list_tuple_actions = [(k, list(v)) for k, v in _grouped_actions] - # finally, sort groupy by first available timestamp - sorted_grouped_actions = sorted( - _list_tuple_actions, key=lambda a: a[1][0].timestamp - ) + grouped_actions = groupby(actions_sorted, lambda a: a.refid) + + process_after: List[LedgerAction] = [] - for refid, actions in sorted_grouped_actions: + for refid, actions in grouped_actions: actions = list(actions) if len(actions) == 0: @@ -40,12 +37,18 @@ class LedgerProcess: if action.type == "trade": self._process_trade(refid, actions) - elif action.type == "deposit" and action.asset != "EUR": + elif action.asset == "EUR": + continue + + elif action.type == "deposit" or action.type == "withdrawal": assert len(actions) == 1 + process_after.append(action) + + for action in sorted(process_after, key=lambda a: a.timestamp): + if action.type == "deposit" and action.asset != "EUR": self._process_deposit(action) elif action.type == "withdrawal" and action.asset != "EUR": - assert len(actions) == 1 self._process_withdrawal(action) def _process_deposit(self, action: LedgerAction): -- cgit v1.2.3