"""Execution cost of a stock order: estimates the impact and spread cost of one stock order with the model and coefficients of Almgren, Thum, Hauptmann and Li (2005), plus fill time and 1-sigma risks. Inputs: order_size (shares, 1 or more, default 50000), adv (average daily volume in shares, 1 or more, default 5000000), spread_bps (full quoted spread, 0 to 10000, default 8), daily_vol_pct (daily volatility in percent, 0.01 to 100, default 2), participation_pct (share of market volume taken while working, 0.01 to 100, default 10), latency_ms (mean order-to-market latency, 0 to 3600000, default 150), ref_price (mid price, 0.0001 to 1e9, default 100), inverse_turnover (shares outstanding / adv, 0.01 to 1e6, default 263). Returns: permanentBps, temporaryBps, realizedImpactBps, halfSpreadBps, totalBps, totalDollars, notionalUsd, durationMinutes, durationTradingDays, timingRiskBps, latencyDriftBps. mysi, Execution cost of a stock order, mysi.dev/execution-simulator, version 1, changed 2026-12-04. MIT. """ import math _EXECUTION_SIMULATOR_GAMMA = 0.314 # permanent impact, Almgren et al. 2005, section 4.3 _EXECUTION_SIMULATOR_ETA = 0.142 # temporary impact, Almgren et al. 2005, section 4.3 _EXECUTION_SIMULATOR_SESSION = 390 # minutes in a regular NYSE session, 9:30 to 16:00 def _execution_simulator_num(name, value, low, high): if isinstance(value, bool) or not isinstance(value, (int, float)) or not low <= value <= high: raise ValueError(f"{name} must be a number from {low} to {high}") return float(value) def execution_simulator(order_size=50000, adv=5000000, spread_bps=8, daily_vol_pct=2, participation_pct=10, latency_ms=150, ref_price=100, inverse_turnover=263): num = _execution_simulator_num x = num("order_size", order_size, 1, 1e12) v = num("adv", adv, 1, 1e12) spread = num("spread_bps", spread_bps, 0, 10000) sigma_bps = num("daily_vol_pct", daily_vol_pct, 0.01, 100) * 100 rate = num("participation_pct", participation_pct, 0.01, 100) / 100 latency = num("latency_ms", latency_ms, 0, 3600000) price = num("ref_price", ref_price, 0.0001, 1e9) turnover = num("inverse_turnover", inverse_turnover, 0.01, 1e6) session = _EXECUTION_SIMULATOR_SESSION permanent = _EXECUTION_SIMULATOR_GAMMA * sigma_bps * (x / v) * turnover ** 0.25 temporary = _EXECUTION_SIMULATOR_ETA * sigma_bps * rate ** 0.6 realized = permanent / 2 + temporary total = realized + spread / 2 minutes = x / (rate * v / session) return { "permanentBps": permanent, "temporaryBps": temporary, "realizedImpactBps": realized, "halfSpreadBps": spread / 2, "totalBps": total, "totalDollars": total / 10000 * x * price, "notionalUsd": x * price, "durationMinutes": minutes, "durationTradingDays": minutes / session, "timingRiskBps": sigma_bps * math.sqrt(minutes / session), "latencyDriftBps": sigma_bps * math.sqrt(latency / (session * 60000)), } EXECUTION_SIMULATOR_CASES = [ ({"order_size": 656100, "adv": 6561000, "spread_bps": 0, "daily_vol_pct": 1.57, "participation_pct": 100, "inverse_turnover": 263}, {"permanentBps": 19.8526, "temporaryBps": 22.2940, "realizedImpactBps": 32.2203}), ({"order_size": 192900, "adv": 1929000, "spread_bps": 0, "daily_vol_pct": 2.26, "participation_pct": 20, "inverse_turnover": 87}, {"permanentBps": 21.6729, "temporaryBps": 12.2184, "realizedImpactBps": 23.0549}), ({"order_size": 50000, "adv": 5000000, "spread_bps": 8, "daily_vol_pct": 2, "participation_pct": 10, "latency_ms": 150, "ref_price": 100, "inverse_turnover": 263}, {"totalBps": 12.3983, "durationMinutes": 39.0, "timingRiskBps": 63.2456}), ] def execution_simulator_check(tolerance=1e-3): for given, want in EXECUTION_SIMULATOR_CASES: got = execution_simulator(**given) for name, value in want.items(): close = abs(got[name] - value) <= tolerance * max(1, abs(value)) assert close, (name, got[name], value) return "ok" if __name__ == "__main__": import json, sys execution_simulator_check() given = dict(a.split("=", 1) for a in sys.argv[1:] if "=" in a) js = lambda v: v.lstrip("-")[:1].isdigit() or v[:1] in '[{".' or v in ("true", "false", "null") given = {k: json.loads(v) if js(v) else v for k, v in given.items()} if given: print(json.dumps(execution_simulator(**given)))