83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
from app.domain.portfolio_math import resolve_portfolio_spot_from_quote
|
|
from app.models.portfolio import PortfolioConfig
|
|
from app.pages.common import strategy_metrics
|
|
from app.pages.hedge import _waterfall_options
|
|
|
|
|
|
def test_protective_put_atm_minus_20pct_improves_equity() -> None:
|
|
metrics = strategy_metrics("protective_put_atm", -20)
|
|
|
|
assert metrics["scenario_price"] == 172.0
|
|
assert metrics["unhedged_equity"] == 27_000.0
|
|
assert metrics["hedged_equity"] == 58_750.0
|
|
assert metrics["hedged_equity"] > metrics["unhedged_equity"]
|
|
assert metrics["waterfall_steps"] == [
|
|
("Base equity", 70_000.0),
|
|
("Spot move", -43_000.0),
|
|
("Option payoff", 38_000.0),
|
|
("Call cap", 0.0),
|
|
("Hedge cost", -6_250.0),
|
|
("Net equity", 58_750.0),
|
|
]
|
|
|
|
|
|
def test_hedge_waterfall_uses_zero_based_contribution_bars() -> None:
|
|
options = _waterfall_options(strategy_metrics("protective_put_atm", -20))
|
|
|
|
assert len(options["series"]) == 1
|
|
assert options["series"][0]["type"] == "bar"
|
|
values = options["series"][0]["data"]
|
|
assert values[2]["value"] == 38_000.0
|
|
assert values[2]["itemStyle"]["color"] == "#22c55e"
|
|
|
|
|
|
def test_hedge_quote_resolution_converts_gld_share_price_to_ozt_spot() -> None:
|
|
"""Hedge page should convert GLD share quotes to USD/ozt for display."""
|
|
config = PortfolioConfig(entry_price=4400.0, gold_ounces=220.0, entry_basis_mode="weight", loan_amount=145000.0)
|
|
share_quote = {
|
|
"symbol": "GLD",
|
|
"price": 404.19,
|
|
"quote_unit": "share",
|
|
"source": "yfinance",
|
|
"updated_at": "2026-03-25T00:00:00+00:00",
|
|
}
|
|
|
|
spot, source, updated_at = resolve_portfolio_spot_from_quote(config, share_quote)
|
|
|
|
assert spot == 4041.9
|
|
assert source == "yfinance"
|
|
|
|
|
|
def test_hedge_quote_resolution_fails_closed_when_quote_unit_missing() -> None:
|
|
"""Hedge page should fall back to configured price when quote_unit is missing."""
|
|
config = PortfolioConfig(entry_price=4400.0, gold_ounces=220.0, entry_basis_mode="weight", loan_amount=145000.0)
|
|
legacy_quote = {
|
|
"symbol": "GLD",
|
|
"price": 404.19,
|
|
"source": "cache",
|
|
}
|
|
|
|
spot, source, _ = resolve_portfolio_spot_from_quote(config, legacy_quote)
|
|
|
|
assert spot == 4400.0
|
|
assert source == "configured_entry_price"
|
|
|
|
|
|
def test_hedge_quote_resolution_fails_closed_for_unsupported_instrument() -> None:
|
|
"""Hedge page should fall back when instrument metadata is unavailable."""
|
|
config = PortfolioConfig(entry_price=4400.0, gold_ounces=220.0, entry_basis_mode="weight", loan_amount=145000.0)
|
|
slv_quote = {
|
|
"symbol": "SLV",
|
|
"price": 28.50,
|
|
"quote_unit": "share",
|
|
"source": "yfinance",
|
|
"updated_at": "2026-03-25T00:00:00+00:00",
|
|
}
|
|
|
|
spot, source, _ = resolve_portfolio_spot_from_quote(config, slv_quote)
|
|
|
|
assert spot == 4400.0
|
|
assert source == "configured_entry_price"
|