$npx -y skills add agiprolabs/claude-trading-skills --skill market-microstructure-traditionalTraditional market microstructure concepts applied to crypto — order book dynamics, market making theory, price formation models, execution quality measurement, and CEX vs DEX structural differences
| 1 | # Market Microstructure (Traditional) |
| 2 | |
| 3 | Market microstructure studies how orders become trades and how trades become |
| 4 | prices. Understanding these mechanics is essential for execution optimization, |
| 5 | market making, and detecting informed flow. This skill covers limit order book |
| 6 | (LOB) theory as applied to crypto markets on centralized exchanges, and compares |
| 7 | LOB mechanics to the AMM-based structure of DEXes. |
| 8 | |
| 9 | ## Core Concepts |
| 10 | |
| 11 | | Concept | What It Tells You | |
| 12 | |---|---| |
| 13 | | **Bid-ask spread** | Cost of immediacy — how much you pay to trade now vs later | |
| 14 | | **Price impact** | How your order moves the market price | |
| 15 | | **Order book imbalance** | Short-term directional predictor from queue sizes | |
| 16 | | **Adverse selection** | Risk of trading against informed counterparties | |
| 17 | | **Inventory risk** | Market maker exposure from accumulated positions | |
| 18 | | **Execution quality** | How well your fills compare to a benchmark | |
| 19 | |
| 20 | --- |
| 21 | |
| 22 | ## Bid-Ask Spread Decomposition |
| 23 | |
| 24 | The bid-ask spread is not a single thing. It decomposes into three components |
| 25 | (Roll, 1984; Glosten & Harris, 1988): |
| 26 | |
| 27 | 1. **Adverse selection** — compensation for trading against informed traders |
| 28 | 2. **Inventory holding** — compensation for carrying risk |
| 29 | 3. **Order processing** — fixed costs of providing liquidity (fees, infrastructure) |
| 30 | |
| 31 | ### Spread Measures |
| 32 | |
| 33 | ```python |
| 34 | # Quoted spread: what you see on the order book |
| 35 | quoted_spread = best_ask - best_bid |
| 36 | quoted_spread_bps = (best_ask - best_bid) / midprice * 10_000 |
| 37 | |
| 38 | # Effective spread: what you actually pay (accounts for price improvement) |
| 39 | effective_half_spread = abs(trade_price - midprice_at_trade) |
| 40 | effective_spread_bps = effective_half_spread / midprice_at_trade * 10_000 |
| 41 | |
| 42 | # Realized spread: market maker's actual profit (after price moves) |
| 43 | # Measured at trade_price vs midprice N seconds later |
| 44 | realized_spread = trade_sign * (trade_price - midprice_after_delay) |
| 45 | ``` |
| 46 | |
| 47 | The **effective spread** matters most for execution quality. The difference |
| 48 | between effective and realized spread measures adverse selection — what the |
| 49 | market maker loses to informed flow. |
| 50 | |
| 51 | --- |
| 52 | |
| 53 | ## Price Formation Models |
| 54 | |
| 55 | ### Glosten-Milgrom (1985) |
| 56 | |
| 57 | A sequential trade model where the market maker sets bid and ask prices to |
| 58 | break even against a mix of informed and uninformed traders. |
| 59 | |
| 60 | - Market maker quotes reflect *expected value conditional on trade direction* |
| 61 | - Spread exists purely due to adverse selection |
| 62 | - Prices converge to true value as information is revealed through trades |
| 63 | |
| 64 | Key insight: the spread is wider when: |
| 65 | - Probability of informed trading (PIN) is higher |
| 66 | - Information asymmetry is larger |
| 67 | - Uninformed trading volume is lower |
| 68 | |
| 69 | ### Kyle's Lambda (1985) |
| 70 | |
| 71 | Kyle models a single informed trader, noise traders, and a market maker. |
| 72 | The market maker sets price as a linear function of net order flow: |
| 73 | |
| 74 | ``` |
| 75 | price_change = lambda * net_order_flow |
| 76 | ``` |
| 77 | |
| 78 | **Lambda (λ)** measures permanent price impact per unit of signed volume. |
| 79 | Higher lambda = less liquid market. Lambda is estimated by regressing |
| 80 | price changes on signed volume: |
| 81 | |
| 82 | ```python |
| 83 | import numpy as np |
| 84 | from numpy.linalg import lstsq |
| 85 | |
| 86 | def estimate_kyle_lambda( |
| 87 | price_changes: np.ndarray, |
| 88 | signed_volumes: np.ndarray, |
| 89 | ) -> float: |
| 90 | """Estimate Kyle's lambda from trade data. |
| 91 | |
| 92 | Args: |
| 93 | price_changes: Midprice changes between trades. |
| 94 | signed_volumes: Trade volume * trade_sign (+1 buy, -1 sell). |
| 95 | |
| 96 | Returns: |
| 97 | Estimated lambda (price impact per unit volume). |
| 98 | """ |
| 99 | X = signed_volumes.reshape(-1, 1) |
| 100 | beta, _, _, _ = lstsq(X, price_changes, rcond=None) |
| 101 | return float(beta[0]) |
| 102 | ``` |
| 103 | |
| 104 | See `references/price_formation.md` for full model derivations and the PIN |
| 105 | model for measuring informed trading probability. |
| 106 | |
| 107 | --- |
| 108 | |
| 109 | ## Price Impact Models |
| 110 | |
| 111 | ### Temporary vs Permanent Impact (Almgren-Chriss) |
| 112 | |
| 113 | When executing a large order: |
| 114 | |
| 115 | - **Temporary impact** — price displacement that reverts after your order. |
| 116 | Caused by consuming standing liquidity. |
| 117 | - **Permanent impact** — information content of your trade that moves the |
| 118 | equilibrium price. Does not revert. |
| 119 | |
| 120 | ``` |
| 121 | total_impact = permanent_impact + temporary_impact |
| 122 | permanent = gamma * (shares / ADV) |
| 123 | temporary = eta * (shares / time_horizon) ^ alpha |
| 124 | ``` |
| 125 | |
| 126 | Typical alpha values: 0.5-0.7 (square root impact is a robust empirical finding). |
| 127 | |
| 128 | ### Square Root Impact Law |
| 129 | |
| 130 | Empirically, price impact scales as the square root of order size relative |
| 131 | to daily volume: |
| 132 | |
| 133 | ```python |
| 134 | def square_root_impact( |
| 135 | order_size: float, |
| 136 | daily_volume: float, |
| 137 | volatility: float, |
| 138 | impact_coefficient: float = 0.1, |
| 139 | ) -> float: |
| 140 | """Estimate price impact using the square root model. |
| 141 | |
| 142 | Args: |
| 143 | order_size: Number of units to trade. |
| 144 | daily_volume: Average daily volume. |
| 145 | volatility: Daily return volat |