{
  "key": "f-loss-budget-stop",
  "mech": "f-loss-budget-stop",
  "approach": "Blind loss-budget overlay on hash-verified N. Accumulate previous-step fill cashflow plus signed filled YES quantity at the current rival midpoint; below -80, latch a half-size stop for the rest of the run. Rebuild reduced desired orders and cap to available collateral. Act on all four spreads. Missing midpoint uses the last two-sided midpoint. A paired screen only; no promotion.",
  "tried": [
    {
      "implementation": "initial",
      "thresholds": [
        5,
        20,
        80
      ],
      "excluded_seeds": [
        320000,
        320001
      ],
      "reason": "L5 collateral failure exposed the need to rebuild reservations after resizing."
    },
    {
      "implementation": "repaired",
      "thresholds": [
        5,
        20,
        80
      ],
      "selection_seeds": [
        320002,
        320199
      ],
      "selected": "L80",
      "synthetic_checks": 6,
      "collateral_fixtures": 60,
      "limitation": "After the stop, CancelAll and replacement recompute reservations and reset retained orders queue priority."
    }
  ],
  "next": "Do not promote from this screen. Reject the overlay as screened; preserve incumbent N.",
  "spreads_acted": [
    1,
    2,
    3,
    4
  ],
  "off_equals_n": {
    "passed": true,
    "seeds": [
      310000,
      310009
    ],
    "steps_per_seed": [
      2000,
      2000,
      2000,
      2000,
      2000,
      2000,
      2000,
      2000,
      2000,
      2000
    ],
    "steps_compared": 20000,
    "repair_validation": "The entire disabled-path prefix is unchanged after collateral repair; 20,000 step comparisons remain applicable."
  },
  "code": "from orderbook_pm_challenge.types import PlaceOrder as _LBPlace, CancelAll as _LBAll, CancelOrder as _LBCancel\nimport math as _lbmath\n\nclass Strategy(_N):\n    # Realized fill edge marked once, at the next callback's rival midpoint.\n    loss_budget = 80.0\n    loss_budget_enabled = True\n    loss_budget_spreads = (1, 2, 3, 4)\n\n    def on_step(self, state):\n        actions = super().on_step(state)\n        if not self.loss_budget_enabled or self.s not in self.loss_budget_spreads:\n            return actions\n        bid = state.competitor_best_bid_ticks\n        ask = state.competitor_best_ask_ticks\n        mid = (bid + ask) / 200.0 if bid is not None and ask is not None else None\n        if not hasattr(self, '_lb_previous'):\n            self._lb_previous = (state.cash, state.no_inventory)\n            self._lb_edge = 0.0\n            self._lb_stopped = False\n            self._lb_mid = mid\n        old_cash, old_no = self._lb_previous\n        mark = mid if mid is not None else self._lb_mid\n        if mark is not None:\n            # NO minting consumes one cash dollar; add it back to recover\n            # the signed YES trade cashflow, including uncovered sells.\n            self._lb_edge += (state.cash - old_cash + state.no_inventory - old_no\n                             + mark * (state.buy_filled_quantity - state.sell_filled_quantity))\n        self._lb_previous = (state.cash, state.no_inventory)\n        if mid is not None:\n            self._lb_mid = mid\n        first_stop = not self._lb_stopped and self._lb_edge < -self.loss_budget\n        self._lb_stopped = self._lb_stopped or first_stop\n        if not self._lb_stopped:\n            return actions\n        # Rebuild the desired book to recompute collateral after resizing.\n        # Existing reduced orders retain their size after the initial stop.\n        desired = {}\n        for order in state.own_orders:\n            q = order.remaining_quantity * (0.5 if first_stop else 1.0)\n            desired[order.order_id] = _LBPlace(order.side, order.price_ticks, q, order.order_id)\n        new_index = 0\n        for action in actions:\n            if isinstance(action, _LBAll):\n                desired.clear()\n            elif isinstance(action, _LBCancel):\n                desired.pop(action.order_id, None)\n            elif isinstance(action, _LBPlace):\n                new_index += 1\n                key = action.client_order_id if action.client_order_id is not None else ('new', new_index)\n                desired[key] = _LBPlace(action.side, action.price_ticks, action.quantity * 0.5, action.client_order_id)\n        result = [_LBAll()]\n        cash = max(0.0, state.cash)\n        yes = max(0.0, state.yes_inventory)\n        for order in desired.values():\n            price = order.price_ticks / 100.0\n            is_buy = order.side.value == 'BUY'\n            capacity = cash / price if is_buy else yes + cash / (1.0 - price)\n            quantity = _lbmath.floor(min(order.quantity, capacity) * 100.0 + 1e-9) / 100.0\n            if quantity < 0.01:\n                continue\n            if is_buy:\n                cash -= round(price * quantity, 10)\n            else:\n                covered = min(yes, quantity)\n                yes -= covered\n                cash -= round((1.0 - price) * (quantity - covered), 10)\n            cash = max(0.0, cash)\n            result.append(_LBPlace(order.side, order.price_ticks, quantity, order.client_order_id))\n        return result\n",
  "code_sha256": "7c8d907a83b66c8a909633b2631200c73008c93e915fd7d4cb7e30e27abe2d78",
  "assembled_sha256": "8fa1ffbf3ac07d76bdf9eef344a6c5c669d6df7838adc9386baecc9cd0f294f3",
  "tuning": {
    "seeds": {
      "allowed": [
        320000,
        320199
      ],
      "used_for_selection": [
        320002,
        320199
      ],
      "n": 198,
      "excluded": [
        320000,
        320001
      ]
    },
    "incumbent_code_sha256": "bf0d179f3e47d28819f6fa7e0d25548c2ced43374c216d36460b00a26a26ffae",
    "selected": {
      "name": "L80",
      "L": 80,
      "criterion": "Largest paired mean total-edge difference on the 198 common repaired tuning seeds; no spread selection."
    },
    "variants": [
      {
        "name": "L5",
        "L": 5,
        "overall": {
          "n": 198,
          "sum_d": -423.38724175999994,
          "sum_d2": 225888.70810987693,
          "mean_diff": -2.1383194028282824,
          "se": 2.4016488110539744,
          "incumbent_mean": 97.34844006532626,
          "candidate_mean": 95.21012066249799,
          "retail_diff": -2.6457229865393943,
          "arb_diff": 0.5074035837237373
        },
        "by_spread": {
          "1": {
            "n": 50,
            "sum_d": -3.8569249038000017,
            "sum_d2": 6.483577689469743,
            "mean_diff": -0.07713849807600004,
            "se": 0.0502486076371162,
            "incumbent_mean": 25.711261479664,
            "candidate_mean": 25.634122981588,
            "retail_diff": -0.08534631316799993,
            "arb_diff": 0.008207815106000067
          },
          "2": {
            "n": 49,
            "sum_d": 0.0,
            "sum_d2": 0.0,
            "mean_diff": 0.0,
            "se": 0.0,
            "incumbent_mean": 55.82961138880204,
            "candidate_mean": 55.82961138880204,
            "retail_diff": 0.0,
            "arb_diff": 0.0
          },
          "3": {
            "n": 48,
            "sum_d": -301.4900187499,
            "sum_d2": 84466.00139107928,
            "mean_diff": -6.281042057289583,
            "se": 6.049893392745826,
            "incumbent_mean": 130.8342429047854,
            "candidate_mean": 124.55320084749583,
            "retail_diff": -4.778390320404167,
            "arb_diff": -1.5026517368562502
          },
          "4": {
            "n": 51,
            "sum_d": -118.04029810629996,
            "sum_d2": 141416.22314110817,
            "mean_diff": -2.3145156491431362,
            "se": 7.4397716869857735,
            "incumbent_mean": 175.9555576758902,
            "candidate_mean": 173.64104202674707,
            "retail_diff": -5.690649025431373,
            "arb_diff": 3.376133376296078
          }
        }
      },
      {
        "name": "L20",
        "L": 20,
        "overall": {
          "n": 198,
          "sum_d": -15.546185478399986,
          "sum_d2": 235.66032950537777,
          "mean_diff": -0.0785160882747474,
          "se": 0.07752644573359727,
          "incumbent_mean": 97.34844006532626,
          "candidate_mean": 97.26992397705152,
          "retail_diff": -0.06086386932929289,
          "arb_diff": -0.017652218940909226
        },
        "by_spread": {
          "1": {
            "n": 50,
            "sum_d": -0.024260510600000007,
            "sum_d2": 0.0005885723745727127,
            "mean_diff": -0.00048521021200000015,
            "se": 0.0004852102120000001,
            "incumbent_mean": 25.711261479664,
            "candidate_mean": 25.710776269452,
            "retail_diff": -0.0011758246039999998,
            "arb_diff": 0.0006906143980000001
          },
          "2": {
            "n": 49,
            "sum_d": 0.0,
            "sum_d2": 0.0,
            "mean_diff": 0.0,
            "se": 0.0,
            "incumbent_mean": 55.82961138880204,
            "candidate_mean": 55.82961138880204,
            "retail_diff": 0.0,
            "arb_diff": 0.0
          },
          "3": {
            "n": 48,
            "sum_d": -15.3502530825,
            "sum_d2": 235.63026969680075,
            "mean_diff": -0.31979693921875,
            "se": 0.31979693921874996,
            "incumbent_mean": 130.8342429047854,
            "candidate_mean": 130.51444596556667,
            "retail_diff": -0.24609222532083333,
            "arb_diff": -0.07370471388541677
          },
          "4": {
            "n": 51,
            "sum_d": -0.17167188529998612,
            "sum_d2": 0.029471236202451588,
            "mean_diff": -0.0033661153980389436,
            "se": 0.003366115398038943,
            "incumbent_mean": 175.9555576758902,
            "candidate_mean": 175.95219156049217,
            "retail_diff": -0.003526040815686139,
            "arb_diff": 0.00015992541764663816
          }
        }
      },
      {
        "name": "L80",
        "L": 80,
        "overall": {
          "n": 198,
          "sum_d": -0.17167188529998612,
          "sum_d2": 0.029471236202451588,
          "mean_diff": -0.0008670297237373037,
          "se": 0.0008670297237373037,
          "incumbent_mean": 97.34844006532626,
          "candidate_mean": 97.34757303560252,
          "retail_diff": -0.0009082226343433994,
          "arb_diff": 4.119291060595226e-05
        },
        "by_spread": {
          "1": {
            "n": 50,
            "sum_d": 0.0,
            "sum_d2": 0.0,
            "mean_diff": 0.0,
            "se": 0.0,
            "incumbent_mean": 25.711261479664,
            "candidate_mean": 25.711261479664,
            "retail_diff": 0.0,
            "arb_diff": 0.0
          },
          "2": {
            "n": 49,
            "sum_d": 0.0,
            "sum_d2": 0.0,
            "mean_diff": 0.0,
            "se": 0.0,
            "incumbent_mean": 55.82961138880204,
            "candidate_mean": 55.82961138880204,
            "retail_diff": 0.0,
            "arb_diff": 0.0
          },
          "3": {
            "n": 48,
            "sum_d": 0.0,
            "sum_d2": 0.0,
            "mean_diff": 0.0,
            "se": 0.0,
            "incumbent_mean": 130.8342429047854,
            "candidate_mean": 130.8342429047854,
            "retail_diff": 0.0,
            "arb_diff": 0.0
          },
          "4": {
            "n": 51,
            "sum_d": -0.17167188529998612,
            "sum_d2": 0.029471236202451588,
            "mean_diff": -0.0033661153980389436,
            "se": 0.003366115398038943,
            "incumbent_mean": 175.9555576758902,
            "candidate_mean": 175.95219156049217,
            "retail_diff": -0.003526040815686139,
            "arb_diff": 0.00015992541764663816
          }
        }
      }
    ]
  },
  "paired": {
    "seeds": [
      430000,
      430499
    ],
    "n": 500,
    "complete": true,
    "missing_seeds": [],
    "loss_stop_triggered_runs": 7,
    "loss_stop_triggered_runs_by_spread": {
      "1": 3,
      "2": 1,
      "3": 1,
      "4": 2
    },
    "overall": {
      "n": 500,
      "sum_d": -27.144200838799996,
      "sum_d2": 264.81200637815664,
      "mean_diff": -0.05428840167759999,
      "se": 0.03248791303525023,
      "incumbent_mean": 95.5420151134666,
      "candidate_mean": 95.487726711789,
      "retail_diff": -0.02120072151219992,
      "arb_diff": -0.03308768016500005
    },
    "by_spread": {
      "1": {
        "n": 131,
        "sum_d": -18.098733838099996,
        "sum_d2": 168.21440675502728,
        "mean_diff": -0.13815827357328242,
        "se": 0.09864433908360343,
        "incumbent_mean": 38.277936657049615,
        "candidate_mean": 38.13977838347633,
        "retail_diff": -0.0230038507648853,
        "arb_diff": -0.11515442280839636
      },
      "2": {
        "n": 120,
        "sum_d": -9.706119312799956,
        "sum_d2": 94.20875211430828,
        "mean_diff": -0.08088432760666629,
        "se": 0.08088432760666629,
        "incumbent_mean": 77.8558643979175,
        "candidate_mean": 77.77498007031083,
        "retail_diff": -0.06061443020499991,
        "arb_diff": -0.02026989740000052
      },
      "3": {
        "n": 133,
        "sum_d": -0.0037975532000018575,
        "sum_d2": 1.4421410306844347e-05,
        "mean_diff": -2.8553031578961335e-05,
        "se": 2.855303157896133e-05,
        "incumbent_mean": 146.55140363646618,
        "candidate_mean": 146.55137508343458,
        "retail_diff": -0.00043471215112765636,
        "arb_diff": 0.000406159119548695
      },
      "4": {
        "n": 116,
        "sum_d": 0.6644498652999573,
        "sum_d2": 2.3888330874107866,
        "mean_diff": 0.005728016080172046,
        "se": 0.013371149512016709,
        "incumbent_mean": 120.02204692465259,
        "candidate_mean": 120.02777494073275,
        "retail_diff": -0.0022009307344829127,
        "arb_diff": 0.007928946814655051
      }
    }
  },
  "failures": [
    {
      "phase": "initial tuning implementation",
      "seed": 320001,
      "strategy": "L5",
      "error": "orderbook_pm_challenge.market.OrderBookError: Insufficient free cash for order collateral",
      "resolution": "Rebuilt the reduced desired book with collateral caps. Initial seeds 320000 and 320001 excluded from selection; no seed/strategy pair rerun. Repaired variants evaluated on 320002..320199."
    }
  ],
  "check_values": [
    17.1160017503,
    20.0171113118,
    2.3903113616,
    241.4847522404
  ],
  "python_version": "3.14.4",
  "seconds_per_simulation": 0.32186038094046154
}
