Loading...
Loading...
Compare original and translation side by side
| Concept | What It Tells You |
|---|---|
| Bid-ask spread | Cost of immediacy — how much you pay to trade now vs later |
| Price impact | How your order moves the market price |
| Order book imbalance | Short-term directional predictor from queue sizes |
| Adverse selection | Risk of trading against informed counterparties |
| Inventory risk | Market maker exposure from accumulated positions |
| Execution quality | How well your fills compare to a benchmark |
| 概念 | 作用 |
|---|---|
| Bid-ask spread | 即时交易成本——立即交易与延迟交易的成本差异 |
| Price impact | 你的订单对市场价格的影响程度 |
| Order book imbalance | 通过挂单队列规模预测短期价格方向 |
| Adverse selection | 与知情交易对手方交易的风险 |
| Inventory risk | 做市商因持仓积累面临的风险 |
| Execution quality | 你的成交情况与基准指标的对比表现 |
undefinedundefined
The **effective spread** matters most for execution quality. The difference
between effective and realized spread measures adverse selection — what the
market maker loses to informed flow.
---
**有效价差**是衡量执行质量的核心指标。有效价差与实现价差的差值代表逆向选择成本——即做市商因知情资金流产生的损失。
---price_change = lambda * net_order_flowimport numpy as np
from numpy.linalg import lstsq
def estimate_kyle_lambda(
price_changes: np.ndarray,
signed_volumes: np.ndarray,
) -> float:
"""Estimate Kyle's lambda from trade data.
Args:
price_changes: Midprice changes between trades.
signed_volumes: Trade volume * trade_sign (+1 buy, -1 sell).
Returns:
Estimated lambda (price impact per unit volume).
"""
X = signed_volumes.reshape(-1, 1)
beta, _, _, _ = lstsq(X, price_changes, rcond=None)
return float(beta[0])references/price_formation.mdprice_change = lambda * net_order_flowimport numpy as np
from numpy.linalg import lstsq
def estimate_kyle_lambda(
price_changes: np.ndarray,
signed_volumes: np.ndarray,
) -> float:
"""从交易数据中估算Kyle模型的lambda值。
参数:
price_changes: 交易间的中间价变动。
signed_volumes: 交易量*交易方向标识(+1为买入,-1为卖出)。
返回:
估算的lambda值(单位交易量的价格冲击)。
"""
X = signed_volumes.reshape(-1, 1)
beta, _, _, _ = lstsq(X, price_changes, rcond=None)
return float(beta[0])references/price_formation.mdtotal_impact = permanent_impact + temporary_impact
permanent = gamma * (shares / ADV)
temporary = eta * (shares / time_horizon) ^ alphatotal_impact = permanent_impact + temporary_impact
permanent = gamma * (shares / ADV)
temporary = eta * (shares / time_horizon) ^ alphadef square_root_impact(
order_size: float,
daily_volume: float,
volatility: float,
impact_coefficient: float = 0.1,
) -> float:
"""Estimate price impact using the square root model.
Args:
order_size: Number of units to trade.
daily_volume: Average daily volume.
volatility: Daily return volatility (decimal).
impact_coefficient: Empirical constant (typically 0.05-0.20).
Returns:
Expected price impact as a fraction.
"""
return impact_coefficient * volatility * (order_size / daily_volume) ** 0.5def square_root_impact(
order_size: float,
daily_volume: float,
volatility: float,
impact_coefficient: float = 0.1,
) -> float:
"""使用平方根模型估算价格冲击。
参数:
order_size: 交易单位数量。
daily_volume: 平均日交易量。
volatility: 日收益波动率(小数形式)。
impact_coefficient: 经验常数(通常为0.05-0.20)。
返回:
预期价格冲击比例。
"""
return impact_coefficient * volatility * (order_size / daily_volume) ** 0.5def order_book_imbalance(
bid_qty: float,
ask_qty: float,
) -> float:
"""Compute order book imbalance.
Returns:
Imbalance in [-1, 1]. Positive = more bids (bullish).
"""
total = bid_qty + ask_qty
if total == 0:
return 0.0
return (bid_qty - ask_qty) / totaldef order_book_imbalance(
bid_qty: float,
ask_qty: float,
) -> float:
"""计算订单簿失衡度。
返回:
失衡度范围为[-1, 1]。正数代表买盘更强(看涨)。
"""
total = bid_qty + ask_qty
if total == 0:
return 0.0
return (bid_qty - ask_qty) / totalintensity(t) = mu + sum(alpha * exp(-beta * (t - t_i)))intensity(t) = mu + sum(alpha * exp(-beta * (t - t_i)))qreservation_price = midprice - q * gamma * sigma^2 * T
optimal_spread = gamma * sigma^2 * T + (2/gamma) * ln(1 + gamma/k)qgammasigmaTkqreservation_price = midprice - q * gamma * sigma^2 * T
optimal_spread = gamma * sigma^2 * T + (2/gamma) * ln(1 + gamma/k)qgammasigmaTkdef vwap(prices: list[float], volumes: list[float]) -> float:
"""Compute VWAP from trade prices and volumes."""
pv_sum = sum(p * v for p, v in zip(prices, volumes))
v_sum = sum(volumes)
return pv_sum / v_sum if v_sum > 0 else 0.0def vwap(prices: list[float], volumes: list[float]) -> float:
"""根据交易价格和成交量计算VWAP。"""
pv_sum = sum(p * v for p, v in zip(prices, volumes))
v_sum = sum(volumes)
return pv_sum / v_sum if v_sum > 0 else 0.0undefinedundefinedimplementation_shortfall = (execution_price - decision_price) * quantityreferences/execution_quality.mdimplementation_shortfall = (execution_price - decision_price) * quantityreferences/execution_quality.md| Dimension | CEX (LOB) | DEX (AMM) |
|---|---|---|
| Price discovery | Limit orders express willingness to trade | Algorithmic curve (x·y=k) |
| Spread | Set by competing market makers | Determined by pool depth and fee tier |
| Depth | Visible order book | Implicit from TVL and curve shape |
| Adverse selection | MMs reprice on information | LPs suffer impermanent loss |
| Execution | Price-time priority | First-come via block inclusion |
| Latency | Microseconds | Block time (400ms Solana, 12s Ethereum) |
| MEV | Front-running is harder (colocated MMs) | Sandwich attacks are endemic |
| Fees | Maker/taker (often maker rebate) | Fixed tier (e.g., 5, 30, 100 bps) |
references/cex_vs_dex.md| 维度 | CEX(LOB) | DEX(AMM) |
|---|---|---|
| 价格发现 | 限价订单表达交易意愿 | 算法曲线(x·y=k) |
| 价差 | 由竞争做市商设定 | 由资金池深度和手续费层级决定 |
| 深度 | 可见订单簿 | 由TVL和曲线形态隐含 |
| 逆向选择 | 做市商根据信息重新定价 | 流动性提供者遭受无常损失 |
| 执行规则 | 价格-时间优先 | 区块打包顺序优先 |
| 延迟 | 微秒级 | 区块时间(Solana为400ms,以太坊为12s) |
| MEV | 抢先交易难度较高(托管做市商) | 三明治攻击普遍存在 |
| 手续费 | 做市/吃单手续费(通常做市商有返佣) | 固定层级(如5、30、100个基点) |
references/cex_vs_dex.md| Tier | Maker Fee | Taker Fee | Net Spread Required |
|---|---|---|---|
| VIP 0 | 0.10% | 0.10% | 20 bps to break even |
| VIP 5 | 0.02% | 0.05% | 7 bps to break even |
| VIP 9 | -0.005% | 0.03% | 2.5 bps + rebate income |
def maker_pnl_per_trade(
spread_captured_bps: float,
maker_fee_bps: float,
adverse_selection_bps: float,
) -> float:
"""Compute market maker P&L per round trip.
Args:
spread_captured_bps: Half-spread captured on each side.
maker_fee_bps: Maker fee (negative = rebate).
adverse_selection_bps: Expected loss to informed flow.
Returns:
Net P&L in basis points per round trip.
"""
gross = 2 * spread_captured_bps # earn half-spread on each leg
fees = 2 * maker_fee_bps # pay/receive fee on each leg
return gross - fees - adverse_selection_bps| 层级 | 做市商手续费 | 吃单手续费 | 盈亏平衡所需净价差 |
|---|---|---|---|
| VIP 0 | 0.10% | 0.10% | 20个基点 |
| VIP 5 | 0.02% | 0.05% | 7个基点 |
| VIP 9 | -0.005% | 0.03% | 2.5个基点 + 返佣收入 |
def maker_pnl_per_trade(
spread_captured_bps: float,
maker_fee_bps: float,
adverse_selection_bps: float,
) -> float:
"""计算做市商每轮交易的盈亏。
参数:
spread_captured_bps: 每边获取的半价差(基点)。
maker_fee_bps: 做市商手续费(负数代表返佣)。
adverse_selection_bps: 预期因知情资金流产生的损失(基点)。
返回:
每轮交易的净盈亏(基点)。
"""
gross = 2 * spread_captured_bps # 每边赚取半价差
fees = 2 * maker_fee_bps # 每边支付/收取手续费
return gross - fees - adverse_selection_bpsreferences/price_formation.mdreferences/execution_quality.mdreferences/cex_vs_dex.mdreferences/price_formation.mdreferences/execution_quality.mdreferences/cex_vs_dex.mdscripts/spread_analysis.pyscripts/market_maker_sim.pyscripts/spread_analysis.pyscripts/market_maker_sim.pyuv pip install numpy pandas scipy matplotlibuv pip install numpy pandas scipy matplotlib