diff --git a/app/core/calculations.py b/app/core/calculations.py index bc6b772..da1286b 100644 --- a/app/core/calculations.py +++ b/app/core/calculations.py @@ -2,11 +2,13 @@ from __future__ import annotations from collections.abc import Iterable, Mapping from datetime import date, datetime +from typing import cast from app.core.pricing.black_scholes import ( DEFAULT_RISK_FREE_RATE, DEFAULT_VOLATILITY, BlackScholesInputs, + OptionType, black_scholes_price_and_greeks, ) from app.models.option import OptionContract @@ -124,10 +126,8 @@ def option_row_greeks( if underlying_price <= 0: return dict(_ZERO_GREEKS) - try: - strike = float(row.get("strike", 0.0)) - except (TypeError, ValueError): - return dict(_ZERO_GREEKS) + strike_raw = row.get("strike", 0.0) + strike = float(strike_raw) if isinstance(strike_raw, (int, float)) else 0.0 if strike <= 0: return dict(_ZERO_GREEKS) @@ -149,12 +149,11 @@ def option_row_greeks( if days_to_expiry <= 0: return dict(_ZERO_GREEKS) - try: - implied_volatility = float(row.get("impliedVolatility", 0.0) or 0.0) - except (TypeError, ValueError): - implied_volatility = 0.0 + iv_raw = row.get("impliedVolatility", 0.0) or 0.0 + implied_volatility = float(iv_raw) if isinstance(iv_raw, (int, float)) else 0.0 volatility = implied_volatility if implied_volatility > 0 else DEFAULT_VOLATILITY + option_type_typed: OptionType = cast(OptionType, option_type) try: pricing = black_scholes_price_and_greeks( BlackScholesInputs( @@ -163,7 +162,7 @@ def option_row_greeks( time_to_expiry=days_to_expiry / 365.0, risk_free_rate=risk_free_rate, volatility=volatility, - option_type=option_type, + option_type=option_type_typed, valuation_date=valuation, ) )