Skip to content

Instantly share code, notes, and snippets.

@DavidVujic
Created December 13, 2023 15:57
Show Gist options
  • Save DavidVujic/514187fd03e85e7c3cc4df94fe512f7f to your computer and use it in GitHub Desktop.
Save DavidVujic/514187fd03e85e7c3cc4df94fe512f7f to your computer and use it in GitHub Desktop.
# 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