{
 "key": "f-cover-stock",
 "approach": "Overlay on N (blind to N's source). Joint cash/inventory value of one more YES share: a YES share covers one of N's uncovered asks, freeing (1-ask) of collateral; valued at a shadow cash cost lam=2e-4*max(0,1-free/(0.4*starting cash)) per step times steps remaining. Add expected retail edge of the cover bid (EWMA bid-fill rate * 0.5 * (p_hat-price)) and subtract the one-step expected arbitrage loss sigma*[phi(g)-g*(1-Phi(g))], g=(p_hat-price)/sigma, with p_hat the mid of N's real (>=0.5 share) best bid/ask and sigma an EWMA of p_hat changes. When positive and N's asks exceed the YES it holds, post one hidden bid of min(5, uncovered) shares one tick below N's best real bid. Hidden from N: own order removed from own_orders, cover YES subtracted from yes_inventory, own fills from buy_filled_quantity, own reservation from reserved/free cash. Cover spend capped at $100 and order sized from free cash left after N's placements. Acts only when s=4.",
 "tried": [
  "Fetched N by script; SHA-256 bf0d179f3e47d28819f6fa7e0d25548c2ced43374c216d36460b00a26a26ffae matched; N's total edge on seed 310000 was 17.1160017503, matching the expected 17.116002.",
  "Probe of N's runtime state on 12 tuning seeds: free cash never fell below ~45% of $1000 and N's asks were uncovered on only ~5% of steps, so the pure collateral term is ~0; the overlay mostly turns on the retail-edge vs arbitrage-loss terms.",
  "First build used the mid of all N orders; N also rests 0.01-share orders across ticks, so best bid >= best ask and the gate never fired. Fixed by using orders >= 0.5 shares.",
  "First tuning pass showed OrderBookError (insufficient free cash) on 4-5 of 200 seeds because N's view had cash inflated by cover spend and N sizes to free cash; fixed (true cash shown, cover sized from free cash after N's orders). Those results were discarded.",
  "Variant v0 (cover bid at N's best real bid) on 310000..310199, all spreads: mean diff -0.085 (se 0.173); by s: 1 -0.006, 2 -0.243, 3 -0.597, 4 +0.669 (se 0.79).",
  "Variant v1 (one tick below N's best real bid), all spreads: +0.040 (se 0.089); by s: 1 -0.00007, 2 -0.025, 3 -0.131, 4 +0.382 (se 0.408). Chose v1 with spread set {4}; froze.",
  "Holdout 410000..410499, once: see paired. Off switch equal to N on 310000..310009; assembled equal to S1 on 310009,310013,310021,310023,310024 (s=4, mechanism active).",
  "Tuning figure is for the frozen choice (v1 on s=4, N untouched elsewhere, which the holdout confirms gives d=0 exactly on s=1..3). Holdout seeds acting: 72 of 131 s=4 runs had d!=0. Paired gain +0.034 (se 0.053) is far below 2 se: no promotion case."
 ],
 "next": "Collateral never binds for N, so a cover-stock value is near zero in this simulator; the cover bid is effectively an extra deep passive bid, which loses to arbitrage at s=1..3. Do not pursue the collateral lever; if anything, the s=4 effect needs a dedicated larger-seed test before any reuse.",
 "spreads_acted": [
  4
 ],
 "off_equals_n": true,
 "code": "import dataclasses as _dc\nimport math as _m\nfrom orderbook_pm_challenge.types import CancelOrder as _CO, PlaceOrder as _PO, Side as _SD\n\n_SPREADS = (4,)\n_OFFSET = 1\n_CAP = 5.0\n\n\nclass Strategy(BaseStrategy):\n    \"\"\"f-cover-stock: joint cash/inventory value of one more YES share.\n\n    A YES share covers one of N's uncovered asks, freeing (1-ask) cash of\n    collateral for the remaining steps. Value it at a shadow cost of cash\n    that rises as free cash runs low, add the expected retail edge of the\n    cover bid, and subtract its expected one-step arbitrage loss. Post a\n    small hidden cover bid when the sum is positive.\n    \"\"\"\n\n    def __init__(self, spreads=None, offset=None, cap=None):\n        self._n = _N()\n        self._sp = _SPREADS if spreads is None else tuple(spreads)\n        self._off = _OFFSET if offset is None else offset\n        self._cap = _CAP if cap is None else cap\n        self._on = None\n        self._oid = None\n        self._orem = 0.0\n        self._otick = 0\n        self._ores = 0.0\n        self._k = 0\n        self._dy = 0.0\n        self._dc = 0.0\n        self._p = None\n        self._v = 1e-4\n        self._fr = 0.05\n        self._cash0 = None\n\n    def on_step(self, state):\n        if self._on is None:\n            b, a = state.competitor_best_bid_ticks, state.competitor_best_ask_ticks\n            s = max(1, (a - b + 1) // 2) if (a is not None and b is not None) else 0\n            self._on = s in self._sp\n            self._cash0 = state.cash\n        if not self._on:\n            return self._n.on_step(state)\n\n        mine = None\n        others = []\n        for o in state.own_orders:\n            if o.order_id == self._oid:\n                mine = o\n            else:\n                others.append(o)\n        myfill = 0.0\n        if self._oid is not None:\n            rem = mine.remaining_quantity if mine is not None else 0.0\n            myfill = round(max(0.0, self._orem - rem), 10)\n            if myfill > 0:\n                self._dy += myfill\n                self._dc += myfill * self._otick / 100.0\n        self._dy = min(self._dy, state.yes_inventory)\n        myres = mine.remaining_quantity * self._otick / 100.0 if mine is not None else 0.0\n        nview = _dc.replace(\n            state,\n            own_orders=tuple(others),\n            yes_inventory=round(state.yes_inventory - self._dy, 10),\n            reserved_cash=round(state.reserved_cash - myres, 10),\n            free_cash=round(state.free_cash + myres, 10),\n            buy_filled_quantity=round(max(0.0, state.buy_filled_quantity - myfill), 10),\n        )\n        acts = list(self._n.on_step(nview))\n\n        pre = []\n        if mine is not None:\n            pre.append(_CO(mine.order_id))\n        self._oid = None\n        self._orem = 0.0\n\n        # N's resulting book, from its view and actions\n        book = {o.order_id: (o.side, o.price_ticks, o.remaining_quantity) for o in others}\n        used = 0.0\n        for x in acts:\n            if isinstance(x, _CO):\n                book.pop(x.order_id, None)\n            elif isinstance(x, _PO):\n                self._k += 1\n                book[\"_new%d\" % self._k] = (x.side, x.price_ticks, x.quantity)\n                pr = x.price_ticks / 100.0\n                used += x.quantity * (pr if x.side == _SD.BUY else 1.0 - pr)\n            else:\n                book = {}\n        bids = [t for (sd, t, q) in book.values() if sd == _SD.BUY and q >= 0.5]\n        asks = [(t, q) for (sd, t, q) in book.values() if sd == _SD.SELL]\n        ra = [t for t, q in asks if q >= 0.5]\n        bb = max(bids) if bids else state.competitor_best_bid_ticks\n        ba = min(ra) if ra else state.competitor_best_ask_ticks\n        if bb is None or ba is None:\n            return pre + acts\n        p = (bb + ba) / 200.0\n        if self._p is not None:\n            d = p - self._p\n            self._v = 0.97 * self._v + 0.03 * d * d\n        self._p = p\n        sig = max(0.004, _m.sqrt(self._v))\n        self._fr = 0.99 * self._fr + 0.01 * (1.0 if state.buy_filled_quantity > 0 else 0.0)\n\n        tick = bb - self._off\n        if tick < 1 or tick >= ba:\n            return pre + acts\n        price = tick / 100.0\n        askq = sum(q for t, q in asks)\n        unc = askq - max(0.0, state.yes_inventory - self._dy)\n        if unc <= 0.01 or self._dc > 100.0:\n            return pre + acts\n        a_px = ba / 100.0\n        free = state.free_cash + myres - used\n        lam = 2e-4 * max(0.0, 1.0 - free / (0.4 * self._cash0))\n        v_coll = (1.0 - a_px) * lam * state.steps_remaining\n        g = (p - price) / sig\n        arb = sig * (_m.exp(-0.5 * g * g) / 2.5066282746 - g * 0.5 * _m.erfc(g / 1.41421356))\n        ret = self._fr * 0.5 * max(0.0, p - price)\n        if v_coll + ret - arb <= 0.0:\n            return pre + acts\n        q = int(min(self._cap, unc) * 100) / 100.0\n        if q <= 0 or price * q > 0.2 * free - 1.0:\n            return pre + acts\n        self._k += 1\n        self._oid = \"zcv%d\" % self._k\n        self._orem = q\n        self._otick = tick\n        return pre + acts + [_PO(_SD.BUY, tick, q, self._oid)]\n",
 "code_sha256": "29b3e7adf3e3315f2da261c2eace235bd22e42fbc748b18d658b8746836f7b0d",
 "assembled_sha256": "13f346f104d0706dc0e47adc5668afbfddca50124c0482ceef4256cc2c5eac26",
 "tuning": {
  "n": 200,
  "mean_diff": 0.082082,
  "se": 0.087628
 },
 "paired": {
  "first_seed": 410000,
  "last_seed": 410499,
  "n": 500,
  "complete": true,
  "incumbent_mean": 104.413753,
  "mean_diff": 0.033797,
  "se": 0.05254,
  "sum_d": 16.898488,
  "sum_d2": 689.296134,
  "retail_diff": 0.077842,
  "arb_diff": -0.044045,
  "failures": 0
 },
 "by_spread": {
  "1": {
   "runs": 115,
   "incumbent_mean": 28.495154,
   "mean_diff": 0.0,
   "se": 0.0,
   "sum_d": 0.0,
   "sum_d2": 0.0,
   "retail_diff": 0.0,
   "arb_diff": 0.0
  },
  "2": {
   "runs": 116,
   "incumbent_mean": 97.766262,
   "mean_diff": 0.0,
   "se": 0.0,
   "sum_d": 0.0,
   "sum_d2": 0.0,
   "retail_diff": 0.0,
   "arb_diff": 0.0
  },
  "3": {
   "runs": 138,
   "incumbent_mean": 128.405451,
   "mean_diff": 0.0,
   "se": 0.0,
   "sum_d": 0.0,
   "sum_d2": 0.0,
   "retail_diff": 0.0,
   "arb_diff": 0.0
  },
  "4": {
   "runs": 131,
   "incumbent_mean": 151.672482,
   "mean_diff": 0.128996,
   "se": 0.200867,
   "sum_d": 16.898488,
   "sum_d2": 689.296134,
   "retail_diff": 0.297108,
   "arb_diff": -0.168112
  }
 },
 "seconds_per_simulation": 0.896
}