fix(types): core calculations mypy errors - isinstance checks, OptionType cast
This commit is contained in:
@@ -2,11 +2,13 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Iterable, Mapping
|
from collections.abc import Iterable, Mapping
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
from app.core.pricing.black_scholes import (
|
from app.core.pricing.black_scholes import (
|
||||||
DEFAULT_RISK_FREE_RATE,
|
DEFAULT_RISK_FREE_RATE,
|
||||||
DEFAULT_VOLATILITY,
|
DEFAULT_VOLATILITY,
|
||||||
BlackScholesInputs,
|
BlackScholesInputs,
|
||||||
|
OptionType,
|
||||||
black_scholes_price_and_greeks,
|
black_scholes_price_and_greeks,
|
||||||
)
|
)
|
||||||
from app.models.option import OptionContract
|
from app.models.option import OptionContract
|
||||||
@@ -124,10 +126,8 @@ def option_row_greeks(
|
|||||||
if underlying_price <= 0:
|
if underlying_price <= 0:
|
||||||
return dict(_ZERO_GREEKS)
|
return dict(_ZERO_GREEKS)
|
||||||
|
|
||||||
try:
|
strike_raw = row.get("strike", 0.0)
|
||||||
strike = float(row.get("strike", 0.0))
|
strike = float(strike_raw) if isinstance(strike_raw, (int, float)) else 0.0
|
||||||
except (TypeError, ValueError):
|
|
||||||
return dict(_ZERO_GREEKS)
|
|
||||||
if strike <= 0:
|
if strike <= 0:
|
||||||
return dict(_ZERO_GREEKS)
|
return dict(_ZERO_GREEKS)
|
||||||
|
|
||||||
@@ -149,12 +149,11 @@ def option_row_greeks(
|
|||||||
if days_to_expiry <= 0:
|
if days_to_expiry <= 0:
|
||||||
return dict(_ZERO_GREEKS)
|
return dict(_ZERO_GREEKS)
|
||||||
|
|
||||||
try:
|
iv_raw = row.get("impliedVolatility", 0.0) or 0.0
|
||||||
implied_volatility = float(row.get("impliedVolatility", 0.0) or 0.0)
|
implied_volatility = float(iv_raw) if isinstance(iv_raw, (int, float)) else 0.0
|
||||||
except (TypeError, ValueError):
|
|
||||||
implied_volatility = 0.0
|
|
||||||
volatility = implied_volatility if implied_volatility > 0 else DEFAULT_VOLATILITY
|
volatility = implied_volatility if implied_volatility > 0 else DEFAULT_VOLATILITY
|
||||||
|
|
||||||
|
option_type_typed: OptionType = cast(OptionType, option_type)
|
||||||
try:
|
try:
|
||||||
pricing = black_scholes_price_and_greeks(
|
pricing = black_scholes_price_and_greeks(
|
||||||
BlackScholesInputs(
|
BlackScholesInputs(
|
||||||
@@ -163,7 +162,7 @@ def option_row_greeks(
|
|||||||
time_to_expiry=days_to_expiry / 365.0,
|
time_to_expiry=days_to_expiry / 365.0,
|
||||||
risk_free_rate=risk_free_rate,
|
risk_free_rate=risk_free_rate,
|
||||||
volatility=volatility,
|
volatility=volatility,
|
||||||
option_type=option_type,
|
option_type=option_type_typed,
|
||||||
valuation_date=valuation,
|
valuation_date=valuation,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user