Coverage for portfolio/options_assignment_risk.py: 100%
38 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"""Early-call-assignment-risk signal (V4.7, development/Problems.md -
2early-assignment probability/pricing modeling) - a genuinely distinct
3concern from options_strategy.py's sizing/selection logic, so it lives in
4its own module rather than folded in there.
6Pure, stdlib-only functions (math only - no scipy/numpy, same convention
7as features/options_greeks.py and every other portfolio/risk module),
8every function degrades to a neutral "no risk detected" default (0.0/
9False) on missing input rather than raising - main.py's
10_apply_option_assignment_risk_sweep() is what actually acts on this
11module's output, this module only ever computes the signal.
13Scope, deliberately narrow: this module ONLY addresses dividend-driven
14early exercise of a SHORT CALL leg - the one shape where the classic
15textbook early-exercise-optimality rule (exercise early only when the
16option's extrinsic value is less than the dividend about to be captured)
17applies. A short PUT approaching expiry deep ITM is a pin-risk concern
18already partly covered by portfolio/options_strategy.py::option_auto_close_due()'s
19calendar-days-to-expiry safety net, not a dividend-driven concern - out of
20scope here by construction (assignment_risk_score() below always scores a
21put at 0.0).
22"""
24from __future__ import annotations
26import math
27from typing import TYPE_CHECKING
29if TYPE_CHECKING:
30 from datetime import date
32_SQRT_2PI = math.sqrt(2.0 * math.pi)
35def _normal_cdf(x: float) -> float:
36 return 0.5 * (1.0 + math.erf(x / math.sqrt(2.0)))
39def extrinsic_value(option_price: float | None, spot: float, strike: float, right: str) -> float | None:
40 """option_price minus intrinsic value. Returns None (not 0.0) when
41 option_price is missing/non-positive - "unknown extrinsic value" must
42 never be indistinguishable from "zero extrinsic value" (a real,
43 meaningful early-exercise trigger condition in its own right), the
44 same None-vs-neutral-default distinction features/bond_features.py's
45 module docstring already establishes for this codebase."""
46 if option_price is None or option_price <= 0.0:
47 return None
48 intrinsic = max(spot - strike, 0.0) if right.lower() == "call" else max(strike - spot, 0.0)
49 return option_price - intrinsic
52def early_exercise_optimal_for_dividend(extrinsic_value: float | None, expected_dividend_amount: float | None) -> bool:
53 """Classic textbook rule: early call exercise is optimal only when the
54 option's extrinsic value is LESS than the dividend the holder would
55 capture by exercising before ex-div (they give up less time value than
56 they gain in dividend). Returns False (never optimal) when either
57 input is None - an unknown value never triggers a false-positive risk
58 flag."""
59 if extrinsic_value is None or expected_dividend_amount is None:
60 return False
61 return extrinsic_value < expected_dividend_amount
64def days_to_next_ex_dividend(current_date: date | None, next_ex_dividend_date: date | None) -> int | None:
65 """Pure date arithmetic, mirrors options_strategy.option_auto_close_due()'s
66 own calendar-day-diff pattern. None when either input is missing -
67 never raises."""
68 if current_date is None or next_ex_dividend_date is None:
69 return None
70 return (next_ex_dividend_date - current_date).days
73def assignment_risk_score(
74 moneyness: float | None,
75 right: str,
76 extrinsic_value: float | None,
77 expected_dividend_amount: float | None,
78 days_to_next_ex_div: int | None,
79 window_days: int,
80) -> float:
81 """0.0-1.0 composite early-call-assignment-risk score.
83 Call-only by construction: a put always scores 0.0 (an upcoming
84 dividend never makes early exercise of a long put rational - dividends
85 only ever pull CALL exercise forward). Also 0.0 whenever: the leg
86 isn't in-the-money (moneyness <= 1.0), there's no known upcoming
87 ex-dividend date within window_days (including a negative
88 days_to_next_ex_div - already past due, stale schedule), window_days
89 itself is non-positive, or extrinsic_value/expected_dividend_amount is
90 unknown (None) or expected_dividend_amount is non-positive.
92 Within the risk window: scores at the time-proximity ceiling when
93 early_exercise_optimal_for_dividend() is already True (extrinsic value
94 below the expected dividend); otherwise decays smoothly via this
95 module's own _normal_cdf() as the extrinsic-value cushion over the
96 dividend widens (reusing this codebase's existing probabilistic
97 vocabulary rather than inventing an ad hoc formula), further scaled by
98 how close the ex-dividend date actually is within the window. Never
99 raises."""
100 if right.lower() != "call":
101 return 0.0
102 if moneyness is None or moneyness <= 1.0:
103 return 0.0
104 if window_days <= 0:
105 return 0.0
106 if days_to_next_ex_div is None or days_to_next_ex_div < 0 or days_to_next_ex_div > window_days:
107 return 0.0
108 if extrinsic_value is None or expected_dividend_amount is None or expected_dividend_amount <= 0.0:
109 return 0.0
111 time_factor = 1.0 - (days_to_next_ex_div / window_days)
113 if early_exercise_optimal_for_dividend(extrinsic_value, expected_dividend_amount):
114 return time_factor
116 cushion_ratio = (extrinsic_value - expected_dividend_amount) / expected_dividend_amount
117 cushion_factor = 2.0 * (1.0 - _normal_cdf(cushion_ratio))
118 return time_factor * cushion_factor
121def assignment_risk_flag(score: float, threshold: float) -> bool:
122 """Pure threshold predicate - the sweep-trigger boundary."""
123 return score >= threshold