Created
December 13, 2023 15:57
-
-
Save DavidVujic/514187fd03e85e7c3cc4df94fe512f7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example action | |
action = { | |
"id": "", | |
"buy": {"amount": "", "currency": ""}, | |
"sell": {"amount": "", "currency": ""}, | |
"state": "", | |
} | |
# alternate implementation: Pydantic or dataclasses | |
from functools import reduce | |
import itertools | |
# example model | |
trade1 = {"id": "123456", "action": "sell", "amount": "10.00", "currency": "SEK"} | |
trade2 = {"id": "123456", "action": "buy", "amount": "15.00", "currency": "SEK"} | |
def get_value(trade: dict) -> dict: | |
amount = float(trade["amount"]) | |
return amount if trade["action"] == "buy" else -amount | |
groups = itertools.groupby([trade1, trade2], key=lambda x: x["currency"]) | |
totals = {} | |
for k, v in groups: | |
trades = list(v) | |
totals[k] = [get_value(trade) for trade in trades] | |
sum(totals["SEK"]) | |
# [get_value(i) for i in list(v)] | |
def aggregate_trades(trades: list[dict]) -> dict: | |
return {"SEK": {"value": ""}, "USD": {"value": ""}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment