{
 "approach": "Blind build from the simulator source only. S0 (98 lines): fair value in ticks inferred from the static competitor ladder (start at its mid; ask lifted -> fair>=ask-0.5, bid dropped -> fair<=bid+0.5; our order fully filled is read as an arb sweep -> fair moves past it; clamped inside the competitor quotes), half-spread max(1 tick, per-step p sd from the jump-diffusion formula at mid-range parameters), size 10 shares shrunk when per-step sd > 1.5 ticks, improve the competitor by one tick when the edge target allows, else quote at the target; unchanged orders are left resting to keep FIFO priority. S1 = S0 + queue-aware join/improve: per side track notional consumed from the competitor's best level (reset when that tick changes, +0.57/step of expected retail flow while we are not ahead of it, set to full when we were filled at that tick), expected queue ahead = 48 - consumed; when improving would be allowed but the queue ahead is <= JOIN_Q notional, join the competitor's tick instead of improving.",
 "tried": [
  {
   "strategy": "S0",
   "variant": "BASE_SIZE=10, VOL_MULT=1, SIZE_VOL=1.5 (frozen)",
   "tuning_mean": 7.478
  },
  {
   "strategy": "S0",
   "variant": "BASE_SIZE=30",
   "tuning_mean": 4.014
  },
  {
   "strategy": "S0",
   "variant": "BASE_SIZE=60",
   "tuning_mean": -12.752
  },
  {
   "strategy": "S0",
   "variant": "BASE_SIZE=30, VOL_MULT=0.6, SIZE_VOL=3",
   "tuning_mean": -3.706
  },
  {
   "strategy": "S1",
   "variant": "JOIN_Q=4 (frozen)",
   "tuning_mean": 6.807
  },
  {
   "strategy": "S1",
   "variant": "JOIN_Q=8",
   "tuning_mean": 6.782
  },
  {
   "strategy": "S1",
   "variant": "JOIN_Q=20",
   "tuning_mean": 6.677
  },
  {
   "strategy": "S1",
   "variant": "JOIN_Q=35",
   "tuning_mean": 6.134
  }
 ],
 "next": "Do not graft as built: joining the competitor's tick lost retail flow (-0.95/sim) worth far more than the arb loss it avoided (+0.21/sim); every threshold lost on tuning, monotonically worse as joining got more frequent. The blind estimate of the competitor's remaining size (hidden notional 24-72, unobserved retail flow) is too coarse; the queue ahead is rarely truly short. If revisited, join only after a fill at that tick proves front-of-queue, and test it on a strong base: this S0 is weak (7.6 vs B 99.7), so the result says little about the effect on B.",
 "code": "from __future__ import annotations\n\nimport math\nfrom statistics import NormalDist\n\nfrom orderbook_pm_challenge.strategy import BaseStrategy\nfrom orderbook_pm_challenge.types import CancelOrder, PlaceOrder, Side, StepState\n\n_N = NormalDist()\n\n\nclass Strategy(BaseStrategy):\n    \"\"\"S1: fair-value quoting from the competitor ladder + queue-aware join/improve.\"\"\"\n\n    BASE_SIZE = 10.0      # shares per side at low volatility\n    HALF_MIN = 1.0        # minimum half-spread, ticks\n    VOL_MULT = 1.0        # half-spread = max(HALF_MIN, VOL_MULT * per-step p sd)\n    SIZE_VOL = 1.5        # size shrinks as per-step p sd (ticks) exceeds this\n    INV_SKEW = 0.01       # ticks of fair skew per share of net inventory\n    LAM, SJ, SIG = 0.0019, 0.40, 0.02  # mid-range process assumptions\n    # --- queue-aware mechanism ---\n    Q0 = 48.0             # expected competitor notional on a fresh level\n    FLOW = 0.57           # expected retail notional per step per side\n    JOIN_Q = 4.0          # join when expected notional ahead <= this\n\n    def __init__(self) -> None:\n        self.fair = None\n        self.cb0 = self.ca0 = None\n        self.last = {Side.BUY: None, Side.SELL: None}  # (price, qty) placed\n        self.used = {Side.BUY: 0.0, Side.SELL: 0.0}    # consumed notional at comp best\n        self.lvl = {Side.BUY: None, Side.SELL: None}   # comp best tick tracked\n        self.ahead_of_comp = {Side.BUY: False, Side.SELL: False}\n\n    def _sd_ticks(self, p: float, h: int) -> float:\n        p = min(0.995, max(0.005, p))\n        var = h * (self.SIG ** 2 + self.LAM * self.SJ ** 2)\n        step = math.sqrt(self.SIG ** 2 + self.LAM * self.SJ ** 2)\n        return 100.0 * _N.pdf(_N.inv_cdf(p)) * step / math.sqrt(max(var, 1e-9))\n\n    def _queue(self, side: Side, comp: int, filled: float) -> float:\n        if self.lvl[side] != comp:\n            self.lvl[side], self.used[side] = comp, 0.0\n        elif not self.ahead_of_comp[side]:\n            self.used[side] += self.FLOW\n        last = self.last[side]\n        if last is not None and last[0] == comp and filled > 0:\n            self.used[side] = self.Q0  # we were filled at comp tick: front of queue\n        return max(0.0, self.Q0 - self.used[side])\n\n    def on_step(self, state: StepState):\n        cb, ca = state.competitor_best_bid_ticks, state.competitor_best_ask_ticks\n        cb_ = cb if cb is not None else 0\n        ca_ = ca if ca is not None else 100\n        if self.fair is None:\n            self.fair = (cb_ + ca_) / 2.0\n        else:\n            if ca is not None and self.ca0 is not None and ca > self.ca0:\n                self.fair = max(self.fair, ca - 0.5)\n            if cb is not None and self.cb0 is not None and cb < self.cb0:\n                self.fair = min(self.fair, cb + 0.5)\n            lb, la = self.last[Side.BUY], self.last[Side.SELL]\n            if la is not None and state.sell_filled_quantity >= la[1] - 1e-9:\n                self.fair = max(self.fair, la[0] + 0.5)\n            if lb is not None and state.buy_filled_quantity >= lb[1] - 1e-9:\n                self.fair = min(self.fair, lb[0] - 0.5)\n        self.fair = min(ca_ - 0.5, max(cb_ + 0.5, self.fair))\n        self.cb0, self.ca0 = cb, ca\n\n        net = state.yes_inventory - state.no_inventory\n        f = self.fair - self.INV_SKEW * net\n        sd = self._sd_ticks(self.fair / 100.0, max(1, state.steps_remaining))\n        half = max(self.HALF_MIN, self.VOL_MULT * sd)\n        size = self.BASE_SIZE * min(1.0, self.SIZE_VOL / max(sd, 1e-9))\n\n        want = {}\n        tb, ta = math.floor(f - half), math.ceil(f + half)\n        qb = self._queue(Side.BUY, cb_, state.buy_filled_quantity)\n        qa = self._queue(Side.SELL, ca_, state.sell_filled_quantity)\n        if tb >= cb_ + 1:\n            tb = cb_ + 1\n            if cb is not None and qb <= self.JOIN_Q:\n                tb = cb_  # mechanism: short queue ahead -> join the competitor's tick\n        if ta <= ca_ - 1:\n            ta = ca_ - 1\n            if ca is not None and qa <= self.JOIN_Q:\n                ta = ca_\n        if 1 <= tb <= 99:\n            want[Side.BUY] = tb\n        if 1 <= ta <= 99:\n            want[Side.SELL] = ta\n        self.ahead_of_comp = {Side.BUY: tb > cb_, Side.SELL: ta < ca_}\n\n        actions = []\n        free = state.free_cash\n        keep = {}\n        for o in state.own_orders:\n            px = want.get(o.side)\n            if o.side not in keep and px == o.price_ticks and o.remaining_quantity >= 0.5 * size:\n                keep[o.side] = o\n            else:\n                actions.append(CancelOrder(o.order_id))\n                if o.side is Side.BUY:\n                    free += o.price_ticks / 100.0 * o.remaining_quantity\n        yes_free = state.yes_inventory if Side.SELL not in keep else 0.0\n        self.last = {s: ((keep[s].price_ticks, keep[s].remaining_quantity) if s in keep else None)\n                     for s in (Side.BUY, Side.SELL)}\n        for side, px in want.items():\n            if side in keep:\n                continue\n            p = px / 100.0\n            if side is Side.BUY:\n                q = min(size, max(0.0, free - 1.0) / p)\n            else:\n                cover = min(size, yes_free)\n                q = cover + min(size - cover, max(0.0, free - 1.0) / (1.0 - p))\n            q = math.floor(q * 100) / 100.0\n            if q < 0.01:\n                continue\n            free -= p * q if side is Side.BUY else (1.0 - p) * max(0.0, q - yes_free)\n            actions.append(PlaceOrder(side=side, price_ticks=px, quantity=q))\n            self.last[side] = (px, q)\n        return actions\n",
 "code_sha256": "8c83ca11bce688a820e6390439df27f853e496565a995dcd72532bef492c63d2",
 "code_s0": "from __future__ import annotations\n\nimport math\nfrom statistics import NormalDist\n\nfrom orderbook_pm_challenge.strategy import BaseStrategy\nfrom orderbook_pm_challenge.types import CancelOrder, PlaceOrder, Side, StepState\n\n_N = NormalDist()\n\n\nclass Strategy(BaseStrategy):\n    \"\"\"S0: fair-value quoting from the competitor ladder, improving the competitor by one tick.\"\"\"\n\n    BASE_SIZE = 10.0      # shares per side at low volatility\n    HALF_MIN = 1.0        # minimum half-spread, ticks\n    VOL_MULT = 1.0        # half-spread = max(HALF_MIN, VOL_MULT * per-step p sd)\n    SIZE_VOL = 1.5        # size shrinks as per-step p sd (ticks) exceeds this\n    INV_SKEW = 0.01       # ticks of fair skew per share of net inventory\n    LAM, SJ, SIG = 0.0019, 0.40, 0.02  # mid-range process assumptions\n\n    def __init__(self) -> None:\n        self.fair = None\n        self.cb0 = self.ca0 = None\n        self.last = {Side.BUY: None, Side.SELL: None}  # (price, qty) placed\n\n    def _sd_ticks(self, p: float, h: int) -> float:\n        p = min(0.995, max(0.005, p))\n        var = h * (self.SIG ** 2 + self.LAM * self.SJ ** 2)\n        step = math.sqrt(self.SIG ** 2 + self.LAM * self.SJ ** 2)\n        return 100.0 * _N.pdf(_N.inv_cdf(p)) * step / math.sqrt(max(var, 1e-9))\n\n    def on_step(self, state: StepState):\n        cb, ca = state.competitor_best_bid_ticks, state.competitor_best_ask_ticks\n        cb_ = cb if cb is not None else 0\n        ca_ = ca if ca is not None else 100\n        if self.fair is None:\n            self.fair = (cb_ + ca_) / 2.0\n        else:\n            if ca is not None and self.ca0 is not None and ca > self.ca0:\n                self.fair = max(self.fair, ca - 0.5)\n            if cb is not None and self.cb0 is not None and cb < self.cb0:\n                self.fair = min(self.fair, cb + 0.5)\n            lb, la = self.last[Side.BUY], self.last[Side.SELL]\n            if la is not None and state.sell_filled_quantity >= la[1] - 1e-9:\n                self.fair = max(self.fair, la[0] + 0.5)\n            if lb is not None and state.buy_filled_quantity >= lb[1] - 1e-9:\n                self.fair = min(self.fair, lb[0] - 0.5)\n        self.fair = min(ca_ - 0.5, max(cb_ + 0.5, self.fair))\n        self.cb0, self.ca0 = cb, ca\n\n        net = state.yes_inventory - state.no_inventory\n        f = self.fair - self.INV_SKEW * net\n        sd = self._sd_ticks(self.fair / 100.0, max(1, state.steps_remaining))\n        half = max(self.HALF_MIN, self.VOL_MULT * sd)\n        size = self.BASE_SIZE * min(1.0, self.SIZE_VOL / max(sd, 1e-9))\n\n        want = {}\n        tb, ta = math.floor(f - half), math.ceil(f + half)\n        if tb >= cb_ + 1:\n            tb = cb_ + 1\n        if ta <= ca_ - 1:\n            ta = ca_ - 1\n        if 1 <= tb <= 99:\n            want[Side.BUY] = tb\n        if 1 <= ta <= 99:\n            want[Side.SELL] = ta\n\n        actions = []\n        free = state.free_cash\n        keep = {}\n        for o in state.own_orders:\n            px = want.get(o.side)\n            if o.side not in keep and px == o.price_ticks and o.remaining_quantity >= 0.5 * size:\n                keep[o.side] = o\n            else:\n                actions.append(CancelOrder(o.order_id))\n                if o.side is Side.BUY:\n                    free += o.price_ticks / 100.0 * o.remaining_quantity\n        yes_free = state.yes_inventory if Side.SELL not in keep else 0.0\n        self.last = {s: ((keep[s].price_ticks, keep[s].remaining_quantity) if s in keep else None)\n                     for s in (Side.BUY, Side.SELL)}\n        for side, px in want.items():\n            if side in keep:\n                continue\n            p = px / 100.0\n            if side is Side.BUY:\n                q = min(size, max(0.0, free - 1.0) / p)\n            else:\n                cover = min(size, yes_free)\n                q = cover + min(size - cover, max(0.0, free - 1.0) / (1.0 - p))\n            q = math.floor(q * 100) / 100.0\n            if q < 0.01:\n                continue\n            free -= p * q if side is Side.BUY else (1.0 - p) * max(0.0, q - yes_free)\n            actions.append(PlaceOrder(side=side, price_ticks=px, quantity=q))\n            self.last[side] = (px, q)\n        return actions\n",
 "tuning_score": {
  "seeds": "115000..115499",
  "n": 500,
  "s1_mean": 6.8067,
  "s0_mean": 7.4775
 },
 "paired": {
  "seeds": "200000..200999",
  "n": 1000,
  "complete": true,
  "mechanism_diff": -0.7336,
  "mechanism_se": 0.0359,
  "mechanism_retail_diff": -0.9458,
  "mechanism_retail_se": 0.0412,
  "mechanism_arb_diff": 0.2123,
  "mechanism_arb_se": 0.0116,
  "reference_diff": -92.8255,
  "reference_se": 5.4357,
  "s1_mean": 6.8704,
  "s0_mean": 7.6039,
  "b_mean": 99.6959,
  "failures": [],
  "seeds_where_s1_differs": 751
 },
 "graft": "Per side keep consumed=notional taken from competitor best level: reset when its tick changes, +expected retail notional/step while you are not strictly better, =full after you were filled at that tick. If improving by 1 tick is allowed but 48-consumed <= JOIN_Q, quote at the competitor tick and leave the order resting. Measured here: -0.73+-0.04/sim; not recommended.",
 "seconds_per_simulation": 0.1493
}