Order Flow Imbalance
Install and import
npm install fintech-algorithmsimport { orderFlowImbalance } from "fintech-algorithms/market-microstructure/order-flow-and-impact/order-flow-imbalance";Signature
orderFlowImbalance(inputRows, resetOnSession, normalizeByDepth)Net signed change in depth at the touch — the quantity that best explains short-horizon price moves in the microstructure literature. It counts additions and cancellations, not just trades.
Parameters
| Name | Type | Notes |
|---|---|---|
inputRows | Row[] | Sequential top-of-book observations with prices and sizes on both sides. |
resetOnSession | boolean | Whether the cumulative measure restarts each session. Carrying it overnight mixes regimes. |
normalizeByDepth | boolean | Divide by prevailing depth, which makes the measure comparable across instruments of different thickness. |
Returns
{ ofi, cumulative, rows, … }
Per-observation imbalance and its cumulative path.
Errors
- When rows are not in sequence order — throws
Complexity: time O(n),
space O(n).
Worked example
executed Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.
Input
[
{
"id": "E001",
"session": "S1",
"event_time_ns": 0,
"bid_price": 100,
"bid_size": 500,
"ask_price": 100.02,
"ask_size": 520
},
{
"id": "E002",
"session": "S1",
"event_time_ns": 1000000,
"bid_price": 100,
"bid_size": 573,
"ask_price": 100.02,
"ask_size": 567
},
{
"id": "E003",
"session": "S1",
"event_time_ns": 2000000,
"bid_price": 100,
"bid_size": 996,
"ask_price": 100.02,
"ask_size": 614
}
]Showing 3 of 60 elements.
truefalseCall
orderFlowImbalance(inputRows, resetOnSession, normalizeByDepth)Returns
object with 12 fields: model, state, reset_on_session, normalize_by_depth, event_count, classified_event_count, positive_event_count, negative_event_count, …
{
"model": "cont-best-quote-ofi",
"state": "estimated",
"reset_on_session": true,
"normalize_by_depth": false,
"event_count": 60,
"classified_event_count": 59,
"positive_event_count": 34,
"negative_event_count": 25,
"zero_event_count": 0,
"cumulative_ofi": 58,
"mean_absolute_event_ofi": 440.5762711864,
"trace": [
{
"id": "E001",
"index": 0,
"session": "S1",
"bid_price": 100,
"bid_size": 500,
"ask_price": 100.02,
"ask_size": 520,
"event_ofi": 0,
"cumulative_ofi": 0,
"side": "balanced",
"reason": "seed"
},
{
"id": "E002",
"index": 1,
"session": "S1",
"bid_price": 100,
"bid_size": 573,
"ask_price": 100.02,
"ask_size": 567,
"event_ofi": 26,
"cumulative_ofi": 26,
"side": "buy-pressure",
"reason": "best-quote-event"
},
{
"id": "E003",
"index": 2,
"session": "S1",
"bid_price": 100,
"bid_size": 996,
"ask_price": 100.02,
"ask_size": 614,
"event_ofi": 376,
"cumulative_ofi": 402,
"side": "buy-pressure",
"reason": "best-quote-event"
}
]
}Other exports
This module also exports
queueImbalance, kyleLambda, hasbrouckPriceImpact, pin, vpin, runTopic. Every module additionally exports run as an alias of its
primary function, and a meta object carrying its catalog id, domain, family,
shape and article URL.
Diagrams
How it works
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.