Market Microstructure Traditional logo

Market Microstructure Traditional

Community
agiprolabs
market-microstructure-traditional

Traditional market microstructure concepts applied to crypto — order book dynamics, market making theory, price formation models, execution quality measurement, and CEX vs DEX structural differences

Overview

Publisheragiprolabs
Repositoryclaude-trading-skills
Skill namemarket-microstructure-traditional
Stars
434
Forks
85
Bundled files
5
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 5 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by agiprolabs on GitHub. Read the source before you install it.

Installation

Install the Market Microstructure Traditional AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/agiprolabs/claude-trading-skills/tree/main/skills/market-microstructure-traditional
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/agiprolabs/claude-trading-skills.git /tmp/claude-trading-skills
mkdir -p .claude/skills
cp -r /tmp/claude-trading-skills/skills/market-microstructure-traditional .claude/skills/market-microstructure-traditional
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Market Microstructure Traditional in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Market Microstructure Traditional on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Market Microstructure Traditional is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Market Microstructure (Traditional)

Market microstructure studies how orders become trades and how trades become prices. Understanding these mechanics is essential for execution optimization, market making, and detecting informed flow. This skill covers limit order book (LOB) theory as applied to crypto markets on centralized exchanges, and compares LOB mechanics to the AMM-based structure of DEXes.

Core Concepts

ConceptWhat It Tells You
Bid-ask spreadCost of immediacy — how much you pay to trade now vs later
Price impactHow your order moves the market price
Order book imbalanceShort-term directional predictor from queue sizes
Adverse selectionRisk of trading against informed counterparties
Inventory riskMarket maker exposure from accumulated positions
Execution qualityHow well your fills compare to a benchmark

Bid-Ask Spread Decomposition

The bid-ask spread is not a single thing. It decomposes into three components (Roll, 1984; Glosten & Harris, 1988):

  1. Adverse selection — compensation for trading against informed traders
  2. Inventory holding — compensation for carrying risk
  3. Order processing — fixed costs of providing liquidity (fees, infrastructure)

Spread Measures

python
# Quoted spread: what you see on the order book
quoted_spread = best_ask - best_bid
quoted_spread_bps = (best_ask - best_bid) / midprice * 10_000

# Effective spread: what you actually pay (accounts for price improvement)
effective_half_spread = abs(trade_price - midprice_at_trade)
effective_spread_bps = effective_half_spread / midprice_at_trade * 10_000

# Realized spread: market maker's actual profit (after price moves)
# Measured at trade_price vs midprice N seconds later
realized_spread = trade_sign * (trade_price - midprice_after_delay)

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 Formation Models

Glosten-Milgrom (1985)

A sequential trade model where the market maker sets bid and ask prices to break even against a mix of informed and uninformed traders.

  • Market maker quotes reflect expected value conditional on trade direction
  • Spread exists purely due to adverse selection
  • Prices converge to true value as information is revealed through trades

Key insight: the spread is wider when:

  • Probability of informed trading (PIN) is higher
  • Information asymmetry is larger
  • Uninformed trading volume is lower

Kyle's Lambda (1985)

Kyle models a single informed trader, noise traders, and a market maker. The market maker sets price as a linear function of net order flow:

price_change = lambda * net_order_flow

Lambda (λ) measures permanent price impact per unit of signed volume. Higher lambda = less liquid market. Lambda is estimated by regressing price changes on signed volume:

python
import 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])

See references/price_formation.md for full model derivations and the PIN model for measuring informed trading probability.


Price Impact Models

Temporary vs Permanent Impact (Almgren-Chriss)

When executing a large order:

  • Temporary impact — price displacement that reverts after your order. Caused by consuming standing liquidity.
  • Permanent impact — information content of your trade that moves the equilibrium price. Does not revert.
total_impact = permanent_impact + temporary_impact
permanent = gamma * (shares / ADV)
temporary = eta * (shares / time_horizon) ^ alpha

Typical alpha values: 0.5-0.7 (square root impact is a robust empirical finding).

Square Root Impact Law

Empirically, price impact scales as the square root of order size relative to daily volume:

python
def 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.5

Order Book Imbalance

The ratio of bid-side to ask-side depth near the top of the book predicts short-term price direction:

python
def 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) / total

Imbalance at levels 1-5 is a strong short-term predictor (Cont et al., 2014). Deeper levels add predictive power but decay quickly.


Trade Arrival Processes

Poisson Process

Simplest model: trades arrive at a constant rate λ. Inter-arrival times are exponentially distributed. Useful as a baseline but too simple for real order flow.

Hawkes Process

Self-exciting point process where each trade increases the probability of subsequent trades. Captures clustering in order flow:

intensity(t) = mu + sum(alpha * exp(-beta * (t - t_i)))
  • mu: baseline arrival rate
  • alpha: excitation magnitude (how much each event boosts intensity)
  • beta: decay rate (how fast excitation fades)
  • alpha/beta < 1: stationarity condition (branching ratio)

The branching ratio α/β measures the fraction of trades that are reactions rather than innovations. Typical values: 0.5-0.8 in crypto markets (high reactivity).


Market Maker Economics

A market maker profits from the spread but faces three risks:

  1. Adverse selection — losing to informed traders
  2. Inventory risk — accumulated directional exposure
  3. Competition — other MMs narrowing the spread

Avellaneda-Stoikov Model

The optimal bid and ask quotes for a market maker with inventory q:

reservation_price = midprice - q * gamma * sigma^2 * T
optimal_spread = gamma * sigma^2 * T + (2/gamma) * ln(1 + gamma/k)

Where:

  • q: current inventory (positive = long)
  • gamma: risk aversion parameter
  • sigma: volatility
  • T: time remaining
  • k: order arrival rate parameter

Key insight: the reservation price skews away from inventory — a long market maker lowers their price to encourage sells.


Execution Quality Measurement

VWAP Benchmark

Volume-Weighted Average Price is the standard benchmark for passive execution:

python
def 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.0

# Execution quality vs VWAP
slippage_vs_vwap = (avg_fill_price - vwap_benchmark) / vwap_benchmark * 10_000

Implementation Shortfall

Measures total cost of executing vs the decision price (Perold, 1988):

implementation_shortfall = (execution_price - decision_price) * quantity

Decomposes into:

  • Delay cost: price drift between decision and first fill
  • Market impact: price move caused by your order
  • Timing cost: cost of breaking the order into slices
  • Opportunity cost: value of unfilled portions

See references/execution_quality.md for complete methodology.


CEX Order Book vs DEX AMM

DimensionCEX (LOB)DEX (AMM)
Price discoveryLimit orders express willingness to tradeAlgorithmic curve (x·y=k)
SpreadSet by competing market makersDetermined by pool depth and fee tier
DepthVisible order bookImplicit from TVL and curve shape
Adverse selectionMMs reprice on informationLPs suffer impermanent loss
ExecutionPrice-time priorityFirst-come via block inclusion
LatencyMicrosecondsBlock time (400ms Solana, 12s Ethereum)
MEVFront-running is harder (colocated MMs)Sandwich attacks are endemic
FeesMaker/taker (often maker rebate)Fixed tier (e.g., 5, 30, 100 bps)

When to use CEX: large orders, latency-sensitive strategies, tight spreads needed, BTC/ETH/major pairs.

When to use DEX: long-tail tokens, censorship resistance, composability with DeFi, transparent execution.

See references/cex_vs_dex.md for detailed structural comparison.


Maker/Taker Fee Structures

CEX fee tiers create incentive asymmetries:

TierMaker FeeTaker FeeNet Spread Required
VIP 00.10%0.10%20 bps to break even
VIP 50.02%0.05%7 bps to break even
VIP 9-0.005%0.03%2.5 bps + rebate income

At high tiers, maker rebates mean market makers are paid to provide liquidity. This fundamentally changes strategy economics:

python
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

Files

References

  • references/price_formation.md — Glosten-Milgrom, Kyle model, PIN model, spread decomposition
  • references/execution_quality.md — VWAP, TWAP, implementation shortfall, slippage decomposition
  • references/cex_vs_dex.md — Structural comparison of LOB vs AMM, hybrid models, routing decisions

Scripts

  • scripts/spread_analysis.py — Analyze bid-ask spreads, compute effective/realized/quoted spread from trade data (--demo mode with synthetic order book)
  • scripts/market_maker_sim.py — Market maker simulation with inventory management and P&L (--demo mode with synthetic price path)

Dependencies

bash
uv pip install numpy pandas scipy matplotlib

Related Skills

  • market-microstructure — On-chain DEX microstructure (AMM-specific)
  • slippage-modeling — Execution cost estimation and modeling
  • liquidity-analysis — Pool and order book depth analysis
  • order-execution — Practical execution algorithms
  • mev-analysis — MEV risk in on-chain execution

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Market Microstructure Traditional AI skill do?

Traditional market microstructure concepts applied to crypto — order book dynamics, market making theory, price formation models, execution quality measurement, and CEX vs DEX structural differences

Why use Market Microstructure Traditional on TypingMind?

Because you install it once and use it with any model. Market Microstructure Traditional is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Market Microstructure Traditional in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/agiprolabs/claude-trading-skills/tree/main/skills/market-microstructure-traditional. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Market Microstructure Traditional?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Market Microstructure Traditional?

As many as you like. As long as a model supports skills, you can use Market Microstructure Traditional with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Market Microstructure Traditional AI skill free?

Yes. It is published on GitHub by agiprolabs under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇