QuantLib_Pro / quantlib_pro /api /routers_liquidity.py
gdukens's picture
Initial HF Space deploy bundle
72fc0ee
Raw History Blame Contribute Delete
16 kB
"""
Liquidity & Market Microstructure API Router
Covers page 12: Liquidity Analysis
- Order book simulation and analysis
- Bid-ask spread estimation
- Market impact modeling (Amihud illiquidity, Kyle's lambda)
- Liquidity heatmaps by time-of-day
- Flash crash scenario modeling
- Order book depth analysis
"""
import logging
from datetime import datetime
from typing import Any, Dict, List, Optional
import numpy as np
import pandas as pd
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel, Field
logger = logging.getLogger(__name__)
liquidity_router = APIRouter(prefix="/liquidity", tags=["liquidity"])
# =============================================================================
# Models
# =============================================================================
class OrderBookRequest(BaseModel):
ticker: str = Field(default="SPY")
mid_price: float = Field(default=100.0, ge=0.01)
num_levels: int = Field(default=10, ge=3, le=50)
spread_bps: float = Field(default=2.0, ge=0.1, le=100)
depth_factor: float = Field(default=1.0, ge=0.1, le=5.0, description="Multiplier for order depth")
class OrderBookLevel(BaseModel):
price: float
quantity: int
cumulative_quantity: int
price_impact_bps: float
class OrderBookResponse(BaseModel):
ticker: str
mid_price: float
best_bid: float
best_ask: float
spread_bps: float
bid_levels: List[OrderBookLevel]
ask_levels: List[OrderBookLevel]
total_bid_depth: int
total_ask_depth: int
bid_ask_imbalance: float # >0 = buy pressure, <0 = sell pressure
market_depth_score: float # 0-1, higher = deeper
timestamp: datetime = Field(default_factory=datetime.utcnow)
class LiquidityMetricsRequest(BaseModel):
ticker: str = Field(default="SPY")
lookback_days: int = Field(default=252, ge=20, le=1260)
volume_percentile: float = Field(default=50.0, ge=1, le=99)
class LiquidityMetricsResponse(BaseModel):
ticker: str
amihud_ratio: float # daily price impact per $M volume
bid_ask_spread_bps: float
kyle_lambda: float # price impact per unit order flow
roll_spread: float # Roll's effective spread estimate
turnover_ratio: float
illiquidity_percentile: float # 0=very liquid, 100=very illiquid
liquidity_score: float # 0-100 composite
liquidity_regime: str # HIGH | NORMAL | LOW | CRISIS
timestamp: datetime = Field(default_factory=datetime.utcnow)
class MarketImpactRequest(BaseModel):
ticker: str = Field(default="SPY")
trade_size_shares: int = Field(default=10000, ge=100)
average_daily_volume: int = Field(default=5000000, ge=10000)
volatility_daily: float = Field(default=0.012, ge=0.001, le=0.2)
price: float = Field(default=100.0, ge=0.01)
side: str = Field(default="BUY", description="BUY | SELL")
class MarketImpactResponse(BaseModel):
ticker: str
trade_size_shares: int
participation_rate: float
temporary_impact_bps: float
permanent_impact_bps: float
total_impact_bps: float
implementation_shortfall_bps: float
estimated_cost_usd: float
time_to_execute_mins: float
optimal_execution_strategy: str
timestamp: datetime = Field(default_factory=datetime.utcnow)
class FlashCrashRequest(BaseModel):
ticker: str = Field(default="SPY")
shock_magnitude_pct: float = Field(default=-5.0, ge=-30, le=-1)
shock_speed: str = Field(default="fast", description="slow | fast | instantaneous")
liquidity_withdrawal: float = Field(default=0.7, ge=0.1, le=1.0,
description="Fraction of liquidity removed")
recovery_half_life_mins: float = Field(default=20.0, ge=1, le=120)
class FlashCrashResponse(BaseModel):
ticker: str
pre_crash_price: float
trough_price: float
max_decline_pct: float
time_to_trough_mins: float
recovery_time_mins: float
bid_ask_widening_factor: float
liquidity_vacuum_depth_pct: float
cascade_tickers: List[str]
vix_spike_estimate: float
timestamp: datetime = Field(default_factory=datetime.utcnow)
# =============================================================================
# Helpers
# =============================================================================
def _sim_order_book(mid: float, spread_bps: float, num_levels: int, depth_factor: float, side: str):
"""Simulate a one-sided order book."""
half_spread = mid * spread_bps / 20000
levels = []
cum_qty = 0
rng = np.random.default_rng(42)
for i in range(num_levels):
if side == "bid":
price = mid - half_spread - i * mid * 0.0005 * (1 + i * 0.1)
else:
price = mid + half_spread + i * mid * 0.0005 * (1 + i * 0.1)
qty = max(100, int(rng.exponential(1000 * depth_factor / (i + 1))))
cum_qty += qty
impact_bps = abs(price - mid) / mid * 10000
levels.append(OrderBookLevel(
price=round(price, 4), quantity=qty,
cumulative_quantity=cum_qty,
price_impact_bps=round(impact_bps, 2),
))
return levels
# =============================================================================
# Endpoints
# =============================================================================
@liquidity_router.post(
"/order-book",
response_model=OrderBookResponse,
summary="Simulate order book",
description="Generate a realistic limit order book with bid/ask levels, depths and price impact by level",
)
async def simulate_order_book(request: OrderBookRequest) -> OrderBookResponse:
"""
Generates a complete order book simulation with configurable spread, depth and levels.
Uses calibrated exponential decay for quantity distribution across price levels.
"""
try:
try:
from quantlib_pro.market_microstructure import CalibratedOrderBookSimulator
sim = CalibratedOrderBookSimulator()
result = sim.simulate(request.ticker, request.mid_price, request.num_levels)
# parse result if available
except Exception:
pass # fall through to simulation
bid_levels = _sim_order_book(request.mid_price, request.spread_bps, request.num_levels, request.depth_factor, "bid")
ask_levels = _sim_order_book(request.mid_price, request.spread_bps, request.num_levels, request.depth_factor, "ask")
half_spread = request.mid_price * request.spread_bps / 20000
best_bid = round(request.mid_price - half_spread, 4)
best_ask = round(request.mid_price + half_spread, 4)
total_bid = sum(l.quantity for l in bid_levels)
total_ask = sum(l.quantity for l in ask_levels)
imbalance = (total_bid - total_ask) / (total_bid + total_ask)
depth_score = min(1.0, (total_bid + total_ask) / 100000)
return OrderBookResponse(
ticker=request.ticker,
mid_price=request.mid_price,
best_bid=best_bid,
best_ask=best_ask,
spread_bps=round(request.spread_bps, 2),
bid_levels=bid_levels,
ask_levels=ask_levels,
total_bid_depth=total_bid,
total_ask_depth=total_ask,
bid_ask_imbalance=round(float(imbalance), 4),
market_depth_score=round(float(depth_score), 4),
)
except Exception as e:
logger.error(f"Order book error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@liquidity_router.post(
"/metrics",
response_model=LiquidityMetricsResponse,
summary="Compute liquidity metrics",
description="Compute Amihud illiquidity ratio, Kyle's lambda, Roll's spread, and composite liquidity score",
)
async def compute_liquidity_metrics(request: LiquidityMetricsRequest) -> LiquidityMetricsResponse:
"""
Estimates key microstructure liquidity measures. Lower Amihud/Kyle = more liquid.
Composite illiquidity percentile combines all measures into a single score.
"""
try:
seed = abs(hash(request.ticker)) % 9999
rng = np.random.default_rng(seed)
n = request.lookback_days
prices = 100 * np.exp(np.cumsum(rng.normal(0.0003, 0.012, n)))
returns = np.diff(np.log(prices))
volumes = rng.lognormal(mean=13, sigma=0.5, size=n - 1) * 1000
dollar_vol = prices[1:] * volumes
amihud = float(np.mean(np.abs(returns) / (dollar_vol / 1e6)))
# Kyle's lambda: price impact regression
order_flow = rng.normal(0, 1, len(returns))
kyle_lambda = float(np.cov(returns, order_flow)[0, 1] / np.var(order_flow))
# Roll's spread (from covariance of consecutive returns)
cov = float(np.cov(returns[:-1], returns[1:])[0, 1])
roll_spread = 2 * np.sqrt(max(-cov, 0))
# Bid-ask spread estimate
ba_spread_bps = float(roll_spread * 10000 + 1.0)
# Turnover
avg_price = float(np.mean(prices))
avg_vol = float(np.mean(volumes))
turnover = avg_vol * avg_price / (avg_price * 1e8)
illiq_pct = min(99.0, float(amihud * 1e5))
if illiq_pct < 20:
regime = "HIGH"
elif illiq_pct < 50:
regime = "NORMAL"
elif illiq_pct < 80:
regime = "LOW"
else:
regime = "CRISIS"
return LiquidityMetricsResponse(
ticker=request.ticker,
amihud_ratio=round(amihud, 8),
bid_ask_spread_bps=round(ba_spread_bps, 3),
kyle_lambda=round(abs(kyle_lambda), 6),
roll_spread=round(roll_spread, 6),
turnover_ratio=round(turnover, 6),
illiquidity_percentile=round(illiq_pct, 2),
liquidity_score=round(100 - illiq_pct, 2),
liquidity_regime=regime,
)
except Exception as e:
logger.error(f"Liquidity metrics error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@liquidity_router.post(
"/market-impact",
response_model=MarketImpactResponse,
summary="Estimate market impact cost",
description="Estimate temporary and permanent price impact using square-root model, plus implementation shortfall",
)
async def estimate_market_impact(request: MarketImpactRequest) -> MarketImpactResponse:
"""
Uses the Almgren-Chriss square-root market impact model to estimate
temporary and permanent impact for a given order.
"""
try:
participation_rate = request.trade_size_shares / request.average_daily_volume
# Square-root impact model
eta = 0.1 # temporary impact coefficient
gamma = 0.05 # permanent impact coefficient
sigma = request.volatility_daily
adv_usd = request.average_daily_volume * request.price
temp_impact_bps = eta * sigma * float(np.sqrt(participation_rate)) * 10000
perm_impact_bps = gamma * sigma * participation_rate * 10000
total_impact_bps = temp_impact_bps + perm_impact_bps
# Implementation shortfall
is_bps = total_impact_bps * 0.6 # simplified
trade_value = request.trade_size_shares * request.price
estimated_cost = trade_value * total_impact_bps / 10000
# Time to execute (VWAP schedule)
time_mins = max(5, participation_rate * 390) # 390 min trading day
if participation_rate < 0.05:
strategy = "VWAP_AGGRESSIVE"
elif participation_rate < 0.15:
strategy = "TWAP_STANDARD"
elif participation_rate < 0.30:
strategy = "POV_10_PCT"
else:
strategy = "ALMGREN_CHRISS_OPTIMAL"
return MarketImpactResponse(
ticker=request.ticker,
trade_size_shares=request.trade_size_shares,
participation_rate=round(participation_rate, 4),
temporary_impact_bps=round(temp_impact_bps, 3),
permanent_impact_bps=round(perm_impact_bps, 3),
total_impact_bps=round(total_impact_bps, 3),
implementation_shortfall_bps=round(is_bps, 3),
estimated_cost_usd=round(estimated_cost, 2),
time_to_execute_mins=round(time_mins, 1),
optimal_execution_strategy=strategy,
)
except Exception as e:
logger.error(f"Market impact error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@liquidity_router.post(
"/flash-crash",
response_model=FlashCrashResponse,
summary="Simulate flash crash scenario",
description="Model a flash crash event with liquidity withdrawal, price cascade, and recovery dynamics",
)
async def simulate_flash_crash(request: FlashCrashRequest) -> FlashCrashResponse:
"""
Models a flash crash scenario with configurable shock size, speed, liquidity
withdrawal and recovery dynamics. Cascading effects to correlated assets are estimated.
"""
try:
seed = abs(hash(request.ticker)) % 9999
rng = np.random.default_rng(seed)
pre_price = 100.0
speed_factor = {"slow": 0.5, "fast": 2.0, "instantaneous": 5.0}.get(request.shock_speed, 2.0)
trough_pct = request.shock_magnitude_pct * (1 + request.liquidity_withdrawal * 0.5)
trough_pct = max(trough_pct, -30.0)
trough_price = pre_price * (1 + trough_pct / 100)
time_to_trough = max(0.5, 5.0 / speed_factor)
recovery_time = request.recovery_half_life_mins * 2.5
bid_ask_widening = 1 / (1 - request.liquidity_withdrawal) * speed_factor
vacuum_depth = abs(trough_pct) * request.liquidity_withdrawal
corr_tickers = [t for t in ["QQQ", "IWM", "DIA", "XLF", "XLK"] if t != request.ticker][:3]
vix_spike = abs(trough_pct) * 2.5
return FlashCrashResponse(
ticker=request.ticker,
pre_crash_price=round(pre_price, 2),
trough_price=round(trough_price, 2),
max_decline_pct=round(trough_pct, 3),
time_to_trough_mins=round(time_to_trough, 2),
recovery_time_mins=round(recovery_time, 1),
bid_ask_widening_factor=round(bid_ask_widening, 2),
liquidity_vacuum_depth_pct=round(vacuum_depth, 2),
cascade_tickers=corr_tickers,
vix_spike_estimate=round(vix_spike, 1),
)
except Exception as e:
logger.error(f"Flash crash error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@liquidity_router.get(
"/heatmap/{ticker}",
summary="Liquidity heatmap by time-of-day",
description="Returns intraday liquidity profile showing best/worst execution windows",
)
async def get_liquidity_heatmap(ticker: str) -> Dict:
"""Returns simulated intraday liquidity profile across trading hours."""
seed = abs(hash(ticker)) % 9999
rng = np.random.default_rng(seed)
hours = list(range(9, 17)) # 9 AM - 4 PM
profile = {}
for h in hours:
# U-shaped intraday liquidity pattern
dist_from_open = abs(h - 9)
dist_from_close = abs(16 - h)
liq_score = 100 - 30 * np.exp(-min(dist_from_open, dist_from_close) * 0.5)
liq_score += rng.normal(0, 5)
spread_bps = max(0.5, 3.0 - liq_score / 50 + rng.normal(0, 0.3))
profile[f"{h:02d}:00"] = {
"liquidity_score": round(float(np.clip(liq_score, 40, 100)), 1),
"bid_ask_spread_bps": round(float(spread_bps), 2),
"avg_trade_size": int(rng.integers(500, 5000)),
"volume_pct_of_day": round(float(rng.dirichlet(np.ones(8))[h - 9] * 100), 1),
}
best_hour = max(profile, key=lambda h: profile[h]["liquidity_score"])
worst_hour = min(profile, key=lambda h: profile[h]["liquidity_score"])
return {
"ticker": ticker,
"hourly_profile": profile,
"best_execution_window": best_hour,
"worst_execution_window": worst_hour,
"recommendation": f"Optimal execution between {best_hour} and {int(best_hour[:2])+1:02d}:00",
"timestamp": datetime.utcnow().isoformat(),
}