Top-N Depth Imbalance
Install and import
npm install fintech-algorithmsimport { topNDepthImbalance } from "fintech-algorithms/market-microstructure/market-depth-analytics/top-n-depth-imbalance";Signature
topNDepthImbalance(bidsRaw, asksRaw, nRaw, thresholdRaw)Depth imbalance restricted to the top N levels — the same idea as queue imbalance, bounded to the part of the book that is realistically accessible.
Parameters
| Name | Type | Notes |
|---|---|---|
bidsRaw | Level[] | Bid levels, best first. |
asksRaw | Level[] | Ask levels, best first. |
nRaw | number | Levels to include. min: 1 · integer: true |
thresholdRaw | number | Magnitude above which the imbalance is flagged as significant. min: 0 |
Returns
{ imbalance, bid_depth, ask_depth, significant, … }
The imbalance with the depths behind it and whether it cleared the threshold.
Errors
- When nRaw exceeds the levels supplied — throws
Complexity: time O(n),
space O(1).
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
[
{
"price": 99.99,
"quantity": 500
},
{
"price": 99.98,
"quantity": 800
},
{
"price": 99.97,
"quantity": 1200
}
]Showing 3 of 5 elements.
[
{
"price": 100.01,
"quantity": 300
},
{
"price": 100.02,
"quantity": 600
},
{
"price": 100.03,
"quantity": 1000
}
]Showing 3 of 5 elements.
30.1Call
topNDepthImbalance(bidsRaw, asksRaw, nRaw, thresholdRaw)Returns
object with 9 fields: model, n, bid_depth, ask_depth, total_depth, imbalance, threshold, state, …
{
"model": "top-n-visible-depth-imbalance",
"n": 3,
"bid_depth": 2500,
"ask_depth": 1900,
"total_depth": 4400,
"imbalance": 0.13636363636363635,
"threshold": 0.1,
"state": "bid-heavy",
"complete_window": true
}Other exports
This module also exports
cumulativeDepth, depthAtDistanceProfile, expectedFillPrice, sweepCostAndSlippage, liquidityWallConcentration, depthDepletionReplenishment, marketDepthHeatmap, calculate. 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.
References
- NYSE Integrated Feed — New York Stock Exchange
- Nasdaq TotalView-ITCH 5.0 Specification — Nasdaq
- The Price Impact of Order Book Events — Rama Cont, Arseniy Kukanov, and Sasha Stoikov
- Multi-Level Order-Flow Imbalance in a Limit Order Book — Ke Xu, Martin D. Gould, and Sam D. Howison
- Evidence boundary