feat(PORTFOLIO-002): add position storage costs
This commit is contained in:
@@ -22,6 +22,9 @@ class Position:
|
||||
entry_date: Date of position entry (for historical conversion lookups)
|
||||
entry_basis_mode: Entry basis mode ("weight" or "value_price")
|
||||
notes: Optional notes about this position
|
||||
storage_cost_basis: Annual storage cost as percentage (e.g., Decimal("0.12") for 0.12%) or fixed amount
|
||||
storage_cost_period: Period for storage cost ("annual" or "monthly")
|
||||
storage_cost_currency: Currency for fixed amount costs (default "USD")
|
||||
created_at: Timestamp when position was created
|
||||
"""
|
||||
|
||||
@@ -33,6 +36,9 @@ class Position:
|
||||
entry_date: date
|
||||
entry_basis_mode: str = "weight"
|
||||
notes: str = ""
|
||||
storage_cost_basis: Decimal | None = None
|
||||
storage_cost_period: str | None = None
|
||||
storage_cost_currency: str = "USD"
|
||||
created_at: datetime = field(default_factory=lambda: datetime.now(UTC))
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
@@ -67,6 +73,9 @@ class Position:
|
||||
"entry_date": self.entry_date.isoformat(),
|
||||
"entry_basis_mode": self.entry_basis_mode,
|
||||
"notes": self.notes,
|
||||
"storage_cost_basis": str(self.storage_cost_basis) if self.storage_cost_basis is not None else None,
|
||||
"storage_cost_period": self.storage_cost_period,
|
||||
"storage_cost_currency": self.storage_cost_currency,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
}
|
||||
|
||||
@@ -82,6 +91,11 @@ class Position:
|
||||
entry_date=date.fromisoformat(data["entry_date"]),
|
||||
entry_basis_mode=data.get("entry_basis_mode", "weight"),
|
||||
notes=data.get("notes", ""),
|
||||
storage_cost_basis=(
|
||||
Decimal(data["storage_cost_basis"]) if data.get("storage_cost_basis") is not None else None
|
||||
),
|
||||
storage_cost_period=data.get("storage_cost_period"),
|
||||
storage_cost_currency=data.get("storage_cost_currency", "USD"),
|
||||
created_at=datetime.fromisoformat(data["created_at"]) if "created_at" in data else datetime.now(UTC),
|
||||
)
|
||||
|
||||
@@ -94,6 +108,9 @@ def create_position(
|
||||
entry_date: date | None = None,
|
||||
entry_basis_mode: str = "weight",
|
||||
notes: str = "",
|
||||
storage_cost_basis: Decimal | None = None,
|
||||
storage_cost_period: str | None = None,
|
||||
storage_cost_currency: str = "USD",
|
||||
) -> Position:
|
||||
"""Create a new position with sensible defaults.
|
||||
|
||||
@@ -105,6 +122,9 @@ def create_position(
|
||||
entry_date: Entry date (default: today)
|
||||
entry_basis_mode: Entry basis mode (default: "weight")
|
||||
notes: Optional notes
|
||||
storage_cost_basis: Annual storage cost as percentage or fixed amount (default: None)
|
||||
storage_cost_period: Period for storage cost ("annual" or "monthly", default: None)
|
||||
storage_cost_currency: Currency for fixed amount costs (default: "USD")
|
||||
"""
|
||||
return Position(
|
||||
id=uuid4(),
|
||||
@@ -115,4 +135,7 @@ def create_position(
|
||||
entry_date=entry_date or date.today(),
|
||||
entry_basis_mode=entry_basis_mode,
|
||||
notes=notes,
|
||||
storage_cost_basis=storage_cost_basis,
|
||||
storage_cost_period=storage_cost_period,
|
||||
storage_cost_currency=storage_cost_currency,
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from decimal import Decimal
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.responses import RedirectResponse
|
||||
@@ -21,6 +22,7 @@ from app.pages.common import (
|
||||
from app.services.alerts import AlertService, build_portfolio_alert_context
|
||||
from app.services.ltv_history import LtvHistoryChartModel, LtvHistoryService
|
||||
from app.services.runtime import get_data_service
|
||||
from app.services.storage_costs import calculate_total_storage_cost
|
||||
from app.services.turnstile import load_turnstile_settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -163,6 +165,17 @@ async def overview_page(workspace_id: str) -> None:
|
||||
configured_gold_value = float(config.gold_value or 0.0)
|
||||
portfolio["cash_buffer"] = max(float(portfolio["gold_value"]) - configured_gold_value, 0.0) + _DEFAULT_CASH_BUFFER
|
||||
portfolio["hedge_budget"] = float(config.monthly_budget)
|
||||
|
||||
# Calculate storage costs for positions
|
||||
positions = config.positions
|
||||
current_values: dict[str, Decimal] = {}
|
||||
for pos in positions:
|
||||
# Use entry value as proxy for current value (would need live prices for accurate calc)
|
||||
current_values[str(pos.id)] = pos.entry_value
|
||||
total_annual_storage_cost = calculate_total_storage_cost(positions, current_values)
|
||||
portfolio["annual_storage_cost"] = float(total_annual_storage_cost)
|
||||
portfolio["storage_cost_pct"] = (float(total_annual_storage_cost) / float(portfolio["gold_value"]) * 100) if portfolio["gold_value"] > 0 else 0.0
|
||||
|
||||
alert_status = AlertService().evaluate(config, portfolio)
|
||||
ltv_history_service = LtvHistoryService(repository=LtvHistoryRepository(base_path=repo.base_path))
|
||||
ltv_history_notice: str | None = None
|
||||
@@ -362,6 +375,11 @@ async def overview_page(workspace_id: str) -> None:
|
||||
f"${portfolio['hedge_budget']:,.0f}",
|
||||
"Monthly budget from saved settings",
|
||||
),
|
||||
(
|
||||
"Storage Costs",
|
||||
f"${portfolio['annual_storage_cost']:,.2f}/yr ({portfolio['storage_cost_pct']:.2f}%)",
|
||||
"Annual vault storage for physical positions (GLD expense ratio baked into share price)",
|
||||
),
|
||||
]
|
||||
for title, value, caption in summary_cards:
|
||||
with ui.card().classes(
|
||||
|
||||
@@ -15,6 +15,7 @@ from app.models.workspace import get_workspace_repository
|
||||
from app.pages.common import dashboard_page, split_page_panes
|
||||
from app.services.alerts import AlertService, build_portfolio_alert_context
|
||||
from app.services.settings_status import save_status_text
|
||||
from app.services.storage_costs import get_default_storage_cost_for_underlying
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -334,6 +335,19 @@ def settings_page(workspace_id: str) -> None:
|
||||
label="Underlying",
|
||||
).classes("w-full")
|
||||
|
||||
def update_storage_cost_default() -> None:
|
||||
"""Update storage cost defaults based on underlying selection."""
|
||||
underlying = str(pos_underlying.value)
|
||||
default_basis, default_period = get_default_storage_cost_for_underlying(underlying)
|
||||
if default_basis is not None:
|
||||
pos_storage_cost_basis.value = float(default_basis)
|
||||
pos_storage_cost_period.value = default_period or "annual"
|
||||
else:
|
||||
pos_storage_cost_basis.value = 0.0
|
||||
pos_storage_cost_period.value = "annual"
|
||||
|
||||
pos_underlying.on_value_change(lambda _: update_storage_cost_default())
|
||||
|
||||
pos_quantity = ui.number(
|
||||
"Quantity",
|
||||
value=100.0,
|
||||
@@ -369,6 +383,23 @@ def settings_page(workspace_id: str) -> None:
|
||||
placeholder="Add notes about this position...",
|
||||
).classes("w-full")
|
||||
|
||||
ui.separator().classes("my-3")
|
||||
ui.label("Storage Costs (optional)").classes("text-sm font-semibold text-slate-700 dark:text-slate-300")
|
||||
ui.label("For physical gold (XAU), defaults to 0.12% annual vault storage.").classes("text-xs text-slate-500 dark:text-slate-400 mb-2")
|
||||
|
||||
pos_storage_cost_basis = ui.number(
|
||||
"Storage cost (% per year or fixed $)",
|
||||
value=0.0,
|
||||
min=0.0,
|
||||
step=0.01,
|
||||
).classes("w-full")
|
||||
|
||||
pos_storage_cost_period = ui.select(
|
||||
{"annual": "Annual", "monthly": "Monthly"},
|
||||
value="annual",
|
||||
label="Cost period",
|
||||
).classes("w-full")
|
||||
|
||||
with ui.row().classes("w-full gap-3 mt-4"):
|
||||
ui.button("Cancel", on_click=lambda: add_position_dialog.close()).props("outline")
|
||||
ui.button("Add Position", on_click=lambda: add_position_from_form()).props("color=primary")
|
||||
@@ -376,15 +407,22 @@ def settings_page(workspace_id: str) -> None:
|
||||
def add_position_from_form() -> None:
|
||||
"""Add a new position from the form."""
|
||||
try:
|
||||
underlying = str(pos_underlying.value)
|
||||
storage_cost_basis_val = float(pos_storage_cost_basis.value)
|
||||
storage_cost_basis = Decimal(str(storage_cost_basis_val)) if storage_cost_basis_val > 0 else None
|
||||
storage_cost_period = str(pos_storage_cost_period.value) if storage_cost_basis else None
|
||||
|
||||
new_position = Position(
|
||||
id=uuid4(),
|
||||
underlying=str(pos_underlying.value),
|
||||
underlying=underlying,
|
||||
quantity=Decimal(str(pos_quantity.value)),
|
||||
unit=str(pos_unit.value),
|
||||
entry_price=Decimal(str(pos_entry_price.value)),
|
||||
entry_date=date.fromisoformat(str(pos_entry_date.value)),
|
||||
entry_basis_mode="weight",
|
||||
notes=str(pos_notes.value or ""),
|
||||
storage_cost_basis=storage_cost_basis,
|
||||
storage_cost_period=storage_cost_period,
|
||||
)
|
||||
workspace_repo.add_position(workspace_id, new_position)
|
||||
add_position_dialog.close()
|
||||
@@ -423,6 +461,19 @@ def settings_page(workspace_id: str) -> None:
|
||||
ui.label(f"Value: ${float(pos.entry_value):,.2f}").classes(
|
||||
"text-xs font-semibold text-emerald-600 dark:text-emerald-400"
|
||||
)
|
||||
# Show storage cost if configured
|
||||
if pos.storage_cost_basis is not None:
|
||||
basis_val = float(pos.storage_cost_basis)
|
||||
period = pos.storage_cost_period or "annual"
|
||||
if basis_val < 1:
|
||||
# Percentage
|
||||
storage_label = f"{basis_val:.2f}% {period} storage"
|
||||
else:
|
||||
# Fixed amount
|
||||
storage_label = f"${basis_val:,.2f} {period} storage"
|
||||
ui.label(f"Storage: {storage_label}").classes(
|
||||
"text-xs text-slate-500 dark:text-slate-400"
|
||||
)
|
||||
with ui.row().classes("gap-1"):
|
||||
ui.button(
|
||||
icon="delete",
|
||||
|
||||
105
app/services/storage_costs.py
Normal file
105
app/services/storage_costs.py
Normal file
@@ -0,0 +1,105 @@
|
||||
"""Storage cost calculation service for positions with physical storage requirements."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from app.models.position import Position
|
||||
|
||||
_DECIMAL_ZERO = Decimal("0")
|
||||
_DECIMAL_ONE = Decimal("1")
|
||||
_DECIMAL_HUNDRED = Decimal("100")
|
||||
_DECIMAL_TWELVE = Decimal("12")
|
||||
|
||||
|
||||
def calculate_annual_storage_cost(position: Position, current_value: Decimal) -> Decimal:
|
||||
"""Calculate annual storage cost for a single position.
|
||||
|
||||
Args:
|
||||
position: Position with optional storage_cost_basis and storage_cost_period
|
||||
current_value: Current market value of the position (quantity × current_price)
|
||||
|
||||
Returns:
|
||||
Annual storage cost in position's storage_cost_currency (default USD)
|
||||
|
||||
Notes:
|
||||
- If storage_cost_basis is None, returns 0 (no storage cost)
|
||||
- If storage_cost_period is "monthly", annualizes the cost (×12)
|
||||
- If storage_cost_basis is a percentage, applies it to current_value
|
||||
- If storage_cost_basis is a fixed amount, uses it directly
|
||||
"""
|
||||
if position.storage_cost_basis is None:
|
||||
return _DECIMAL_ZERO
|
||||
|
||||
basis = position.storage_cost_basis
|
||||
period = position.storage_cost_period or "annual"
|
||||
|
||||
# Determine if basis is a percentage (e.g., 0.12 for 0.12%) or fixed amount
|
||||
# Heuristic: if basis < 1, treat as percentage; otherwise as fixed amount
|
||||
if basis < _DECIMAL_ONE:
|
||||
# Percentage-based cost
|
||||
if period == "monthly":
|
||||
# Monthly percentage, annualize it
|
||||
annual_rate = basis * _DECIMAL_TWELVE
|
||||
else:
|
||||
# Already annual
|
||||
annual_rate = basis
|
||||
|
||||
# Apply percentage to current value
|
||||
return (current_value * annual_rate) / _DECIMAL_HUNDRED
|
||||
else:
|
||||
# Fixed amount
|
||||
if period == "monthly":
|
||||
# Monthly fixed cost, annualize it
|
||||
return basis * _DECIMAL_TWELVE
|
||||
else:
|
||||
# Already annual fixed cost
|
||||
return basis
|
||||
|
||||
|
||||
def calculate_total_storage_cost(
|
||||
positions: list[Position],
|
||||
current_values: dict[str, Decimal],
|
||||
) -> Decimal:
|
||||
"""Calculate total annual storage cost across all positions.
|
||||
|
||||
Args:
|
||||
positions: List of positions with optional storage costs
|
||||
current_values: Mapping of position ID (str) to current market value
|
||||
|
||||
Returns:
|
||||
Total annual storage cost in USD (assumes all positions use USD)
|
||||
"""
|
||||
total = _DECIMAL_ZERO
|
||||
for position in positions:
|
||||
current_value = current_values.get(str(position.id), _DECIMAL_ZERO)
|
||||
cost = calculate_annual_storage_cost(position, current_value)
|
||||
total += cost
|
||||
return total
|
||||
|
||||
|
||||
def get_default_storage_cost_for_underlying(underlying: str) -> tuple[Decimal | None, str | None]:
|
||||
"""Get default storage cost settings for a given underlying instrument.
|
||||
|
||||
Args:
|
||||
underlying: Instrument symbol (e.g., "XAU", "GLD", "GC=F")
|
||||
|
||||
Returns:
|
||||
Tuple of (storage_cost_basis, storage_cost_period) or (None, None) if no default
|
||||
|
||||
Notes:
|
||||
- XAU (physical gold): 0.12% annual for allocated vault storage
|
||||
- GLD: None (expense ratio baked into share price)
|
||||
- GC=F: None (roll costs are the storage analog, handled separately)
|
||||
"""
|
||||
if underlying == "XAU":
|
||||
# Physical gold: 0.12% annual storage cost for allocated vault storage
|
||||
return Decimal("0.12"), "annual"
|
||||
elif underlying == "GLD":
|
||||
# GLD: expense ratio is implicit in share price, no separate storage cost
|
||||
return None, None
|
||||
elif underlying == "GC=F":
|
||||
# Futures: roll costs are the storage analog (deferred to GCF-001)
|
||||
return None, None
|
||||
else:
|
||||
return None, None
|
||||
Reference in New Issue
Block a user