Spaces:
Running
Running
Download pmct/ledger.py from sharpflowapp/web: direct link, hf CLI and curl.
- Browser
- Download file 4.07 kB
-
https://huggingface.co/spaces/sharpflowapp/web/resolve/main/pmct/ledger.py
- Command line
-
hf download hf://spaces/sharpflowapp/web/pmct/ledger.py
-
curl -L -o ledger.py https://huggingface.co/spaces/sharpflowapp/web/resolve/main/pmct/ledger.py
4.07 kB
| """Normalize a wallet's raw activity feed into a tidy trade ledger. | |
| The Data API returns loosely-typed dicts whose exact keys drift over time, so | |
| every field is read with fallbacks. The output is one DataFrame per wallet with | |
| a stable schema the rest of the pipeline relies on. | |
| Cashflow convention (from the trader's perspective): | |
| BUY -> cash out (negative) | |
| SELL -> cash in (positive) | |
| REDEEM / REWARD -> cash in (positive) # market resolved in their favor | |
| Open positions are marked to market separately in metrics.py. | |
| """ | |
| from __future__ import annotations | |
| import pandas as pd | |
| LEDGER_COLS = [ | |
| "ts", "type", "side", "market", "asset", "outcome", | |
| "size", "price", "usdc", "cashflow", "title", | |
| ] | |
| # activity types that bring cash IN (winning redemptions + protocol incentives) | |
| CASH_IN_TYPES = ("REDEEM", "CLAIM", "REWARD", "MAKER_REBATE", "TAKER_REBATE") | |
| # the uncopyable subset: liquidity rewards + maker/taker rebates (market-making) | |
| REBATE_TYPES = ("REWARD", "MAKER_REBATE", "TAKER_REBATE") | |
| def _first(d: dict, *keys, default=None): | |
| for k in keys: | |
| if k in d and d[k] not in (None, ""): | |
| return d[k] | |
| return default | |
| def _to_float(x, default=0.0) -> float: | |
| try: | |
| return float(x) | |
| except (TypeError, ValueError): | |
| return default | |
| def normalize_activity(raw: list[dict]) -> pd.DataFrame: | |
| rows = [] | |
| for a in raw: | |
| typ = str(_first(a, "type", "activityType", default="TRADE")).upper() | |
| side = str(_first(a, "side", default="")).upper() | |
| size = _to_float(_first(a, "size", "shares", "amount")) | |
| price = _to_float(_first(a, "price", "avgPrice")) | |
| usdc = _first(a, "usdcSize", "usdc", "usdcValue", default=None) | |
| usdc = _to_float(usdc) if usdc is not None else round(size * price, 6) | |
| # ts may be unix seconds or ms | |
| ts_raw = _to_float(_first(a, "timestamp", "ts", "time")) | |
| if ts_raw > 1e12: | |
| ts_raw /= 1000.0 | |
| ts = pd.to_datetime(ts_raw, unit="s", utc=True) if ts_raw else pd.NaT | |
| # Protocol incentives (liquidity rewards + maker/taker rebates) are real | |
| # income but NOT copyable directional skill — tag them so metrics can | |
| # measure how rebate-driven a wallet is (market makers = uncopyable). | |
| if typ in CASH_IN_TYPES or side == "SELL": | |
| cashflow = +usdc | |
| elif side == "BUY": | |
| cashflow = -usdc | |
| else: # unknown non-trade rows contribute no directional cashflow | |
| cashflow = 0.0 | |
| rows.append({ | |
| "ts": ts, | |
| "type": typ, | |
| "side": side, | |
| "market": _first(a, "conditionId", "condition_id", "market", default=""), | |
| "asset": _first(a, "asset", "tokenId", "token_id", default=""), | |
| "outcome": _first(a, "outcome", "outcomeIndex", default=""), | |
| "size": size, | |
| "price": price, | |
| "usdc": usdc, | |
| "cashflow": cashflow, | |
| "title": _first(a, "title", "slug", "question", default=""), | |
| }) | |
| df = pd.DataFrame(rows, columns=LEDGER_COLS) | |
| if not df.empty: | |
| df = df.dropna(subset=["ts"]).sort_values("ts").reset_index(drop=True) | |
| return df | |
| def attach_open_positions(positions: list[dict]) -> pd.DataFrame: | |
| """Current open positions -> per-market marked-to-market value & PnL.""" | |
| rows = [] | |
| for p in positions: | |
| size = _to_float(_first(p, "size", "shares")) | |
| avg = _to_float(_first(p, "avgPrice", "averagePrice")) | |
| cur = _to_float(_first(p, "curPrice", "currentPrice", "price")) | |
| rows.append({ | |
| "market": _first(p, "conditionId", "condition_id", "market", default=""), | |
| "asset": _first(p, "asset", "tokenId", default=""), | |
| "size": size, | |
| "avgPrice": avg, | |
| "curPrice": cur, | |
| "cost_basis": size * avg, | |
| "mark_value": size * cur, | |
| "cashPnl": _to_float(_first(p, "cashPnl", default=size * (cur - avg))), | |
| }) | |
| return pd.DataFrame(rows) | |