Skip to content

VOLATILITY RATIO

Intuição

O volatility ratio compara o True Range atual com um ATR (média do True Range). Valores maiores que 1 indicam expansão de volatilidade; valores menores sugerem contração.

Definição

  • TR_t = max(high_t-low_t, |high_t-close_{t-1}|, |low_t-close_{t-1}|)
  • ATR_t = mean(TR_{t-n+1:t})

A feature retorna VR_t = TR_t / ATR_t.

Uso

from quantmaster.features.volatility import volatility_ratio

df["vr"] = volatility_ratio(df, window=14)

API

Source code in src/quantmaster/features/volatility.py
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
def volatility_ratio(
    data: pd.DataFrame,
    *,
    window: int = 14,
    high_col: str = "high",
    low_col: str = "low",
    close_col: str = "close",
) -> pd.Series:
    window = validate_positive_int(window, name="window")
    validate_columns(data, required=(high_col, low_col, close_col))

    h = pd.to_numeric(data[high_col], errors="coerce").astype(float)
    l = pd.to_numeric(data[low_col], errors="coerce").astype(float)
    c = pd.to_numeric(data[close_col], errors="coerce").astype(float)

    prev_c = c.shift(1)
    tr_1 = h - l
    tr_2 = (h - prev_c).abs()
    tr_3 = (l - prev_c).abs()
    tr = pd.concat([tr_1, tr_2, tr_3], axis=1).max(axis=1)

    atr = tr.rolling(window).mean()
    out = tr / atr.where(atr > 0)
    out.name = f"volatility_ratio_{window}"
    return out