"""ATR (rolling mean of True Range) — port fedele da V15."""

from __future__ import annotations

import pandas as pd


def calc_atr(df: pd.DataFrame, period: int = 14) -> pd.Series:
    """
    ATR series. Caller typically reads iloc[-1] for current ATR and
    `series.rolling(20).mean().iloc[-1]` for the average — the ratio
    of the two becomes `atr_ratio` consumed by both Brains.
    """
    hl = df["high"] - df["low"]
    hc = (df["high"] - df["close"].shift()).abs()
    lc = (df["low"] - df["close"].shift()).abs()
    return pd.concat([hl, hc, lc], axis=1).max(axis=1).rolling(period).mean()
