Coverage for portfolio/forex_sleeve.py: 100%
8 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""Forex sleeve (V5.7.0): a small, concentrated forex book with its own gross budget.
3Why a sleeve: forex was switched off because, as members of the pooled 6+6 equity book, the pairs sat at ~2% of NAV and
4IB's $2 minimum commission cost ~8-10 bps per fill (Problems.md #142). A sleeve ranks the pairs among THEMSELVES, holds
5`k` longs and `k` shorts, gives each leg a budget big enough that the minimum is small, rebalances slowly and skips
6resizes below a dollar threshold. Since V5.8.0 the weights come from the generic `portfolio/class_sleeve.py` (bonds use
7the same structure); these names stay as the forex entry points `main.py` and the research scripts import.
9Pure, no I/O, no Lean imports.
10"""
12from __future__ import annotations
14from collections.abc import Mapping
16from portfolio.book_construction import BookAllocation
17from portfolio.class_sleeve import build_class_sleeve_weights, resize_below_minimum
20def build_forex_sleeve_weights(
21 allocations: Mapping[str, BookAllocation], gross_budget: float, max_weight_per_pair: float | None = None
22) -> dict[str, float]:
23 """Signed target weight (fraction of NAV) per selected pair: each leg gets `gross_budget / 2`, split evenly over its
24 names, optionally capped per pair. A one-sided book (no shorts, no longs) gets {} - the sleeve is dollar-neutral by
25 construction, never a directional currency bet. Non-finite or non-positive budget -> {}."""
26 return build_class_sleeve_weights(allocations, gross_budget, max_weight_per_pair)
29def forex_resize_below_minimum(delta_units: float, price: float, min_trade_usd: float) -> bool:
30 """True when a resize of `delta_units` base-currency units at `price` is worth less than `min_trade_usd` in notional:
31 paying IB's dollar minimum to move such a small amount costs more bps than the move is worth. `min_trade_usd <= 0`
32 disables the check (False). Non-finite inputs -> True (do not trade on garbage)."""
33 return resize_below_minimum(delta_units, price, min_trade_usd)