fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Multi-Level Sweep Cost and Slippage

Install and import#

bash
npm install fintech-algorithms
ts
import { sweepCostAndSlippage } from "fintech-algorithms/market-microstructure/market-depth-analytics/multi-level-sweep-cost-and-slippage";

Signature#

sweepCostAndSlippage(bids, asks, side, quantity, benchmarkRaw, benchmarkPriceRaw, limitPrice)

The cost of sweeping multiple levels, measured against a chosen benchmark. Slippage is only meaningful relative to a benchmark, so which one is a parameter rather than an assumption.

Parameters#

NameTypeNotes
bidsLevel[]Bid levels.
asksLevel[]Ask levels.
side"buy" | "sell"Order direction.
quantitynumberOrder quantity.
min: 0
benchmarkRawstringWhich benchmark to measure against — touch, midpoint or a supplied price.
benchmarkPriceRawnumberThe benchmark price when one is supplied explicitly.
optional
limitPricenumberOptional limit price.
optional

Returns#

{ sweep_cost, slippage_bps, average_price, benchmark_price, … }

Cost and slippage in basis points with the benchmark actually used.

Errors#

  • When the benchmark is unrecognised, or requires a price that was not supplied — 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#

bids
[
  {
    "price": 99.99,
    "quantity": 500
  },
  {
    "price": 99.98,
    "quantity": 800
  },
  {
    "price": 99.97,
    "quantity": 1200
  }
]

Showing 3 of 5 elements.

asks
[
  {
    "price": 100.01,
    "quantity": 300
  },
  {
    "price": 100.02,
    "quantity": 600
  },
  {
    "price": 100.03,
    "quantity": 1000
  }
]

Showing 3 of 5 elements.

side
"buy"
quantity
2600
benchmarkRaw
"midpoint"
benchmarkPriceRaw
null
limitPrice
null

Call#

sweepCostAndSlippage(bids, asks, side, quantity, benchmarkRaw, benchmarkPriceRaw, limitPrice)

Returns#

object with 20 fields: model, side, requested_quantity, filled_quantity, unfilled_quantity, full_fill, fills, fill_notional, …

{
  "model": "visible-book-sweep-cost",
  "side": "buy",
  "requested_quantity": 2600,
  "filled_quantity": 2600,
  "unfilled_quantity": 0,
  "full_fill": true,
  "fills": [
    {
      "level_index": 0,
      "price": 100.01,
      "quantity": 300,
      "notional": 30003
    },
    {
      "level_index": 1,
      "price": 100.02,
      "quantity": 600,
      "notional": 60012
    },
    {
      "level_index": 2,
      "price": 100.03,
      "quantity": 1000,
      "notional": 100030
    }
  ],
  "fill_notional": 260073,
  "partial_vwap": 100.02807692307692,
  "worst_fill_price": 100.04,
  "best_bid": 99.99,
  "best_ask": 100.01,
  "midpoint": 100,
  "benchmark": "midpoint"
}

Showing 14 of 20 fields.

Other exports#

This module also exports cumulativeDepth, topNDepthImbalance, depthAtDistanceProfile, expectedFillPrice, 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#

Multi-Level Sweep Cost and Slippage — system map

Calculation flow#

Multi-Level Sweep Cost and Slippage calculation flow
flowchart LR
    S1["Calculate eligible perlevel fills"]
    S2["Select and validate the benchmark"]
    S3["Compute fill VWAP"]
    S4["Apply buysell direction to price difference"]
    S5["Scale to basis points and filled monetary cost"]
    S1 --> S2
    S2 --> S3
    S3 --> S4
    S4 --> S5
    S5 --> D{"full versus partial sweep and benchmark choice"}
    D --> O["signed_slippage_bps + diagnostics"]
    O --> A["Audit: Benchmark choice is returned with its numeric value"]

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#

The rest of the Market-Depth Analytics family#