fix(CORE-002C): align historical units with workspace weight
This commit is contained in:
@@ -3,8 +3,9 @@ from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from decimal import Decimal
|
||||
|
||||
from app.domain.units import BaseCurrency, Money, _coerce_currency, decimal_from_float, to_decimal
|
||||
from app.domain.units import BaseCurrency, Money, Weight, WeightUnit, _coerce_currency, decimal_from_float, to_decimal
|
||||
from app.models.backtest import BacktestPortfolioState
|
||||
from app.models.portfolio import PortfolioConfig
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
@@ -94,6 +95,23 @@ def asset_quantity_from_floats(portfolio_value: float, entry_spot: float, symbol
|
||||
return float(asset_quantity_from_money(notional, spot).amount)
|
||||
|
||||
|
||||
def asset_quantity_from_workspace_config(config: PortfolioConfig, *, entry_spot: float, symbol: str) -> float:
|
||||
if config.gold_ounces is not None and config.gold_ounces > 0:
|
||||
try:
|
||||
from app.domain.instruments import asset_quantity_from_weight
|
||||
|
||||
quantity = asset_quantity_from_weight(
|
||||
symbol,
|
||||
Weight(amount=decimal_from_float(float(config.gold_ounces)), unit=WeightUnit.OUNCE_TROY),
|
||||
)
|
||||
return float(quantity.amount)
|
||||
except ValueError:
|
||||
pass
|
||||
if config.gold_value is not None:
|
||||
return asset_quantity_from_floats(float(config.gold_value), entry_spot, symbol)
|
||||
raise ValueError("Workspace config must provide collateral weight or value")
|
||||
|
||||
|
||||
def materialize_backtest_portfolio_state(
|
||||
*,
|
||||
symbol: str,
|
||||
|
||||
Reference in New Issue
Block a user