How the Overnight Clearing Desk Works

Under-the-hood reference for the classroom simulation. All game logic lives in pure functions in site/overnight-engine.js. The instructor page and student bank pages call the same engine so the rules stay identical. Units are $ billions for balance-sheet amounts and percent for rates (4.40 means 4.40%).

Starting state

When the instructor hosts a new game, the engine creates a fresh session with newState():

Students join from the lobby by entering a bank name. Each bank is created with newBank() → randomStartingBS(), which produces a unique but balanced sheet totaling ~$120B assets:

The sheet is immediately balanced. Free collateral for repo is marked Treasuries minus pledged. Banks can join at any time; their starting sheet is independent of the current day or phase.

1. Bank balance sheets & how they are generated

Each bank object holds the classic items plus a treasuryBook (face amounts by tenor) and a marked treasuries value. Key generation and mutation points:

Adjustable in the instructor UI? Yes for post-creation shocks:

These changes take effect immediately, including mid-session while the market is open. There is no “after-hours only” lock. The random ranges and coupon rates (sticky deposits 3.50%, floating assets = SOFR + 100 bp) are engine constants and are not UI-adjustable. Fixed loans have been removed; their residual is folded into Treasuries.

2. Yield curve & yield-curve history

The live curve is stored on the session as curve (six tenors). History is an array curveHistory of {day, curve} snapshots.

When adjustments take effect — the live Treasury load happens once at the start of a new session (instructor page). Manual curve edits, parallel/short shifts, and card-driven shifts take effect immediately (mid-game or after hours). The automatic SOFR-linked drift happens only when the instructor advances to the next day.

3. Situation cards

Named scenarios the instructor can apply at any time. Each card:

  1. Optionally overwrites the corridor rates (IORB / SRF).
  2. Applies a relative curveShift (or an absolute curve) and marks books.
  3. Draws a fixed pool of liquidity shocks and assigns them to banks according to a mode, then calls shockBank() on the volatile field (cash moves by the same amount).

The five built-in cards (from SITUATION_CARDS in the engine):

CardCorridorCurve shift (approx.)Shock shapeEconomic story
Pandemic shockIORB 0.15 / SRF 0.25Sharp parallel drop, front end most (−150 to −40 bp)Mostly large negative volatile shocks; a few positive; shuffledRisk-off freezes funding; Fed cuts hard. Most banks lose wholesale funds; a few strong franchises see inflows.
Hot inflation printIORB 5.40 / SRF 5.50Modest rise, front end most (+25 to +5 bp)Mild-to-moderate negatives; lowest-cash banks hit firstHot CPI forces a hike. Funding tightens; banks already short cash feel it most.
Liquidity crisis (2019-style)IORB 4.40 / SRF 5.00Short-end rise (+40 to −5 bp)A few very large negatives; most mild or zero; shuffledConcentrated squeeze. Corridor widens as the Fed emphasizes the SRF backstop.
Flight to safetyIORB 4.25 / SRF 4.35Long-end decline (0 to −35 bp)Mostly positive inflows; a couple modest negatives; shuffledRisk aversion drives cash into the banking system. Longer yields fall on flight to quality.
Quarter-end turnIORB 4.40 / SRF 4.55Mild short-end rise (+15 to 0 bp)Mild even negatives; largest / more wholesale-funded banks hit firstWindow-dressing and balance-sheet constraints create mild funding pressure into the turn.

All card effects (rates, curve, balance-sheet shocks) are applied immediately when the instructor clicks Apply Card. They are not restricted to the start of the game or to after-hours. The story text and per-bank shock amounts are stored on s.shock so every page can display them.

4. Tick & timer system

The instructor sets the real-time length of a trading day (default 5 minutes) and clicks Start Trading. That writes:

A client-side interval on the instructor page then computes the in-game tick:

When real time reaches timerEnd the market auto-closes (same path as a manual close). Pause/Resume freezes the elapsed-time calculation. The tick value is written back to Firebase so every client can see it.

Currently the tick is used for the clock display and for the auto-close trigger. The architecture is deliberately ready for future intraday events (for example, a scheduled curve tweak or balance-sheet drip at tick 20) because any client or the host can key off the shared tick field. No such intraday mutations exist today; all curve and balance changes are either instructor-driven or happen at the day boundary.

The timer length is chosen before (or at) the start of trading. Once the timer is running, only pause/resume is available; the length itself is not changed mid-session. Trading can also proceed without a timer (instructor closes manually).

5. Repo order book (overview)

Students submit orders from their bank page. A bid is a request to borrow cash overnight (pledge Treasuries); an offer is a request to lend cash overnight (receive Treasuries). Vocabulary follows the “price of cash” convention: higher bid / lower offer is more aggressive.

SOFR itself is the volume-weighted median of the day's private interbank trades (Fed operations are excluded), calculated at the close.

Reserve targets and tick-driven cash flows

generateReservePlans runs when a bank joins, when the instructor clicks Start Trading, when the instructor clicks Recalculate planned cashflows for next trading day, and with force=true at the end of every day inside advanceDay. Before each of those calls the instructor page copies the current values of the Banks-section controls (kSticky, kVolatile, cushion, fractionShort, clustering, vol step, vol prob) into gameParams; those controls are the sole source of truth. It computes each bank's risk-desk target as kSticky × sticky deposits + kVolatile × volatile funding + cushion (higher weight on volatile funding to reflect runoff risk) and assigns an assignedNetCashFlow for the day. The short cohort size is round(n * fractionShort) (default 0.40); those banks receive a net outflow large enough that they finish below target if they do nothing, and the rest receive a net inflow. A pre-computed sequence of tick-by-tick jumps is built so that the jumps sum exactly to the assigned net cash flow. Existing plans are left alone unless the call is forced.

Students see a live Reserve shortfall / surplus vs target line (current cash minus the day's target). They start the day above target. The instructor page shows each bank's assigned net cash flow; students do not. The host (instructor page) applies the pre-computed jumps as the shared tick advances, with catch-up so that lagged intervals do not skip intermediate flows. Mid-session joining is restricted: banks cannot be added while a trading day is actively running (timer started and market open).

Game loop — key phases

Start of trading day

After advanceDay() finishes, the session is in phase "trading", day has been incremented, the previous day's orders have been cleaned, timers and tick are reset, SOFR is null, and reserve plans have been force-regenerated for the new day. The instructor may optionally start the real-time timer (which begins the 5-minute tick clock and enables host application of the pre-computed cash-flow jumps). Students may already have banks or may join now. The curve is whatever was left by the previous day's drift (or the default on day 1). No automatic processes run until an order is submitted or the instructor acts.

During the trading day

End of trade day

Triggered by the instructor's “Close Market & Print SOFR” button or automatically when the timer expires. Host jump application stops with the timer. closeMarket():

No further matching occurs. Students can still see the tape, their sheets, and the curve, but cannot place new orders.

After hours

While the market is closed the instructor can still apply situation cards, curve changes, systemic shocks, or cash re-rolls (all take effect immediately). The primary “after hours” action is “Advance to Next Day”, which calls advanceDay():

The session is now ready for the next trading day. “New Game” is a stronger reset: it re-draws every bank's entire starting balance sheet with fresh randoms (keeping only the bank name) and returns to day 1 with a clean slate.

← back to the desk