{
 "approach": "Engine audit of same-tick fill allocation at commit b244530: _best_order picks min (ask) / max (bid) price, ties broken by RestingOrder.sequence (strict FIFO price-time, no pro-rata). Every order, competitor included, gets a new sequence at creation. Competitor replenishments are created in refresh_competitor at step start, before participant actions. Arb sweeps every level through p_t, so priority does not matter to arb fills, only to retail fills. The participant is filled first at a tick only if its order was resting there before the competitor's order arrived. Cancel/replace (incl. CancelAll every step) gives up that seniority, and an order joining an existing competitor level sits behind ~quote_notional/price shares and almost never fills. Rule (S1): tag our orders placed strictly inside the competitor's best as queue-senior. When the competitor ladder later moves onto that tick (cb == our bid or ca == our ask), keep the order instead of re-quoting one tick inside, as long as its edge vs the fair estimate is >= KEEP_M. Never cancel/re-place an unchanged order. S0: fair = midpoint of competitor best bid/ask; bid=min(cb+1,floor(fair-M)), ask=max(ca-1,ceil(fair+M)), placed only strictly inside the competitor's quotes; size Q shrunk by inventory; capped by free cash.",
 "tried": [
  {
   "variant": "S0 M=1,Q=6",
   "seeds": "115000..115499",
   "n": 500,
   "mean_total_edge": -1.8758
  },
  {
   "variant": "S0 M=2,Q=6 (frozen)",
   "seeds": "115000..115499",
   "n": 500,
   "mean_total_edge": 0.3517
  },
  {
   "variant": "S0 M=3,Q=6",
   "seeds": "115000..115499",
   "n": 500,
   "mean_total_edge": 0.0,
   "note": "never quotes"
  },
  {
   "variant": "S0 M=2,Q=3",
   "seeds": "115000..115499",
   "n": 500,
   "mean_total_edge": 0.2378
  },
  {
   "variant": "S1 KEEP_M=2",
   "seeds": "115000..115249 only (half 115250..115499 killed by a tool timeout, unfinished)",
   "n": 250,
   "mean_total_edge": 0.3366,
   "paired_diff_vs_S0": 0.0
  },
  {
   "variant": "S1 KEEP_M=0 (frozen)",
   "seeds": "115000..115499",
   "n": 500,
   "mean_total_edge": 0.3517,
   "paired_diff_vs_S0": -1e-06
  },
  {
   "variant": "S1 KEEP_M=-99 (always keep senior)",
   "seeds": "115000..115499",
   "n": 500,
   "mean_total_edge": 0.3517
  },
  {
   "variant": "S1 KEEP_M=0,KEEP_FRAC=0.5",
   "seeds": "115000..115499",
   "n": 500,
   "mean_total_edge": 0.3517
  }
 ],
 "next": "Negative result in this setting. My S0 uses a competitor-midpoint fair estimate that is too poor: with M=1 arb loss swamps retail edge (-1.88). With M>=2 it rarely quotes, so the senior-at-competitor-tick case almost never comes up: S1 differs from S0 on 6 of 1000 holdout seeds. Retest the rule grafted onto a strategy that quotes every step near a good fair estimate (e.g. B). It only pays when the competitor replenishes onto a tick we already hold. Worth instrumenting how often cb/ca lands on a resting order of ours first. Arb fills are unaffected by priority (arb sweeps all levels through p).",
 "code": "import math\nfrom orderbook_pm_challenge.strategy import BaseStrategy\nfrom orderbook_pm_challenge.types import CancelAll, CancelOrder, PlaceOrder, Side, StepState\n\nM = 2.0      # min edge (ticks) vs estimated fair\nQ = 6.0      # base quote size (shares)\nINV = 60.0   # inventory at which a side's size is halved\nKEEP_M = 0.0  # min edge (ticks) to keep a queue-senior order at the competitor's tick\nKEEP_FRAC = 0.0  # keep only if remaining >= KEEP_FRAC * desired size\n\n\nclass Strategy(BaseStrategy):\n    \"\"\"S1 = S0 + FIFO queue-seniority retention. S0: quote one tick inside the hidden competitor's ladder around a\n    competitor-midpoint fair estimate, sized against adverse selection.\n    Engine audit: same-tick fills are strict FIFO by placement sequence, and\n    competitor replenishments are created at step start with a new sequence.\n    An order of ours resting at a tick before the competitor arrives there is\n    filled first. Rule: when the competitor ladder moves onto our resting tick,\n    keep that order (ahead of the competitor, one tick better than re-quoting\n    inside) instead of cancel-and-replace; never re-place an unchanged order.\"\"\"\n\n    def __init__(self):\n        self.last_bid = 49\n        self.last_ask = 51\n        self.n = 0\n        self.senior = {}\n\n    def fair(self, s):\n        cb, ca = s.competitor_best_bid_ticks, s.competitor_best_ask_ticks\n        if cb is None and ca is None:\n            cb, ca = self.last_bid, self.last_ask\n        elif cb is None:\n            cb = ca - (self.last_ask - self.last_bid)\n        elif ca is None:\n            ca = cb + (self.last_ask - self.last_bid)\n        self.last_bid, self.last_ask = cb, ca\n        return cb, ca, 0.5 * (cb + ca)\n\n    def sizes(self, s):\n        net = s.yes_inventory - s.no_inventory\n        qb = Q / (1.0 + max(0.0, net) / INV)\n        qa = Q / (1.0 + max(0.0, -net) / INV)\n        return qb, qa\n\n    def targets(self, s):\n        cb, ca, f = self.fair(s)\n        b = min(cb + 1, math.floor(f - M))\n        a = max(ca - 1, math.ceil(f + M))\n        return cb, ca, f, b, a\n\n    def afford(self, s, side, tick, q, free):\n        p = tick / 100.0\n        if side is Side.BUY:\n            cost = p\n        else:\n            cost = 0.0 if s.yes_inventory >= q else (1.0 - p)\n        if cost > 0:\n            q = min(q, 0.9 * free / cost)\n        q = int(q * 100) / 100.0\n        return q\n\n    def keep(self, o, side, cb, ca, f, t0, q):\n        if not self.senior.get(o.order_id) or o.remaining_quantity < KEEP_FRAC * q:\n            return False\n        k = o.price_ticks\n        if k == t0 and abs(o.remaining_quantity - q) < 0.01:\n            return True\n        if side is Side.BUY:\n            return k == cb and f - k >= KEEP_M\n        return k == ca and k - f >= KEEP_M\n\n    def on_step(self, s: StepState):\n        cb, ca, f, b, a = self.targets(s)\n        qb, qa = self.sizes(s)\n        acts = []\n        kept = {Side.BUY: False, Side.SELL: False}\n        live = set()\n        for o in s.own_orders:\n            t0, q = (b, qb) if o.side is Side.BUY else (a, qa)\n            if not kept[o.side] and self.keep(o, o.side, cb, ca, f, t0, q):\n                kept[o.side] = True\n                live.add(o.order_id)\n            else:\n                acts.append(CancelOrder(o.order_id))\n        self.senior = {k: v for k, v in self.senior.items() if k in live}\n        free = s.free_cash + sum(o.remaining_quantity * (o.price_ticks / 100.0 if o.side is Side.BUY else 0.0) for o in s.own_orders if o.order_id not in live)\n        free = min(free, s.free_cash + s.reserved_cash)\n        if not kept[Side.BUY] and 1 <= b <= 99 and b > cb:\n            q = self.afford(s, Side.BUY, b, qb, free)\n            if q >= 0.01:\n                self.n += 1\n                oid = \"b%d\" % self.n\n                acts.append(PlaceOrder(side=Side.BUY, price_ticks=b, quantity=q, client_order_id=oid))\n                self.senior[oid] = True\n                free -= q * b / 100.0\n        if not kept[Side.SELL] and 1 <= a <= 99 and a < ca:\n            q = self.afford(s, Side.SELL, a, qa, free)\n            if q >= 0.01:\n                self.n += 1\n                oid = \"a%d\" % self.n\n                acts.append(PlaceOrder(side=Side.SELL, price_ticks=a, quantity=q, client_order_id=oid))\n                self.senior[oid] = True\n        return acts\n",
 "code_sha256": "e9dcc925af8b3e4bc544bc762cf237a454b2ad6e698eb767ec1de4371cbd7d94",
 "code_s0": "import math\nfrom orderbook_pm_challenge.strategy import BaseStrategy\nfrom orderbook_pm_challenge.types import CancelAll, PlaceOrder, Side, StepState\n\nM = 2.0      # min edge (ticks) vs estimated fair\nQ = 6.0      # base quote size (shares)\nINV = 60.0   # inventory at which a side's size is halved\n\n\nclass Strategy(BaseStrategy):\n    \"\"\"S0: quote one tick inside the hidden competitor's ladder around a\n    competitor-midpoint fair estimate, sized against adverse selection.\"\"\"\n\n    def __init__(self):\n        self.last_bid = 49\n        self.last_ask = 51\n\n    def fair(self, s):\n        cb, ca = s.competitor_best_bid_ticks, s.competitor_best_ask_ticks\n        if cb is None and ca is None:\n            cb, ca = self.last_bid, self.last_ask\n        elif cb is None:\n            cb = ca - (self.last_ask - self.last_bid)\n        elif ca is None:\n            ca = cb + (self.last_ask - self.last_bid)\n        self.last_bid, self.last_ask = cb, ca\n        return cb, ca, 0.5 * (cb + ca)\n\n    def sizes(self, s):\n        net = s.yes_inventory - s.no_inventory\n        qb = Q / (1.0 + max(0.0, net) / INV)\n        qa = Q / (1.0 + max(0.0, -net) / INV)\n        return qb, qa\n\n    def targets(self, s):\n        cb, ca, f = self.fair(s)\n        b = min(cb + 1, math.floor(f - M))\n        a = max(ca - 1, math.ceil(f + M))\n        return cb, ca, f, b, a\n\n    def afford(self, s, side, tick, q, free):\n        p = tick / 100.0\n        if side is Side.BUY:\n            cost = p\n        else:\n            cost = 0.0 if s.yes_inventory >= q else (1.0 - p)\n        if cost > 0:\n            q = min(q, 0.9 * free / cost)\n        q = int(q * 100) / 100.0\n        return q\n\n    def on_step(self, s: StepState):\n        cb, ca, f, b, a = self.targets(s)\n        qb, qa = self.sizes(s)\n        acts = [CancelAll()]\n        free = s.free_cash + s.reserved_cash\n        if 1 <= b <= 99 and b > cb:\n            q = self.afford(s, Side.BUY, b, qb, free)\n            if q >= 0.01:\n                acts.append(PlaceOrder(side=Side.BUY, price_ticks=b, quantity=q))\n                free -= q * b / 100.0\n        if 1 <= a <= 99 and a < ca:\n            q = self.afford(s, Side.SELL, a, qa, free)\n            if q >= 0.01:\n                acts.append(PlaceOrder(side=Side.SELL, price_ticks=a, quantity=q))\n        return acts\n",
 "tuning_score": {
  "seeds": "115000..115499",
  "n": 500,
  "s1_mean": 0.35171,
  "s0_mean": 0.351711
 },
 "paired": {
  "seeds": "200000..200999",
  "n": 1000,
  "complete": true,
  "mechanism_diff": -1.29e-06,
  "mechanism_se": 9.6e-07,
  "mechanism_retail_diff": -1.48e-06,
  "mechanism_arb_diff": 1.9e-07,
  "reference_diff": -99.2527,
  "reference_se": 5.7357,
  "s1_mean": 0.443196,
  "s0_mean": 0.443197,
  "b_mean": 99.6959,
  "failures": [],
  "seeds_where_s1_differs": 6
 },
 "graft": "Give each order a client id, tagged senior if placed strictly inside the competitor's best on its side. Each step, if the competitor best now equals a senior resting order's tick (cb==bid or ca==ask) and edge vs fair >= KEEP_M, keep it rather than re-quote 1 tick inside: you are ahead of the competitor in FIFO at a better price. Never CancelAll and re-place unchanged orders.",
 "seconds_per_simulation": 1.881,
 "seconds_per_simulation_note": "wall time per simulation with 8 concurrent single-process runners; 1.7 s single-runner on 20 timing seeds"
}