Skip to content

Price Volume Correlation

Intuição

A Price Volume Correlation mede a correlação entre retornos do preço e o nível (log) de volume em uma janela móvel, capturando se movimentos de preço tendem a ocorrer junto com maior (ou menor) atividade.

Definição

Em uma janela n:

  • r_t = retorno do preço (log ou pct)
  • v_t = log(volume_t)
  • PVC = corr(r, v)

Uso

from quantmaster.features.volume import price_volume_correlation

df["pvc_20"] = price_volume_correlation(df, window=20)

API

Source code in src/quantmaster/features/volume.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def price_volume_correlation(
    data: pd.DataFrame,
    *,
    window: int = 20,
    price_col: str = "close",
    volume_col: str = "volume",
    log_returns: bool = True,
) -> pd.Series:
    window = validate_positive_int(window, name="window")
    validate_columns(data, required=[price_col, volume_col])

    price = get_price_series(data, price_col=price_col).astype(float)
    volume = pd.to_numeric(data[volume_col], errors="coerce").astype(float)

    price = price.where(price > 0)
    volume = volume.where(volume > 0)

    if log_returns:
        rets = np.log(price).diff()
    else:
        rets = price.pct_change()

    vol_level = np.log(volume)
    out = rets.rolling(window).corr(vol_level)
    out.name = f"price_volume_correlation_{window}"
    return out