Skip to content

vpin_proxy

from quantmaster.features.microstructure import vpin_proxy

df["vpin_20"] = vpin_proxy(df, window=20)

Proxy for VPIN (Volume-Synchronized Probability of Informed Trading).

Uses candle direction to classify volume as buy/sell. VPIN ~ |V_buy - V_sell| / (V_buy + V_sell) (rolling sum)

Source code in src/quantmaster/features/microstructure.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
def vpin_proxy(
    data: pd.DataFrame,
    *,
    window: int = 20,
    open_col: str = "open",
    close_col: str = "close",
    volume_col: str = "volume",
) -> pd.Series:
    """
    Proxy for VPIN (Volume-Synchronized Probability of Informed Trading).

    Uses candle direction to classify volume as buy/sell.
    VPIN ~ |V_buy - V_sell| / (V_buy + V_sell) (rolling sum)
    """
    window = validate_positive_int(window, name="window")
    validate_columns(data, required=(open_col, close_col, volume_col))

    o = pd.to_numeric(data[open_col], errors="coerce").astype(float)
    c = pd.to_numeric(data[close_col], errors="coerce").astype(float)
    v = pd.to_numeric(data[volume_col], errors="coerce").astype(float)

    signed_volume = v * np.sign(c - o)

    # Absolute imbalance sum
    num = signed_volume.rolling(window).sum().abs()
    # Total volume sum
    den = v.rolling(window).sum()

    out = num / den.where(den > 0)
    out.name = f"vpin_proxy_{window}"
    return out