Skip to content

Order Flow Imbalance

Intuição

O Order Flow Imbalance aproxima desequilíbrio de fluxo usando o volume e o sinal do retorno, produzindo um indicador normalizado em [-1, 1].

Definição

Em uma janela n:

  • s_t = sign(r_t)
  • OFI = sum(volume * s) / sum(volume)

Uso

from quantmaster.features.volume import order_flow_imbalance

df["ofi_20"] = order_flow_imbalance(df, window=20)

API

Source code in src/quantmaster/features/volume.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def order_flow_imbalance(
    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()

    s = np.sign(rets)
    s = s.where(s != 0.0, 0.0)

    buy_sell = (volume * s).rolling(window).sum()
    total = volume.rolling(window).sum()
    out = (buy_sell / total.where(total > 0)).clip(lower=-1.0, upper=1.0)
    out.name = f"order_flow_imbalance_{window}"
    return out