fintech-algorithms

Depth-Weighted Midprice

Install and import

bash
npm install fintech-algorithms
ts
import { depthWeightedMidprice } from "fintech-algorithms/market-microstructure/order-book-dynamics/depth-weighted-midprice";

Signature

depthWeightedMidprice(bidPrices, bidSizes, askPrices, askSizes)

A midpoint weighted by depth across several levels rather than the naive average of best bid and ask. Less prone to jumping when a single small order sits at the touch.

Parameters

NameTypeNotes
bidPricesnumber[]Bid prices, best first.
bidSizesnumber[]Sizes aligned with bidPrices.
askPricesnumber[]Ask prices, best first.
askSizesnumber[]Sizes aligned with askPrices.

Returns

{ midprice, simple_midpoint, weighted_bid, weighted_ask, … }

The depth-weighted midpoint alongside the naive one, so the difference is visible.

Errors

  • When total depth on either side is zero — throws

Complexity: time O(levels), 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

bidPrices
[99.99, 99.98, 99.97]
bidSizes
[500, 1000, 1500]
askPrices
[100.01, 100.02, 100.03]
askSizes
[300, 700, 2000]

Call

depthWeightedMidprice(bidPrices, bidSizes, askPrices, askSizes)

Returns

object with 13 fields: model, level_count, bid_level_count, ask_level_count, top_midpoint, depth_weighted_bid, depth_weighted_ask, depth_weighted_midprice, …

{
  "model": "same-side-depth-weighted-midpoint",
  "level_count": 3,
  "bid_level_count": 3,
  "ask_level_count": 3,
  "top_midpoint": 100,
  "depth_weighted_bid": 99.97666666666667,
  "depth_weighted_ask": 100.02566666666667,
  "depth_weighted_midprice": 100.00116666666668,
  "shift": 0.0011666666666769743,
  "shift_bps": 0.11666666666769743,
  "bid_total_depth": 3000,
  "ask_total_depth": 3000,
  "state": "above-top-mid"
}

Other exports

This module also exports orderBookSlope, microprice, orderBookResiliency, hawkesOrderArrival, 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

Depth-Weighted Midprice — system map

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.

Read the article →

References