{
 "key": "f5-rival-advance-same",
 "mechanism": "When the rival best bid (ask) improves by exactly one tick, cancel own orders on that same side and hold it so RC re-quotes after the cooldown.",
 "approach": "Overlay on RC: an exact +1 tick rival bid (-1 ask) holds that side for the cooldown. Frozen variant pulls and drops only orders queuing behind the rival. Literal cancel-all variants lost about 17 and 21 on tuning. Frozen variant rarely acts, mostly at spread 1, and holdout shows no gain: -0.35 (se 0.43).",
 "tried": [
  {
   "name": "all_cd1",
   "description": "cancel every own order on the side, cooldown 1",
   "code_sha256": "e6d0f34ef16b041ed2ffaa742b80460e299a50249925353caccb8573364d6c2b",
   "mean_diff": -16.5725895199425,
   "se": 1.383768917727757,
   "n": 200
  },
  {
   "name": "all_cd2",
   "description": "cancel every own order on the side, cooldown 2",
   "code_sha256": "34ef7dea0fd826565cf03c5df4295263422b246756004d1d84300a6da98385a6",
   "mean_diff": -20.786180628613,
   "se": 1.6272844184068833,
   "n": 200
  },
  {
   "name": "behind_cd1",
   "description": "cancel/drop only orders queuing behind the rival, cooldown 1",
   "code_sha256": "fb43b28dea22f524d46ba061719c1e700ae372b9cd98ca638d0020e689706b48",
   "mean_diff": 0.13361161908149996,
   "se": 0.30560750673154363,
   "n": 200
  },
  {
   "name": "behind_cd2",
   "description": "cancel/drop only orders queuing behind the rival, cooldown 2",
   "code_sha256": "27f936d412ffcb6b95c6a2764b38f40d49630d3885ee1f04d4d99e76ba8ab79b",
   "mean_diff": 0.13380007891849993,
   "se": 0.30603548718764634,
   "n": 200
  }
 ],
 "next": "Drop this mechanism. Cancelling all same-side orders on a rival advance costs retail edge heavily (tuning retail -13.0 at cooldown 1) and raises no arbitrage edge, and RC seldom rests behind the rival, so the behind-only form has almost nothing to act on (s2-s4 holdout |d| under 0.1). The spread 1 result, -1.15 (se 1.51), is noise-sized. Not worth a larger run.",
 "spreads_acted": [
  1,
  2,
  3,
  4
 ],
 "off_equals_rc": true,
 "code": "\n# Overlay f5-rival-advance-same: when the rival's best bid (ask) improves by exactly\n# one tick, pull own bids (asks) and hold that side so RC re-quotes after the cooldown.\nfrom orderbook_pm_challenge.types import (\n    CancelOrder as _FoCancel,\n    PlaceOrder as _FoPlace,\n    Side as _FoSide,\n)\n\n\nclass Strategy(_RC):\n    FO_SPREADS = (1, 2, 3, 4)  # rival spreads acted on; empty = mechanism off\n    FO_COOLDOWN = 2  # callbacks a side stays held, counting the trigger\n    FO_BEHIND_ONLY = True  # True: touch only what queues behind the rival\n\n    def __init__(self):\n        super().__init__()\n        self._fo_first = True\n        self._fo_s = None\n        self._fo_prev = (None, None)\n        self._fo_hold = {_FoSide.BUY: 0, _FoSide.SELL: 0}\n\n    def on_step(self, state):\n        actions = super().on_step(state)\n        if not self.FO_SPREADS:\n            return actions\n        return self._fo_edit(state, actions)\n\n    def _fo_edit(self, state, actions):\n        bid = state.competitor_best_bid_ticks\n        ask = state.competitor_best_ask_ticks\n        if self._fo_first:\n            self._fo_first = False\n            if bid is not None and ask is not None:\n                self._fo_s = max(1, (ask - bid + 1) // 2)\n        if self._fo_s not in self.FO_SPREADS:\n            return actions\n        prev_bid, prev_ask = self._fo_prev\n        self._fo_prev = (bid, ask)\n        hold = self._fo_hold\n        if prev_bid is not None and bid is not None and bid == prev_bid + 1:\n            hold[_FoSide.BUY] = self.FO_COOLDOWN\n        if prev_ask is not None and ask is not None and ask == prev_ask - 1:\n            hold[_FoSide.SELL] = self.FO_COOLDOWN\n        cancels = [\n            o.order_id\n            for o in state.own_orders\n            if hold[o.side] > 0 and self._fo_cancels(o.side, o.price_ticks, bid, ask)\n        ]\n        # Pulled or never-placed orders can leave RC cancelling an id that is gone.\n        live = {o.order_id for o in state.own_orders} - set(cancels)\n        out = [_FoCancel(order_id) for order_id in cancels]\n        for action in actions:\n            if isinstance(action, _FoCancel):\n                if action.order_id in live:\n                    live.discard(action.order_id)\n                    out.append(action)\n            elif isinstance(action, _FoPlace):\n                if hold[action.side] > 0 and self._fo_drops(action.side, action.price_ticks, bid, ask):\n                    continue\n                if action.client_order_id:\n                    live.add(action.client_order_id)\n                out.append(action)\n            else:\n                live.clear()\n                out.append(action)\n        for side in hold:\n            if hold[side] > 0:\n                hold[side] -= 1\n        return out\n\n    def _fo_cancels(self, side, price, bid, ask):\n        if not self.FO_BEHIND_ONLY:\n            return True\n        if side is _FoSide.BUY:\n            return bid is not None and price < bid\n        return ask is not None and price > ask\n\n    def _fo_drops(self, side, price, bid, ask):\n        if not self.FO_BEHIND_ONLY:\n            return True\n        if side is _FoSide.BUY:\n            return bid is not None and price <= bid\n        return ask is not None and price >= ask\n",
 "code_sha256": "27f936d412ffcb6b95c6a2764b38f40d49630d3885ee1f04d4d99e76ba8ab79b",
 "assembled_sha256": "a16187f946f4c17e275246ce2a2033a0d37c5f7442a15e21b9ea04b3d9afdd30",
 "comparator_sha256": "3c07bdcd221ac25729c3ebc92b21ac9e2014510ecb85816c4391ed81e933291f",
 "tuning": {
  "seeds": "330000..330199",
  "selected": "behind_cd2",
  "selection": "highest exact paired overall mean across all acted spreads; behind_cd2 +0.13380 vs behind_cd1 +0.13361 is a tie in practice",
  "overall": {
   "n": 200,
   "sum_d": 26.760015783699984,
   "sum_d2": 3731.157725067286,
   "mean_diff": 0.13380007891849993,
   "se": 0.30603548718764634,
   "comparator_mean": 97.4672213329115,
   "candidate_mean": 97.60102141183,
   "retail_diff": -0.3400969141040001,
   "arb_diff": 0.4738969930249998
  },
  "by_spread": {
   "1": {
    "n": 54,
    "sum_d": 21.09401660679991,
    "sum_d2": 3699.0541783946537,
    "mean_diff": 0.39062993716296135,
    "se": 1.1356023383491642,
    "comparator_mean": 46.182844338938885,
    "candidate_mean": 46.57347427610185,
    "retail_diff": -1.2951759257592597,
    "arb_diff": 1.6858058629314812
   },
   "2": {
    "n": 57,
    "sum_d": 0.0,
    "sum_d2": 0.0,
    "mean_diff": 0.0,
    "se": 0.0,
    "comparator_mean": 87.23571902150526,
    "candidate_mean": 87.23571902150526,
    "retail_diff": 0.0,
    "arb_diff": 0.0
   },
   "3": {
    "n": 49,
    "sum_d": 5.665999176900073,
    "sum_d2": 32.1035466726323,
    "mean_diff": 0.1156326362632668,
    "se": 0.11563263626326678,
    "comparator_mean": 112.87398754092447,
    "candidate_mean": 112.98962017718775,
    "retail_diff": 0.039186064697959204,
    "arb_diff": 0.07644657156530585
   },
   "4": {
    "n": 40,
    "sum_d": 0.0,
    "sum_d2": 0.0,
    "mean_diff": 0.0,
    "se": 0.0,
    "comparator_mean": 162.4077324637125,
    "candidate_mean": 162.4077324637125,
    "retail_diff": 0.0,
    "arb_diff": 0.0
   }
  }
 },
 "paired": {
  "seeds": "440000..440499",
  "n": 500,
  "complete": true,
  "missing_seeds": [],
  "overall": {
   "n": 500,
   "sum_d": -173.25924943770002,
   "sum_d2": 46228.66609546551,
   "mean_diff": -0.34651849887540004,
   "se": 0.43016811068306027,
   "comparator_mean": 101.4369248623788,
   "candidate_mean": 101.09040636350339,
   "retail_diff": -0.5435962907768002,
   "arb_diff": 0.19707779190299995
  },
  "by_spread": {
   "1": {
    "n": 142,
    "sum_d": -163.58931881540002,
    "sum_d2": 46108.611701309244,
    "mean_diff": -1.152037456446479,
    "se": 1.5144255348999944,
    "comparator_mean": 28.7146791778493,
    "candidate_mean": 27.56264172140282,
    "retail_diff": -1.9140734099802823,
    "arb_diff": 0.7620359535394364
   },
   "2": {
    "n": 110,
    "sum_d": 2.1200711237000007,
    "sum_d2": 4.494701569546584,
    "mean_diff": 0.01927337385181819,
    "se": 0.01927337385181819,
    "comparator_mean": 88.78927784058273,
    "candidate_mean": 88.80855121443454,
    "retail_diff": 0.0,
    "arb_diff": 0.01927337385181819
   },
   "3": {
    "n": 119,
    "sum_d": -10.693835210700001,
    "sum_d2": 114.35811151360714,
    "mean_diff": -0.08986416143445379,
    "se": 0.0898641614344538,
    "comparator_mean": 143.37780997585293,
    "candidate_mean": 143.2879458144185,
    "retail_diff": 2.343099159596389e-06,
    "arb_diff": -0.08986650453361344
   },
   "4": {
    "n": 129,
    "sum_d": -1.0961665353,
    "sum_d2": 1.2015810731116063,
    "mean_diff": -0.008497415002325582,
    "se": 0.008497415002325582,
    "comparator_mean": 153.5829305298,
    "candidate_mean": 153.57443311479767,
    "retail_diff": 0.0,
    "arb_diff": -0.008497415002325582
   }
  }
 },
 "failures": [],
 "check_values": [
  17.5126554022,
  20.6298693268,
  2.3903113616,
  248.006347259
 ],
 "python_version": "3.12.3",
 "seconds_per_simulation": 0.33,
 "notes": [
  "RC's text has two top-level 'class Strategy' statements, the later subclassing the earlier. assembled_sha256 renames only the last one to _RC, the one bound as the module's Strategy; renaming both would leave the second's base undefined. Found by counting regex matches and inspecting the imported class's MRO, without reading the source.",
  "off_equals_rc: per-callback action lists and total_edge identical on seeds 310000, 310001, 310002.",
  "Spread gate test with FO_SPREADS=(1,) and cancel-all, seeds 330200..330239: all 30 off-target seeds d exactly 0, all 10 target seeds d nonzero. The inferred s equalled the sampled spread on every tuning and holdout seed.",
  "Diagnostic on seeds 310000..310019 (cancel-all, cooldown 1): RC placed on the held side on 4184 of 4232 callbacks right after a hold, so it does re-quote.",
  "behind_cd1 and behind_cd2 are a reading of the brief's parenthesis \"now behind the rival in the queue\"; the brief's literal cancel-all reading is all_cd1 and all_cd2, measured on tuning only, since only the frozen variant goes to holdout.",
  "Screen only, not a promotion."
 ]
}