# HF across prices — rebalancing scenario

The user chose the rebalancing scenario only. This is a fixed-time estimate from
the known hedge curve, anchored to current supplied assets and actual debt. It
is not a projected series of executed CoW orders or a funding/borrowing approval.

## Inputs and account checks

`positions.js` retains notional, strike, expiry, underlying units, IV, Aave prices
and account LT/LTV from the same lens snapshot. `risk-reader.js` resolves every
active collateral/borrowing reserve by its ID, reads its parameters, debt token
balance and oracle price at that same block, then reconciles reconstructed
collateral, debt and weighted LT/LTV against the lens. Reserve-ID holes are kept.

Both debt assets matter. Borrowed base reprices with the x-axis; quote and other
asset prices stay fixed. Collateral eligibility flags are respected. Aave E-mode,
unreconciled balances or missing inputs make the chart unavailable while leaving
the position card available. A final block-hash check rejects a reorg during the
model read. Scope: Base V3 and variable debt, matching the current pair interface.

Sources: `thetaAaveCattle/src/interfaces/IAavePool.sol`, the pair and lens;
[Aave IPool](https://github.com/aave/aave-v3-origin/blob/main/src/contracts/interfaces/IPool.sol).
The additional reads are read-only and do not affect indexer coverage.

## Hedge curve and value

Let `s` be base price in quote tokens, `K` strike, `N` fixed strategy notional,
and `C(s)` the zero-rate Black–Scholes call value at fixed IV and time. Time is
quantized at the contract's next one-hour epoch boundary.

```text
Long target base  b(s) = N Φ(d1)
Short target base b(s) = N [1 − Φ(d1)]

Long value  V(s) = V(s0) + N [C(s) − C(s0)]
Short value V(s) = V(s0) + N [s − s0 − C(s) + C(s0)]
Quote holdings q(s) = V(s) − s b(s)
```

`V(s0)` comes from current holdings valued in quote units. The hypothetical
initial rebalance preserves that value. It may change weighted collateral when
the two reserves have different liquidation thresholds. Consequently, the curve
at today's price is labelled **after model rebalancing**; actual account HF stays
in the account grid above it. The x-axis current price uses the Aave base/quote
price ratio. The quote token is not assumed to be worth exactly one USD.

This conserves value; setting quote holdings to `(N−b)×K` would not. The formulas
above apply while the target is affordable. The chart models a **one-way price
move from today**, with no extra borrowing to finance trades.

### When quote assets run out

The first implementation stopped the curve when ideal quote holdings became
negative. The user correctly identified that as an execution funding limit, not
undefined HF: wstETH collateral can continue appreciating against USDC debt.

If the initial target is affordable, follow the self-financing path until quote
holdings reach zero at `sf`. Beyond that boundary, in the direction where the
target requires more buying, keep `Bf = b(sf)` base and zero quote. Value becomes
`Bf × s`; existing debt continues to reprice. This affects long on an increasing
price path and short on a decreasing price path. No negative cash or new debt is
introduced. With quote-only debt, HF can continue growing without a ceiling as
base price increases under the fixed assumptions. Base-denominated debt can
instead give HF a finite upper limit.

If today's target is already unaffordable, start with `B0 = V(s0)/s0` base and
zero quote. Keep those holdings until the target decreases to `B0` at a join
price `sj`. Reanchor the hedge integral at that point:

```text
F(s) = N C(s) for long; N [s − C(s)] for short
V(s) = B0 sj + F(s) − F(sj)
q(s) = V(s) − s b(s)
```

Reusing the original negative-cash integral would assume an unfunded initial
trade. The implementation instead solves the affordable join using the target
delta and evaluates quote as a stable difference of `N K Φ(d2)` values. Holdings,
value, HF and borrowing capacity remain continuous at both kinds of boundary.
A price reversal after quote exhaustion would follow a different path; this
chart does not model that history.

Expiry processing, zero IV and absent funded holdings remain outside this
continuous scenario. Spread earnings, interest, fees, slippage, discrete grid
steps and execution delays are excluded.

## Risk lines

For each eligible supplied asset, let `t` be LT and `l` be LTV as fractions.

```text
C = Σ supplied × price
W = Σ supplied × price × t
A = Σ supplied × price × l
D = Σ debt × price

HF = W / D
Actual LTV = D / C
LTV limit  = A / C
Borrowing line in HF units = W / A
```

Liquidation is marked at HF 1. The borrowing boundary is `D=A`; it is not a fixed
LTV percentage placed on an HF axis. Caps, liquidity and further Aave restrictions
can limit borrowing before this boundary. The chart does not certify outgoing
trade settlement. No debt has no finite HF or liquidation price.

Borrow and repay scenarios modify only the selected debt quantity, starting from
the current snapshot each time. Borrowed proceeds remain outside the position;
repayment funds come from outside it. Over-repayment is refused. Current account
figures, wallet identity and supplied collateral remain the actual snapshot.

## Curves and crossings

The shape follows strategy, reserve weights and debt mix. It is not selected by a
long/short label. The fictional long example uses 0.25 wstETH + 220 USDC debt and
has a U-shaped HF curve. The fictional short uses USDC debt and bends downward
toward liquidation as price falls. The current live long's USDC-only debt produces
a different curve. Samples include consistent synthetic account and reserve data.

Uniform plot samples alone can miss two crossings around a shallow minimum.
`risk-model.js` finds stationary points before bracketing crossings. Since debt
is affine in price, the HF slope numerator has derivative `W''D`. `W''` and `A''`
change sign at the analytic log-price inflection derived from their reserve
weights. The search splits at those points, finds extrema, then brackets both
sides of HF 1 and the borrowing boundary. Funding joins are also explicit search
and plot boundaries: a join can be a local extremum with a discontinuous slope.
Derivative searches use the appropriate one-sided derivative at each join. On
the segment with no quote left, base holdings stay constant, HF is fractional
linear and the borrowing-limit difference is affine in price.

Independent Astra review verified this fix. The regression has a true minimum
below HF 1 while all 161 ordinary samples sit above 1, and still finds both roots.
Other tests cover reference BS values, self-financing trades, debt repricing,
borrow/repay isolation, reserve flags and account reconciliation. The funding
correction is compared with 20,000 separately executed affordable trades for
each price direction, for long and short, including initially underfunded
targets. Tests also check boundary continuity, quote-only HF growth, the
base-debt asymptote, and liquidation/borrowing crossings after quote exhaustion.

## Identity and future flows

Position titles show `notional / baseUnit`, rounded with an approximation marker,
and distinguish it from a leverage multiple. Current configuration encodes long
call-like and covered-short modes, not a separate Call/Put subtype. Borrowed base
can change net exposure without changing that encoded strategy. Put naming must
not be inferred from debt mix alone.

Borrowing follows deployment and funding as a separate owner decision. See
[ROLL-PLAN.md](ROLL-PLAN.md) for the studied destination-review and activation flow.
