# src/risk/sizing/volatility.py

from .base import PositionSizer


class VolatilitySizer(PositionSizer):
    """
    Ajusta el tamaño según la volatilidad (ATR o std).
    """

    def __init__(self, volatility_multiplier: float = 2.0):
        if volatility_multiplier <= 0:
            raise ValueError("volatility_multiplier debe ser > 0")

        self.volatility_multiplier = volatility_multiplier

    def calculate_size(
        self,
        capital: float,
        entry_price: float,
        stop_price=None,
        volatility: float = None,
    ) -> float:

        if volatility is None:
            raise ValueError("VolatilitySizer requiere volatilidad (ATR o std)")

        risk_per_unit = volatility * entry_price * self.volatility_multiplier

        if risk_per_unit <= 0:
            return 0.0

        units = capital / risk_per_unit
        return units
