feat(CORE-002): add GLD share quote conversion seam

This commit is contained in:
Bu5hm4nn
2026-03-25 14:52:48 +01:00
parent 1a2dfaff01
commit f0d7ab5748
10 changed files with 425 additions and 34 deletions

View File

@@ -1,12 +1,16 @@
from __future__ import annotations
from decimal import Decimal
from app.domain.backtesting_math import PricePerAsset
from app.domain.instruments import price_per_weight_from_asset_price
from app.domain.portfolio_math import (
build_alert_context,
portfolio_snapshot_from_config,
strategy_metrics_from_snapshot,
)
from app.domain.units import BaseCurrency, WeightUnit
from app.models.portfolio import PortfolioConfig
from app.pages.common import strategy_catalog
def test_portfolio_snapshot_from_config_preserves_weight_price_and_margin_values() -> None:
@@ -37,7 +41,27 @@ def test_build_alert_context_uses_unit_safe_gold_value_calculation() -> None:
assert round(float(context["margin_call_price"]), 2) == 878.79
def test_build_alert_context_accepts_explicit_gld_share_quote_conversion() -> None:
config = PortfolioConfig(entry_price=4400.0, gold_ounces=220.0, entry_basis_mode="weight", loan_amount=145000.0)
share_quote = PricePerAsset(amount=Decimal("404.19"), currency=BaseCurrency.USD, symbol="GLD")
ounce_spot = price_per_weight_from_asset_price(share_quote, per_unit=WeightUnit.OUNCE_TROY)
context = build_alert_context(
config,
spot_price=float(ounce_spot.amount),
source="yfinance",
updated_at="2026-03-24T00:00:00+00:00",
)
assert context["spot_price"] == 4041.9
assert context["gold_value"] == 889218.0
assert context["net_equity"] == 744218.0
assert context["quote_source"] == "yfinance"
def test_strategy_metrics_from_snapshot_preserves_minus_20pct_protective_put_example() -> None:
from app.pages.common import strategy_catalog
strategy = next(item for item in strategy_catalog() if item["name"] == "protective_put_atm")
snapshot = {
"gold_value": 215000.0,