{
  "key": "f-fill-imbalance",
  "approach": {
    "mechanism": "Rolling signed fill imbalance: event=(sell_filled_quantity-buy_filled_quantity)/(sell_filled_quantity+buy_filled_quantity), zero on no fills. A positive window sum boosts bids; a negative sum boosts asks. A same-direction nonzero event with absolute window sum at least the threshold starts or refreshes the duration. Added size is at the exact prices of N orders left resting after its actions.",
    "architecture": "S1 owns one unread N instance, calls it each step, hides only overlay order IDs with dataclasses.replace, cancels surviving overlay orders before applying N actions, and appends collateral-capped size. No quote prices are changed. Outside selected first-step spreads N actions are returned directly.",
    "incumbent_sha256": "bf0d179f3e47d28819f6fa7e0d25548c2ced43374c216d36460b00a26a26ffae",
    "seed_310000_expected": 17.116002,
    "seed_310000_measured": 17.1160017503,
    "seed_310000_matches": true,
    "spread": "max(1,(first_competitor_ask-first_competitor_bid+1)//2); missing first quote disables overlay",
    "selection": "Two fixed variants on 310000..310199. For each, select spreads with tuning mean_diff > one SE, then choose the variant maximizing their contribution across all 200 seeds. If none qualify, choose the least harmful single spread/variant. Frozen before holdout; excluded tuning spreads have exact zero differences.",
    "frozen_parameters": {
      "FI_WINDOW": 12,
      "FI_THRESHOLD": 3.0,
      "FI_DURATION": 6,
      "FI_MULTIPLIER": 1.5,
      "FI_CASH_FRACTION": 0.05
    },
    "checks": "Disabled overlay: identical callback state/action traces and SimulationResult on 310000..310009. Assembled versus direct S1: identical traces and results on 310000..310004. Simulator unmodified at b244530ee2a6247b5f930830eeca9dd950da0e7e; default ChallengeConfig and ParameterVariance, 2000 steps; serial in-process, no sandbox. No incumbent source or other strategy/results were inspected.",
    "uncertainty": "Paired differences include all 500 seeds. SE is sample SD of per-seed edge differences divided by sqrt(n). By-spread grouping uses observed first-step spread, not hidden configuration."
  },
  "tried": [
    {
      "variant": 1,
      "parameters": {
        "FI_WINDOW": 12,
        "FI_THRESHOLD": 3.0,
        "FI_DURATION": 6,
        "FI_MULTIPLIER": 1.5,
        "FI_CASH_FRACTION": 0.05
      },
      "tuning_first_seed": 310000,
      "tuning_last_seed": 310199,
      "tuning_all_spreads": {
        "runs": 200,
        "incumbent_mean": 85.162644703073,
        "mean_diff": -1.406641566658,
        "se": 0.294345285786857,
        "sum_d": -281.32831333160004,
        "sum_d2": 3843.966160554886,
        "retail_diff": 0.7833603052574993,
        "arb_diff": -2.190001871931
      },
      "by_spread": {
        "1": {
          "runs": 47,
          "incumbent_mean": 30.59600304578298,
          "mean_diff": -1.3639144974553192,
          "se": 0.5678406220521753,
          "sum_d": -64.1039813804,
          "sum_d2": 784.5540551270576,
          "retail_diff": -0.22046576952127916,
          "arb_diff": -1.1434487279617023
        },
        "2": {
          "runs": 57,
          "incumbent_mean": 81.35713208259824,
          "mean_diff": -1.0400540685947364,
          "se": 0.3941074342389032,
          "sum_d": -59.28308190989997,
          "sum_d2": 557.441188293036,
          "retail_diff": 0.8028678186508753,
          "arb_diff": -1.8429218872368422
        },
        "3": {
          "runs": 53,
          "incumbent_mean": 115.06691980369057,
          "mean_diff": -1.663250833052831,
          "se": 0.5868475797223542,
          "sum_d": -88.15229415180005,
          "sum_d2": 1095.7584421959143,
          "retail_diff": 1.0529959199,
          "arb_diff": -2.716246752967924
        },
        "4": {
          "runs": 43,
          "incumbent_mean": 112.99101207346745,
          "mean_diff": -1.6229989741744197,
          "se": 0.846118448677502,
          "sum_d": -69.78895588950004,
          "sum_d2": 1406.2124749388777,
          "retail_diff": 1.5223651814232575,
          "arb_diff": -3.1453641556325582
        }
      },
      "selected": true
    },
    {
      "variant": 2,
      "parameters": {
        "FI_WINDOW": 24,
        "FI_THRESHOLD": 5.0,
        "FI_DURATION": 12,
        "FI_MULTIPLIER": 2.0,
        "FI_CASH_FRACTION": 0.1
      },
      "tuning_first_seed": 310000,
      "tuning_last_seed": 310199,
      "tuning_all_spreads": {
        "runs": 200,
        "incumbent_mean": 85.162644703073,
        "mean_diff": -3.2154013670290005,
        "se": 0.5547019865955134,
        "sum_d": -643.0802734058001,
        "sum_d2": 14313.994088752155,
        "retail_diff": 1.1954795905675004,
        "arb_diff": -4.4108809576645
      },
      "by_spread": {
        "1": {
          "runs": 47,
          "incumbent_mean": 30.59600304578298,
          "mean_diff": -1.7551281364659566,
          "se": 0.6842976210880832,
          "sum_d": -82.49102241389996,
          "sum_d2": 1157.1674268428426,
          "retail_diff": 0.07429368788510504,
          "arb_diff": -1.8294218246787237
        },
        "2": {
          "runs": 57,
          "incumbent_mean": 81.35713208259824,
          "mean_diff": -2.2636153478210534,
          "se": 0.7206996577285816,
          "sum_d": -129.02607482580004,
          "sum_d2": 1950.0157285518908,
          "retail_diff": 1.3811489240491222,
          "arb_diff": -3.644764271819298
        },
        "3": {
          "runs": 53,
          "incumbent_mean": 115.06691980369057,
          "mean_diff": -4.188669155824529,
          "se": 1.1132520896074165,
          "sum_d": -221.99946525870004,
          "sum_d2": 4345.476385320766,
          "retail_diff": 1.3959927554188691,
          "arb_diff": -5.584661911239622
        },
        "4": {
          "runs": 43,
          "incumbent_mean": 112.99101207346745,
          "mean_diff": -4.873574672265118,
          "se": 1.7982411356102719,
          "sum_d": -209.56371090740006,
          "sum_d2": 6861.334548036656,
          "retail_diff": 1.9276979087186068,
          "arb_diff": -6.801272581013953
        }
      },
      "selected": false
    }
  ],
  "next": "Do not promote this overlay: its paired holdout gain does not exceed twice its standard error. No finalist seeds were run.",
  "spreads_acted": [
    2
  ],
  "off_equals_n": true,
  "code": "from collections import deque as _FIdeque\nfrom dataclasses import replace as _FIreplace\nfrom math import floor as _FIfloor\nfrom orderbook_pm_challenge.types import PlaceOrder as _FIPlace, CancelOrder as _FICancel, CancelAll as _FIAll, Side as _FISide, OwnOrderView as _FIView\n\n\nclass Strategy(_N):\n    # Frozen after tuning; the harness can override these class attributes.\n    FI_ENABLED = True\n    FI_SPREADS = (2,)\n    FI_WINDOW = 12\n    FI_THRESHOLD = 3.0\n    FI_DURATION = 6\n    FI_MULTIPLIER = 1.5\n    FI_CASH_FRACTION = 0.05\n\n    def __init__(self):\n        self._fi_n = _N()\n        self._fi_spread = None\n        self._fi_history = _FIdeque(maxlen=self.FI_WINDOW)\n        self._fi_added = set()\n        self._fi_side = None\n        self._fi_until = -1\n        self._fi_serial = 0\n\n    def on_step(self, state):\n        if self._fi_spread is None:\n            bid = state.competitor_best_bid_ticks\n            ask = state.competitor_best_ask_ticks\n            self._fi_spread = max(1, (ask - bid + 1) // 2) if bid is not None and ask is not None else -1\n        if not self.FI_ENABLED or self._fi_spread not in self.FI_SPREADS:\n            return self._fi_n.on_step(state)\n\n        hidden = tuple(o for o in state.own_orders if o.order_id in self._fi_added)\n        visible = tuple(o for o in state.own_orders if o.order_id not in self._fi_added)\n        actions = list(self._fi_n.on_step(_FIreplace(state, own_orders=visible)))\n\n        buy = state.buy_filled_quantity\n        sell = state.sell_filled_quantity\n        event = (sell - buy) / (sell + buy) if sell + buy > 1e-9 else 0.0\n        self._fi_history.append(event)\n        imbalance = sum(self._fi_history)\n        if abs(imbalance) >= self.FI_THRESHOLD and event * imbalance > 0:\n            self._fi_side = _FISide.BUY if imbalance > 0 else _FISide.SELL\n            self._fi_until = state.step + self.FI_DURATION\n\n        # Each added order lives for one callback interval. N cannot cancel it\n        # by id, and sees every fill and the actual cash/inventory balances.\n        prefix = [_FICancel(o.order_id) for o in hidden]\n        self._fi_added.clear()\n        if state.step >= self._fi_until:\n            return prefix + actions\n\n        # Lower bound on free collateral after N's actions. Covered sells can\n        # release less cash than their full complement notional; count only\n        # the release guaranteed by total YES inventory. No simulator internals.\n        free = state.free_cash\n        orders = {o.order_id: o for o in state.own_orders}\n        for action in prefix + actions:\n            if isinstance(action, _FIAll):\n                free = state.cash\n                orders.clear()\n            elif isinstance(action, _FICancel):\n                order = orders.pop(action.order_id, None)\n                if order is not None:\n                    q = order.remaining_quantity\n                    release = q * order.price_ticks / 100 if order.side == _FISide.BUY else max(0.0, q - state.yes_inventory) * (1 - order.price_ticks / 100)\n                    free += release\n            elif isinstance(action, _FIPlace):\n                q = _FIfloor(action.quantity * 100 + 1e-8) / 100\n                p = action.price_ticks / 100\n                reserved_yes_upper = sum(o.remaining_quantity for o in orders.values() if o.side == _FISide.SELL)\n                yes_lower = max(0.0, state.yes_inventory - reserved_yes_upper)\n                cost = q * p if action.side == _FISide.BUY else max(0.0, q - yes_lower) * (1 - p)\n                free -= cost\n                # A local view solely for subsequent reservation accounting.\n                key = action.client_order_id or ('_fi_virtual_' + str(len(orders)))\n                orders[key] = _FIView(key, action.side, action.price_ticks, q, state.step)\n\n        budget = max(0.0, min(free - 0.05, state.cash * self.FI_CASH_FRACTION))\n        extras = []\n        # Only enlarge quotes N actually leaves resting, at their exact price.\n        for order in tuple(orders.values()):\n            if order.side != self._fi_side or budget <= 0:\n                continue\n            unit = order.price_ticks / 100 if order.side == _FISide.BUY else 1 - order.price_ticks / 100\n            quantity = min(order.remaining_quantity * (self.FI_MULTIPLIER - 1), budget / unit)\n            quantity = _FIfloor(quantity * 100) / 100\n            if quantity < 0.01:\n                continue\n            self._fi_serial += 1\n            key = '_fi_extra_' + str(self._fi_serial)\n            extras.append(_FIPlace(order.side, order.price_ticks, quantity, key))\n            self._fi_added.add(key)\n            budget -= quantity * unit\n        return prefix + actions + extras\n",
  "code_sha256": "2ebc1d2958aaa086ba3467b9295a07a0b9fc9cd94e882f4ce2148ef66d6306bf",
  "assembled_sha256": "88d65086163c21f6e5bd808a8509b1530b49331cee3f128dd51a3d974c561804",
  "tuning": {
    "mean_diff": -0.29641540954949985,
    "se": 0.11646691350741475,
    "n": 200
  },
  "paired": {
    "first_seed": 410000,
    "last_seed": 410499,
    "n": 500,
    "complete": true,
    "incumbent_mean": 104.39217582931519,
    "mean_diff": -0.29910455828379984,
    "se": 0.09448538194529064,
    "sum_d": -149.55227914189993,
    "sum_d2": 2272.1398750292637,
    "retail_diff": 0.25743266281519955,
    "arb_diff": -0.5565372210972002,
    "failures": 0
  },
  "by_spread": {
    "1": {
      "runs": 115,
      "incumbent_mean": 28.495164940043477,
      "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.76628686140518,
      "mean_diff": -1.289243785706034,
      "se": 0.39480609634502734,
      "sum_d": -149.55227914189993,
      "sum_d2": 2272.1398750292637,
      "retail_diff": 1.1096235466172393,
      "arb_diff": -2.398867332315518
    },
    "3": {
      "runs": 138,
      "incumbent_mean": 128.40565590283623,
      "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.5898790537267,
      "mean_diff": 0.0,
      "se": 0.0,
      "sum_d": 0.0,
      "sum_d2": 0.0,
      "retail_diff": 0.0,
      "arb_diff": 0.0
    }
  },
  "seconds_per_simulation": 1.6132285492909142
}
