Sources (each row and copied code come solely from its named item): [f-drift-skew](../../items/f-drift-skew/result.json), [f-pit-calibration](../../items/f-pit-calibration/result.json), [f-info-order](../../items/f-info-order/result.json), [f-competing-risk](../../items/f-competing-risk/result.json), [f-flow-scales](../../items/f-flow-scales/result.json), [f-cash-order](../../items/f-cash-order/result.json). Source-verification caveat supplied with the inputs: drift-skew, PIT calibration, competing-risk and flow-scales each have one cited source that failed verification; cash-order has two. Those external claims are not relied on here. Measurements below are copied item reports, not independently replicated. The supplied item JSONs do not identify the failed passages. ```json { "rows": [ { "key": "f-drift-skew", "spreads_acted": [ "1" ], "off_equals_n": true, "code_sha256": "ef39c3c81b0a1881e544e3c2f40faf9d3401143ca906431d044ada27c53cd834", "assembled_sha256": "08cb7e9758be0000f74cc90dc9166e92645b6e599ef042abeab699afb4623f2c", "tuning": { "mean_diff": "-2.8112894160675", "se": "1.142947729493399" }, "paired": { "n": "500", "complete": true, "incumbent_mean": "104.4137531881468", "mean_diff": "-2.0238095518382", "se": "0.6009124229704581", "sum_d": "-1011.9047759191", "sum_d2": "92141.28970107235", "retail_diff": "-1.8872337652555997", "arb_diff": "-0.1365757865886", "failures": [] }, "by_spread": { "1": { "runs": "115", "mean_diff": "-8.799171964513913", "se": "2.5197521091442283", "sum_d": "-1011.9047759191", "sum_d2": "92141.28970107235" }, "2": { "runs": "116", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "3": { "runs": "138", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "4": { "runs": "131", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" } }, "flag": "" }, { "key": "f-pit-calibration", "spreads_acted": [ "2" ], "off_equals_n": true, "code_sha256": "79c32cab81d9c5d1d3938910f81600327f9fae8da58b86b4856d1c618a13fa7c", "assembled_sha256": "bbe0ab7f36618704121f91bf89bfd53d6d991548a59f8a89b692cd4c19001aee", "tuning": { "mean_diff": "-0.608981", "se": "0.174788" }, "paired": { "n": "500", "complete": true, "incumbent_mean": "104.413753", "mean_diff": "-0.874101", "se": "0.149297", "sum_d": "-437.050266", "sum_d2": "5943.284315", "retail_diff": "-1.345242", "arb_diff": "0.471141", "failures": "0" }, "by_spread": { "1": { "runs": "115", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "2": { "runs": "116", "mean_diff": "-3.767675", "se": "0.567526", "sum_d": "-437.050266", "sum_d2": "5943.284315" }, "3": { "runs": "138", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "4": { "runs": "131", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" } }, "flag": "" }, { "key": "f-info-order", "spreads_acted": [ "1" ], "off_equals_n": true, "code_sha256": "bf4f1420642286ae7f960cebe1a0f370108380ab93339880c852eafd2e3cbb1d", "assembled_sha256": "08e9c7869ef61c6d9a3a8a073b3d97056da8c59aa1c0f4c5c53775c0bb9d6621", "tuning": { "mean_diff": "0.04864326477299996", "se": "0.07000554257747216" }, "paired": { "n": "500", "complete": true, "incumbent_mean": "104.4137531881468", "mean_diff": "-0.026590906622400077", "se": "0.007696442344119877", "sum_d": "-13.295453311200038", "sum_d2": "15.132726734212786", "retail_diff": "0.011876189333000097", "arb_diff": "-0.03846709596159997", "failures": "0" }, "by_spread": { "1": { "runs": "115", "mean_diff": "-0.11561263748869599", "se": "0.03220311717728864", "sum_d": "-13.295453311200038", "sum_d2": "15.132726734212786" }, "2": { "runs": "116", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "3": { "runs": "138", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "4": { "runs": "131", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" } }, "flag": "" }, { "key": "f-competing-risk", "spreads_acted": [ "1" ], "off_equals_n": true, "code_sha256": "5b4a3006e16e3ff48ba6be73948de2de03cefc00cbaff94f24990bac6f4e2ac5", "assembled_sha256": "c3a4f8c5c6b2f0bb9756f1370bff240e406ece940dc422f69bc557c6fd5f1a0f", "tuning": { "mean_diff": "-41.394405191622496", "se": "4.73840452756909" }, "paired": { "n": "500", "complete": true, "incumbent_mean": "104.4137531881468", "mean_diff": "-5.9084120034344", "se": "1.5193631281184425", "sum_d": "-2954.2060017172", "sum_d2": "593416.5128150856", "retail_diff": "-3.2141916298837994", "arb_diff": "-2.694220373561", "failures": "0" }, "by_spread": { "1": { "runs": "115", "mean_diff": "-25.68874784101913", "se": "6.282970456311677", "sum_d": "-2954.2060017172", "sum_d2": "593416.5128150856" }, "2": { "runs": "116", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "3": { "runs": "138", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "4": { "runs": "131", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" } }, "flag": "" }, { "key": "f-flow-scales", "spreads_acted": [ "4" ], "off_equals_n": true, "code_sha256": "32f15d36aa069f24f51d668f00a8078f4a81dc646ed13252cbff516e3b16f9b7", "assembled_sha256": "18419c9594a4e685223f3d2eed57f197ccf29f36c13c02f4078e5b9f8e33599f", "tuning": { "mean_diff": "0.0015171980589997248", "se": "0.0010894502096014396" }, "paired": { "n": "500", "complete": true, "incumbent_mean": "104.4137531881468", "mean_diff": "0.08052538148639997", "se": "0.04882673466936453", "sum_d": "40.26269074319998", "sum_d2": "598.06264814066", "retail_diff": "0.07047975590079988", "arb_diff": "0.010045625584000057", "failures": [] }, "by_spread": { "1": { "runs": "115", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "2": { "runs": "116", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "3": { "runs": "138", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "4": { "runs": "131", "mean_diff": "0.3073487842992365", "se": "0.18544956708277158", "sum_d": "40.26269074319998", "sum_d2": "598.06264814066" } }, "flag": "" }, { "key": "f-cash-order", "spreads_acted": [ "1", "3", "4" ], "off_equals_n": true, "code_sha256": "429bc9c5278d27ebeda31aa430d96cc1f6f63bf44710efe5cf8f5086dfdf3547", "assembled_sha256": "409a99d41dd272616f19aa5f653e231acd7ad654f6ac7d6956103196a9d7313f", "tuning": { "mean_diff": "0.613005449284999", "se": "0.4275371719797223" }, "paired": { "n": "500", "complete": true, "incumbent_mean": "104.4137531881468", "mean_diff": "0.23052258987960023", "se": "0.3194031685810437", "sum_d": "115.26129493980011", "sum_d2": "25480.15716507524", "retail_diff": "0.27498706822460045", "arb_diff": "-0.044464478347599916", "failures": "0" }, "by_spread": { "1": { "runs": "115", "mean_diff": "-0.16040588702608619", "se": "0.43279404974813807", "sum_d": "-18.446677007999913", "sum_d2": "2458.602094898985" }, "2": { "runs": "116", "mean_diff": "0.0", "se": "0.0", "sum_d": "0.0", "sum_d2": "0.0" }, "3": { "runs": "138", "mean_diff": "0.6984688386521741", "se": "1.0639979431468838", "sum_d": "96.38869973400003", "sum_d2": "21470.64872799363" }, "4": { "runs": "131", "mean_diff": "0.2848799405633587", "se": "0.3007404085149028", "sum_d": "37.31927221379999", "sum_d2": "1550.9063421826272" } }, "flag": "" } ], "top": [ { "key": "f-flow-scales", "spread": "4", "code": "from dataclasses import replace as _flow_replace\nfrom orderbook_pm_challenge.types import PlaceOrder as _FlowPlace, CancelOrder as _FlowCancel, CancelAll as _FlowCancelAll, Side as _FlowSide\nimport math as _flow_math\n\nclass Strategy(_N):\n # Composition: the superclass is never initialized or called on self.\n FLOW_SPREADS = (4,)\n FLOW_SIZE = 0.3\n\n def __init__(self):\n self._incumbent = _N()\n self._flow_short = [0.0, 0.0]\n self._flow_long = [0.0, 0.0]\n self._flow_spread = None\n self._flow_serial = 0\n self._flow_ids = set()\n\n def on_step(self, state):\n if self._flow_spread is None:\n bid = state.competitor_best_bid_ticks\n ask = state.competitor_best_ask_ticks\n self._flow_spread = max(1, (ask - bid + 1) // 2) if bid is not None and ask is not None else 0\n if self._flow_spread not in self.FLOW_SPREADS:\n return self._incumbent.on_step(state)\n for i, value in enumerate((state.buy_filled_quantity, state.sell_filled_quantity)):\n self._flow_short[i] += (value - self._flow_short[i]) / 24.0\n self._flow_long[i] += (value - self._flow_long[i]) / 192.0\n extras = tuple(o for o in state.own_orders if o.order_id in self._flow_ids)\n native = tuple(o for o in state.own_orders if o.order_id not in self._flow_ids)\n base_actions = list(self._incumbent.on_step(_flow_replace(state, own_orders=native)))\n actions = [_FlowCancel(o.order_id) for o in extras] + base_actions\n self._flow_ids.clear()\n # Reconstruct N's resting quotes from its public actions, never its code.\n projected = {o.order_id: (o.side, o.price_ticks, o.remaining_quantity) for o in native}\n pending = []\n budget = max(0.0, state.free_cash)\n for action in base_actions:\n if isinstance(action, _FlowCancelAll):\n projected.clear()\n pending.clear()\n elif isinstance(action, _FlowCancel):\n projected.pop(action.order_id, None)\n pending = [(key, v) for key, v in pending if key != action.order_id]\n elif isinstance(action, _FlowPlace):\n pending.append((action.client_order_id, (action.side, action.price_ticks, action.quantity)))\n unit = action.price_ticks / 100.0 if action.side == _FlowSide.BUY else 1.0 - action.price_ticks / 100.0\n budget -= unit * action.quantity\n # Ignore all collateral released by cancellations: conservative free-cash bound.\n budget = max(0.0, budget)\n quotes = list(projected.values()) + [v for _, v in pending]\n if state.step < 192:\n return actions\n for i, side in enumerate((_FlowSide.BUY, _FlowSide.SELL)):\n short, long = self._flow_short[i], self._flow_long[i]\n if short <= 1.35 * long or long < 0.02:\n continue\n own = [q for q in quotes if q[0] == side]\n if not own:\n continue\n price = (max if side == _FlowSide.BUY else min)(q[1] for q in own)\n size = sum(q[2] for q in own if q[1] == price)\n unit = price / 100.0 if side == _FlowSide.BUY else 1.0 - price / 100.0\n desired = size * self.FLOW_SIZE * min(1.0, short / long - 1.0)\n quantity = _flow_math.floor(min(desired, 20.0 / unit, budget / unit) * 100.0) / 100.0\n if quantity < 0.01:\n continue\n self._flow_serial += 1\n oid = '__flow_scales_extra_' + str(self._flow_serial)\n self._flow_ids.add(oid)\n actions.append(_FlowPlace(side, price, quantity, oid))\n budget = max(0.0, budget - unit * quantity)\n return actions\n" }, { "key": "f-cash-order", "spread": "4", "code": "from dataclasses import replace as _cash_replace\nimport math as _cash_math\nfrom orderbook_pm_challenge.strategy import BaseStrategy as _CashBase\nfrom orderbook_pm_challenge.types import PlaceOrder as _CashPlace, CancelOrder as _CashCancel, CancelAll as _CashCancelAll, Side as _CashSide\n\nclass Strategy(_CashBase):\n # Frozen after tuning. No orders are added; only N's proposals are resized.\n spreads = (1, 3, 4)\n enabled = True\n def __init__(self):\n self.n = _N()\n self._records = {}\n self._pending = []\n self._spread = None\n self._mid = None\n self._drift = 0.0\n self.diagnostics = dict(steps=0, binds=0, swaps=0, ledger_misses=0,\n released=0.0, extra_quantity=0.0, repairs=0)\n\n @staticmethod\n def _reserve(r):\n side, tick, qty, covered, step = r\n return round((tick / 100.0) * qty if side == _CashSide.BUY\n else (1.0 - tick / 100.0) * max(0.0, qty - covered), 10)\n\n def _reconcile(self, state):\n records = {}\n pending = list(self._pending)\n good = True\n for o in state.own_orders:\n r = self._records.get(o.order_id)\n if r is None:\n candidates = [i for i, p in enumerate(pending)\n if p[0] == o.side and p[1] == o.price_ticks\n and p[4] == o.submitted_step\n and p[2] + 1e-8 >= o.remaining_quantity]\n if candidates:\n r = pending.pop(candidates[0])\n else:\n good = False\n r = (o.side, o.price_ticks, o.remaining_quantity, 0.0, o.submitted_step)\n filled = max(0.0, r[2] - o.remaining_quantity)\n covered = round(max(0.0, r[3] - filled), 10)\n records[o.order_id] = (o.side, o.price_ticks, o.remaining_quantity,\n min(covered, o.remaining_quantity), o.submitted_step)\n if abs(sum(self._reserve(r) for r in records.values()) - state.reserved_cash) > 1e-6:\n good = False\n return records, good\n\n def _simulate(self, state, records, actions):\n book = dict(records)\n pending = []\n legal = True\n for i, a in enumerate(actions):\n if isinstance(a, _CashCancelAll):\n book.clear()\n elif isinstance(a, _CashCancel):\n if a.order_id not in book:\n legal = False\n book.pop(a.order_id, None)\n elif isinstance(a, _CashPlace):\n q = round(_cash_math.floor((float(a.quantity) + 1e-9) / 0.01) * 0.01, 10)\n avail = round(max(0.0, state.yes_inventory - round(sum(r[3] for r in book.values()), 10)), 10)\n cover = min(q, avail) if a.side == _CashSide.SELL else 0.0\n r = (a.side, a.price_ticks, q, cover, state.step)\n key = a.client_order_id if a.client_order_id is not None else ('new', i)\n if key in book or q <= 0:\n legal = False\n book[key] = r\n if sum(self._reserve(v) for v in book.values()) > state.cash + 1e-9:\n legal = False\n known = {k: r for k, r in book.items() if isinstance(k, str)}\n pending = [r for k, r in book.items() if not isinstance(k, str)]\n free = state.cash - sum(self._reserve(r) for r in book.values())\n return known, pending, free, legal, book\n\n def on_step(self, state):\n # N owns every submitted order; the overlay creates no additional orders.\n actions = list(self.n.on_step(state))\n d = self.diagnostics\n d['steps'] += 1\n bid, ask = state.competitor_best_bid_ticks, state.competitor_best_ask_ticks\n if self._spread is None:\n self._spread = max(1, ((ask if ask is not None else 100) -\n (bid if bid is not None else 0) + 1) // 2)\n if bid is not None and ask is not None:\n mid = (bid + ask) / 200.0\n elif bid is not None:\n mid = min(.995, (bid + self._spread / 2.0) / 100.0)\n elif ask is not None:\n mid = max(.005, (ask - self._spread / 2.0) / 100.0)\n else:\n mid = self._mid if self._mid is not None else .5\n if self._mid is not None:\n self._drift = .75 * self._drift + .25 * (mid - self._mid)\n self._mid = mid\n fair = max(.005, min(.995, mid + self._drift))\n records, good = self._reconcile(state)\n known, pending, free, legal, final = self._simulate(state, records, actions)\n if not good:\n d['ledger_misses'] += 1\n # Binding means that the residual cash cannot fund a second copy of\n # at least one cash-backed non-probe proposal. This catches N's own\n # pre-emptive clipping; an actually unaffordable action would fail.\n proposals = []\n for i, a in enumerate(actions):\n if not isinstance(a, _CashPlace) or a.quantity < 1.0:\n continue\n key = a.client_order_id if a.client_order_id is not None else ('new', i)\n if key not in final:\n continue\n r = final[key]\n cost = self._reserve(r)\n if cost <= 1e-8:\n continue\n edge = (fair - a.price_ticks / 100.0) if a.side == _CashSide.BUY else (a.price_ticks / 100.0 - fair)\n proposals.append((edge * r[2] / cost, i, a, cost))\n binding = (not legal) or any(free + 1e-8 < p[3] for p in proposals)\n if binding:\n d['binds'] += 1\n if good and legal and binding and self.enabled and self._spread in self.spreads:\n # Only a surviving pre-existing order may be evicted. N's explicit\n # cancels and all other original proposals retain their ordering.\n survivors = []\n for oid, r in records.items():\n if oid not in final or self._reserve(r) <= 1e-8:\n continue\n edge = fair - r[1] / 100.0 if r[0] == _CashSide.BUY else r[1] / 100.0 - fair\n survivors.append((edge * r[2] / self._reserve(r), oid, r))\n if survivors and proposals:\n worst, oid, old = min(survivors, key=lambda x: (x[0], x[1]))\n best, index, a, cost = max(proposals, key=lambda x: (x[0], -x[1]))\n if best > worst + 1e-8 and free + 1e-8 < cost:\n released = self._reserve(old)\n unit = a.price_ticks / 100.0 if a.side == _CashSide.BUY else 1.0 - a.price_ticks / 100.0\n extra = _cash_math.floor((min(float(a.quantity), released / unit) + 1e-12) / .01) * .01\n if extra >= .01:\n changed = list(actions)\n changed[index] = _cash_replace(a, quantity=round(a.quantity + extra, 10))\n changed.insert(index, _CashCancel(oid))\n ck, cp, cf, ok, cb = self._simulate(state, records, changed)\n if ok:\n actions = changed\n known, pending = ck, cp\n d['swaps'] += 1\n d['released'] += released\n d['extra_quantity'] += extra\n if good and not legal and self.enabled and self._spread in self.spreads:\n actions = self._repair(state, records, actions, fair)\n known, pending, _, _, _ = self._simulate(state, records, actions)\n d['repairs'] += 1\n self._records, self._pending = known, pending\n return actions\n\n def _repair(self, state, records, actions, fair):\n # A previous collateral swap can leave N's next proposal unfunded.\n # Evict only inferior survivors, then clip to actual affordable size.\n doomed = {a.order_id for a in actions if isinstance(a, _CashCancel)}\n if any(isinstance(a, _CashCancelAll) for a in actions):\n doomed.update(records)\n out = []\n def ratio(r):\n edge = fair - r[1]/100.0 if r[0] == _CashSide.BUY else r[1]/100.0 - fair\n cost = self._reserve(r)\n return edge*r[2]/cost if cost > 1e-8 else float('inf')\n for a in actions:\n _, _, free, _, book = self._simulate(state, records, out)\n if isinstance(a, _CashCancel) and a.order_id not in book:\n continue\n if isinstance(a, _CashPlace):\n unit = a.price_ticks/100.0 if a.side == _CashSide.BUY else 1-a.price_ticks/100.0\n def capacity():\n _, _, f, _, b = self._simulate(state, records, out)\n cover = round(max(0.0, state.yes_inventory-round(sum(r[3] for r in b.values()),10)),10) if a.side == _CashSide.SELL else 0.0\n return max(0.0, cover + max(0.0, f-1e-7)/unit), cover\n cap, cover = capacity()\n new = (a.side,a.price_ticks,float(a.quantity),min(a.quantity,cover),state.step)\n candidates = sorted((ratio(r),oid) for oid,r in book.items()\n if oid in records and oid not in doomed and self._reserve(r)>1e-8)\n for score, oid in candidates:\n if a.quantity <= cap+1e-8 or score >= ratio(new)-1e-8:\n break\n out.append(_CashCancel(oid))\n cap, cover = capacity()\n if a.quantity > cap:\n q = round(_cash_math.floor(cap/.01)*.01,10)\n if q < .01:\n continue\n a = _cash_replace(a,quantity=q)\n out.append(a)\n return out\n" }, { "key": "f-drift-skew", "spread": "1", "code": "import math as _ds_math\nfrom statistics import NormalDist as _DSNormal\nfrom dataclasses import replace as _ds_replace\nfrom orderbook_pm_challenge.types import PlaceOrder as _DSPlace, CancelAll as _DSAll, CancelOrder as _DSCancel, Side as _DSSide\n\nclass Strategy:\n # Latent normal-score slope is shrunk 90% toward its zero-trend prior.\n spreads = (1,)\n gain = 8.0\n enabled = True\n\n def __init__(self):\n self.n = _N()\n self.s = None\n self.last_x = None\n self.slope = 0.0\n self.normal = _DSNormal()\n\n def on_step(self, state):\n actions = self.n.on_step(state)\n if self.s is None:\n b, a = state.competitor_best_bid_ticks, state.competitor_best_ask_ticks\n self.s = max(1, (a-b+1)//2) if a is not None and b is not None else 0\n if not self.enabled or self.s not in self.spreads:\n return actions\n b, a = state.competitor_best_bid_ticks, state.competitor_best_ask_ticks\n if a is None or b is None:\n return actions\n p = min(0.995, max(0.005, (a+b)/200.0))\n t = max(1, state.steps_remaining)\n z = self.normal.inv_cdf(p)\n x = z * _ds_math.sqrt(t)\n if self.last_x is not None:\n delta = min(1.0, max(-1.0, x-self.last_x))\n self.slope += (delta-self.slope)/128.0\n self.last_x = x\n drift = _ds_math.exp(-0.5*z*z)/_ds_math.sqrt(2*_ds_math.pi) * (z/(2*t) + 0.1*self.slope/_ds_math.sqrt(t))\n skew = min(0.30, max(-0.30, self.gain*100*drift))\n # Resize only N's placements. No added orders need hiding from N.\n # Maintain a collateral ledger to keep increases valid after cancellations.\n orders = {o.order_id: (o.side, o.price_ticks, o.remaining_quantity) for o in state.own_orders}\n def reserves():\n yes = state.yes_inventory\n cash = 0.0\n for side, tick, qty in orders.values():\n if side == _DSSide.BUY:\n cash += qty*tick/100.0\n else:\n covered = min(yes, qty)\n yes -= covered\n cash += (qty-covered)*(1-tick/100.0)\n return cash, yes\n out = []\n for i, action in enumerate(actions):\n if isinstance(action, _DSAll):\n orders.clear()\n elif isinstance(action, _DSCancel):\n orders.pop(action.order_id, None)\n elif isinstance(action, _DSPlace):\n used, yes = reserves()\n free = max(0.0, state.cash-used-1e-7)\n price = action.price_ticks/100.0\n capacity = free/price if action.side == _DSSide.BUY else yes+free/(1-price)\n factor = 1+skew if action.side == _DSSide.BUY else 1-skew\n qty = _ds_math.floor(min(action.quantity*factor, capacity)*100+1e-9)/100.0\n if qty < 0.01:\n continue\n action = _ds_replace(action, quantity=qty)\n orders[action.client_order_id or ('__drift', i)] = (action.side, action.price_ticks, qty)\n out.append(action)\n return out\n" } ] } ```