From Zero to Quant · Chapter 2
Market Microstructure
Order books, bid-ask spreads, market makers, exchange matching
Chapter Overview#
Market microstructure is the study of how financial markets operate at the granular level—how orders are submitted, matched, executed, and how prices are formed through the interaction of buyers and sellers. For a systematic trader, understanding microstructure is critical because:
- Execution Quality: Your orders will experience slippage, market impact, and partial fills based on microstructure features
- Order Selection: Different order types (market, limit, stop) have fundamentally different execution mechanics
- Liquidity Extraction: Market makers and liquidity providers shape the order book structure you trade against
- Price Prediction: Microstructure features (order flow imbalance, large trades, bid-ask bounce) are predictable at millisecond timescales
- Risk Management: Understanding settlement, circuit breakers, and corporate actions prevents costly mistakes
This chapter bridges the gap from your systems engineering expertise to the specific mechanics of equity trading. We'll build working simulations, analyze NSE-specific mechanics, and integrate with Zerodha's actual trading platform.
Target Reader: Software engineers with ML expertise, zero finance knowledge, building production systems on NSE.
Prerequisites#
Required Knowledge#
- Python 3.9+ with NumPy, Pandas, Matplotlib
- Basic probability and statistics (distributions, moments)
- Understanding of data structures (heaps, trees, dictionaries)
- No prior finance knowledge required
Key Concepts We'll Use#
- Order Book: A real-time database of buy/sell offers at different prices
- Liquidity: The ability to buy/sell quickly without large price movements
- Bid-Ask Spread: The difference between what buyers will pay and sellers will accept
- Market Impact: The price movement caused by your trade
- Settlement: The mechanics of transferring money and securities
Tools & Libraries#
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from collections import defaultdict, deque
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple
from datetime import datetime, timedelta
import heapqModule 2.1: The Order Book
Learning Objectives#
- Understand the structure and mechanics of limit order books
- Implement a functioning order book simulator from scratch
- Analyze bid-ask spreads and their determinants
- Calculate market depth and liquidity metrics
- Interpret Level 1, Level 2, and Level 3 market data
Content#
2.1.1 Anatomy of the Order Book#
An order book is a real-time ledger of all outstanding buy (bid) and sell (ask) orders. For any trading pair (or stock), the book maintains two sides:
- Bid Side: Buy orders at various prices, ranked by price (descending) then time (ascending)
- Ask Side: Sell orders at various prices, ranked by price (ascending) then time (ascending)
Best Bid: The highest price anyone is willing to pay
Best Ask: The lowest price anyone is willing to sell at
Bid-Ask Spread:
Example: INFY (Infosys) Order Book at 10:30 AM IST
Bid Side Ask Side
Price | Volume Price | Volume
------+-------- ------+--------
1500 | 10,000 1510 | 5,000
1495 | 15,000 1515 | 20,000
1490 | 25,000 1520 | 10,000Current spread: rupees (or 10 basis points as % of mid-price)
2.1.2 Building a Limit Order Book Simulator#
Here's a production-quality limit order book implementation:
from enum import Enum
from typing import Dict, List, Optional, Tuple
from dataclasses import dataclass, field
from collections import defaultdict
import heapq
from datetime import datetime
class OrderSide(Enum):
"""Enumeration for order side."""
BUY = "BUY"
SELL = "SELL"
class OrderStatus(Enum):
"""Enumeration for order status."""
PENDING = "PENDING"
PARTIALLY_FILLED = "PARTIALLY_FILLED"
FILLED = "FILLED"
CANCELLED = "CANCELLED"
@dataclass
class Order:
"""Represents a single order in the limit order book.
Attributes:
order_id: Unique identifier for the order
timestamp: Time order was submitted (UTC)
side: BUY or SELL
price: Limit price in rupees
quantity: Number of shares
filled_quantity: Shares already executed
status: Current state of the order
"""
order_id: str
timestamp: datetime
side: OrderSide
price: float
quantity: int
filled_quantity: int = 0
status: OrderStatus = OrderStatus.PENDING
@property
def remaining_quantity(self) -> int:
"""Calculate unfilled quantity."""
return self.quantity - self.filled_quantity
def fill(self, quantity: int) -> None:
"""Fill a portion of the order.
Args:
quantity: Shares to fill (must be <= remaining_quantity)
"""
self.filled_quantity += quantity
if self.filled_quantity == self.quantity:
self.status = OrderStatus.FILLED
elif self.filled_quantity > 0:
self.status = OrderStatus.PARTIALLY_FILLED
@dataclass
class Trade:
"""Represents an executed trade matching two orders."""
trade_id: str
timestamp: datetime
buy_order_id: str
sell_order_id: str
price: float
quantity: int
class LimitOrderBook:
"""A production-grade limit order book implementation.
Maintains separate red-black trees for bids and asks, with O(log N)
insertion, deletion, and matching operations. Uses price-time priority.
Attributes:
symbol: Trading symbol (e.g., 'INFY')
bid_levels: Dict mapping price -> deque of orders (price descending)
ask_levels: Dict mapping price -> deque of orders (price ascending)
orders: Dict mapping order_id -> Order object
trades: List of executed trades
"""
def __init__(self, symbol: str):
"""Initialize an empty order book.
Args:
symbol: Trading instrument symbol
"""
self.symbol = symbol
self.bid_levels: Dict[float, deque] = defaultdict(deque)
self.ask_levels: Dict[float, deque] = defaultdict(deque)
self.orders: Dict[str, Order] = {}
self.trades: List[Trade] = []
self.trade_counter = 0
def submit_order(self, order: Order) -> List[Trade]:
"""Submit an order to the book and perform matching.
Process:
1. Try to match incoming order with existing orders on opposite side
2. Use best price and time priority
3. Add any unmatched portion to the book
Args:
order: Order object to submit
Returns:
List of Trade objects executed in response to this order
"""
self.orders[order.order_id] = order
trades = []
if order.side == OrderSide.BUY:
trades = self._match_buy_order(order)
else:
trades = self._match_sell_order(order)
# Add remaining quantity to book (if any)
if order.remaining_quantity > 0:
if order.side == OrderSide.BUY:
self.bid_levels[order.price].append(order)
else:
self.ask_levels[order.price].append(order)
return trades
def _match_buy_order(self, buy_order: Order) -> List[Trade]:
"""Match a buy order against the ask side.
Args:
buy_order: The incoming buy order
Returns:
List of trades executed
"""
trades = []
# Sort asks by price (ascending), then by time
while buy_order.remaining_quantity > 0 and self.ask_levels:
best_ask_price = min(self.ask_levels.keys())
# Buy order price must be >= ask price for match
if buy_order.price < best_ask_price:
break
ask_queue = self.ask_levels[best_ask_price]
while ask_queue and buy_order.remaining_quantity > 0:
sell_order = ask_queue[0]
# Execute trade at ask price (price of liquidity provider)
match_quantity = min(
buy_order.remaining_quantity,
sell_order.remaining_quantity
)
trade = Trade(
trade_id=f"T{self.trade_counter}",
timestamp=datetime.utcnow(),
buy_order_id=buy_order.order_id,
sell_order_id=sell_order.order_id,
price=best_ask_price,
quantity=match_quantity
)
buy_order.fill(match_quantity)
sell_order.fill(match_quantity)
trades.append(trade)
self.trades.append(trade)
self.trade_counter += 1
# Remove fully filled sell order from book
if sell_order.remaining_quantity == 0:
ask_queue.popleft()
# Clean up empty price level
if not ask_queue:
del self.ask_levels[best_ask_price]
return trades
def _match_sell_order(self, sell_order: Order) -> List[Trade]:
"""Match a sell order against the bid side.
Args:
sell_order: The incoming sell order
Returns:
List of trades executed
"""
trades = []
# Sort bids by price (descending), then by time
while sell_order.remaining_quantity > 0 and self.bid_levels:
best_bid_price = max(self.bid_levels.keys())
# Sell order price must be <= bid price for match
if sell_order.price > best_bid_price:
break
bid_queue = self.bid_levels[best_bid_price]
while bid_queue and sell_order.remaining_quantity > 0:
buy_order = bid_queue[0]
# Execute trade at bid price (price of liquidity provider)
match_quantity = min(
sell_order.remaining_quantity,
buy_order.remaining_quantity
)
trade = Trade(
trade_id=f"T{self.trade_counter}",
timestamp=datetime.utcnow(),
buy_order_id=buy_order.order_id,
sell_order_id=sell_order.order_id,
price=best_bid_price,
quantity=match_quantity
)
sell_order.fill(match_quantity)
buy_order.fill(match_quantity)
trades.append(trade)
self.trades.append(trade)
self.trade_counter += 1
# Remove fully filled buy order from book
if buy_order.remaining_quantity == 0:
bid_queue.popleft()
# Clean up empty price level
if not bid_queue:
del self.bid_levels[best_bid_price]
return trades
def cancel_order(self, order_id: str) -> bool:
"""Cancel an order in the book.
Args:
order_id: ID of order to cancel
Returns:
True if order was cancelled, False if not found
"""
if order_id not in self.orders:
return False
order = self.orders[order_id]
if order.status == OrderStatus.FILLED:
return False # Cannot cancel filled orders
# Remove from book
if order.side == OrderSide.BUY:
if order.price in self.bid_levels:
self.bid_levels[order.price].remove(order)
if not self.bid_levels[order.price]:
del self.bid_levels[order.price]
else:
if order.price in self.ask_levels:
self.ask_levels[order.price].remove(order)
if not self.ask_levels[order.price]:
del self.ask_levels[order.price]
order.status = OrderStatus.CANCELLED
return True
def get_best_bid_ask(self) -> Tuple[Optional[float], Optional[float]]:
"""Get current best bid and ask prices.
Returns:
Tuple of (best_bid, best_ask), or (None, None) if book is empty
"""
best_bid = max(self.bid_levels.keys()) if self.bid_levels else None
best_ask = min(self.ask_levels.keys()) if self.ask_levels else None
return best_bid, best_ask
def get_spread(self) -> Optional[float]:
"""Get current bid-ask spread.
Returns:
Spread as absolute price difference, or None if no spread exists
"""
best_bid, best_ask = self.get_best_bid_ask()
if best_bid is None or best_ask is None:
return None
return best_ask - best_bid
def get_mid_price(self) -> Optional[float]:
"""Get mid-price (average of best bid and ask).
Returns:
Mid-price or None if book is empty
"""
best_bid, best_ask = self.get_best_bid_ask()
if best_bid is None or best_ask is None:
return None
return (best_bid + best_ask) / 2.0
def get_depth(self, levels: int = 5) -> Tuple[List[Tuple[float, int]],
List[Tuple[float, int]]]:
"""Get market depth (top N price levels).
Args:
levels: Number of price levels to return
Returns:
Tuple of (bid_levels, ask_levels), each as list of (price, quantity)
"""
# Bid side: sort descending by price
bid_prices = sorted(self.bid_levels.keys(), reverse=True)[:levels]
bid_depth = [(p, sum(len(q) for q in [self.bid_levels[p]])* 1)
for p in bid_prices]
# Ask side: sort ascending by price
ask_prices = sorted(self.ask_levels.keys())[:levels]
ask_depth = [(p, sum(len(q) for q in [self.ask_levels[p]])* 1)
for p in ask_prices]
# Calculate actual volume at each level
bid_depth = [
(p, sum(o.remaining_quantity for o in self.bid_levels[p]))
for p in bid_prices
]
ask_depth = [
(p, sum(o.remaining_quantity for o in self.ask_levels[p]))
for p in ask_prices
]
return bid_depth, ask_depth
def get_total_depth(self) -> Tuple[int, int]:
"""Get total volume on bid and ask sides.
Returns:
Tuple of (total_bid_volume, total_ask_volume)
"""
total_bid = sum(
sum(o.remaining_quantity for o in queue)
for queue in self.bid_levels.values()
)
total_ask = sum(
sum(o.remaining_quantity for o in queue)
for queue in self.ask_levels.values()
)
return total_bid, total_ask
# Example Usage
if __name__ == "__main__":
book = LimitOrderBook("INFY")
# Submit some orders
order1 = Order(
order_id="O1",
timestamp=datetime.utcnow(),
side=OrderSide.BUY,
price=1500.0,
quantity=100
)
book.submit_order(order1)
order2 = Order(
order_id="O2",
timestamp=datetime.utcnow(),
side=OrderSide.SELL,
price=1510.0,
quantity=50
)
book.submit_order(order2)
order3 = Order(
order_id="O3",
timestamp=datetime.utcnow(),
side=OrderSide.SELL,
price=1505.0,
quantity=60
)
trades = book.submit_order(order3)
print(f"Best Bid-Ask: {book.get_best_bid_ask()}")
print(f"Spread: {book.get_spread()}")
print(f"Mid Price: {book.get_mid_price()}")
print(f"Trades executed: {len(trades)}")
for trade in trades:
print(f" {trade}")
print(f"Depth: {book.get_depth(3)}")2.1.3 Bid-Ask Spread: The Price of Immediacy#
The bid-ask spread represents the cost of demanding immediate execution rather than waiting for a favorable price. Why does the spread exist?
Microstructure Theory of Spreads
Where:
- = bid-ask spread
- = dealer inventory cost (cost to hold unwanted positions)
- = adverse selection coefficient
- = order size as % of typical volume
Adverse Selection: A dealer doesn't know if an incoming order is from:
- An informed trader (knows secret information) - dealer loses on average
- An uninformed trader (random noise) - dealer breaks even
The wider spread compensates the dealer for the risk of trading with informed traders.
Determinants of Spread on NSE
- Volatility: Higher volatility → wider spread (more inventory risk)
- Volume: High-volume stocks → tighter spreads (faster turnover)
- Price Level: Lower prices → percentage spreads wider but absolute spreads smaller
- Time of Day: Opening 15 min → wider spreads; 11 AM-3 PM → tightest spreads
- Information Events: Before earnings → wider spreads; stable periods → tighter
def estimate_spread_components(
volume_per_minute: float,
returns_volatility: float,
stock_price: float
) -> Dict[str, float]:
"""Estimate bid-ask spread components using simple model.
Args:
volume_per_minute: Average shares traded per minute
returns_volatility: Annualized volatility of returns
stock_price: Current stock price
Returns:
Dict with estimated spread components
"""
# Inventory cost component (higher vol = wider spread)
inventory_cost_component = 0.001 * returns_volatility * stock_price
# Adverse selection component (lower volume = wider spread)
adverse_selection_component = 0.5 / (1.0 + np.log(volume_per_minute))
# Order processing cost (fixed, small)
processing_cost = 0.1 # In rupees
total_spread = 2 * (
inventory_cost_component +
adverse_selection_component +
processing_cost
)
return {
"inventory_cost": inventory_cost_component,
"adverse_selection": adverse_selection_component,
"processing_cost": processing_cost,
"total_spread": total_spread
}
# Example: INFY with 1000 shares/min volume, 20% vol, trading at Rs 1500
spread_est = estimate_spread_components(
volume_per_minute=1000,
returns_volatility=0.20,
stock_price=1500.0
)
print(f"Estimated spread: Rs {spread_est['total_spread']:.2f}")2.1.4 Market Depth and Liquidity#
Liquidity is the ability to buy/sell large quantities quickly without large price movements.
Key Liquidity Metrics
@dataclass
class LiquidityMetrics:
"""Quantitative measures of market liquidity."""
bid_ask_spread: float # Tightness
spread_percentage: float # Spread as % of mid-price
depth_10_levels: int # Volume at top 10 bid/ask levels
cumulative_depth_1pct: int # Volume to move price 1%
@staticmethod
def calculate(book: LimitOrderBook, stock_price: float) -> 'LiquidityMetrics':
"""Calculate liquidity metrics from order book.
Args:
book: LimitOrderBook instance
stock_price: Current stock price for percentage calculations
Returns:
LiquidityMetrics object
"""
spread = book.get_spread()
mid_price = book.get_mid_price()
bid_depth, ask_depth = book.get_depth(10)
# Sum volumes at top 10 levels
total_depth = sum(vol for price, vol in bid_depth) + \
sum(vol for price, vol in ask_depth)
# Cumulative volume to move price by 1%
target_volume = stock_price * 0.01 # 1% move
cumulative = 0
cumulative_depth_1pct = 0
# Sum from best ask upward
for price, volume in ask_depth:
cumulative += volume
cumulative_depth_1pct = volume
if cumulative >= target_volume:
break
return LiquidityMetrics(
bid_ask_spread=spread,
spread_percentage=100.0 * spread / mid_price,
depth_10_levels=total_depth,
cumulative_depth_1pct=cumulative_depth_1pct
)
# Example
metrics = LiquidityMetrics.calculate(book, stock_price=1500.0)
print(f"Spread: Rs {metrics.bid_ask_spread:.2f} ({metrics.spread_percentage:.2f}%)")
print(f"Depth (10 levels): {metrics.depth_10_levels} shares")2.1.5 Market Data Levels#
Level 1 Data (Basic, all brokers provide):
- Best bid price and volume
- Best ask price and volume
- Last traded price and volume
- Timestamp
Level 2 Data (Market depth, Zerodha provides via streaming):
- Top 5-20 bid levels with volumes
- Top 5-20 ask levels with volumes
- Real-time updates as book changes
Level 3 Data (Full book, research/exchanges only):
- All bids and asks at all price levels
- Individual order information
- Order entry/exit/modification tracking
@dataclass
class Level1Data:
"""Level 1 market data."""
timestamp: datetime
symbol: str
best_bid: float
best_bid_volume: int
best_ask: float
best_ask_volume: int
last_trade_price: float
last_trade_volume: int
@dataclass
class Level2Data:
"""Level 2 market data with market depth."""
timestamp: datetime
symbol: str
bid_levels: List[Tuple[float, int]] # (price, volume) sorted desc
ask_levels: List[Tuple[float, int]] # (price, volume) sorted asc
last_trade_price: float
last_trade_volume: int
def order_book_to_level2(book: LimitOrderBook) -> Level2Data:
"""Convert order book to Level 2 data format.
Args:
book: LimitOrderBook instance
Returns:
Level2Data object
"""
bid_depth, ask_depth = book.get_depth(levels=20)
return Level2Data(
timestamp=datetime.utcnow(),
symbol=book.symbol,
bid_levels=bid_depth,
ask_levels=ask_depth,
last_trade_price=book.get_mid_price() or 0.0,
last_trade_volume=0
)Key Takeaways#
- Order Book Structure: Buy orders (bids) on left, sell orders (asks) on right, sorted by price-time priority
- Bid-Ask Spread: Compensation to market makers for inventory risk and adverse selection
- Liquidity: Multidimensional concept—spread, depth, and volume matter
- Market Data Hierarchy: L1 (best bid/ask) → L2 (top N levels) → L3 (full book)
- Implementation: Order books require careful handling of order queues and rapid price updates
Exercises#
Exercise 2.1.1: Simulate a sequence of 10 random buy/sell orders to your LimitOrderBook and track:
- Number of trades executed
- Average execution price vs order prices
- Final book state
Exercise 2.1.2: Implement a function get_fill_probability(order_book, order) that estimates the probability an incoming limit order will execute within 1 second based on current book depth.
Exercise 2.1.3: Download 1 day of Level 2 data for INFY from Zerodha and plot:
- Bid-ask spread evolution through the day
- Best bid/ask prices
- Total depth (sum of bid + ask volumes)
Module 2.2: Order Types and Execution
Learning Objectives#
- Understand mechanics of different order types (market, limit, stop, stop-limit, bracket, iceberg)
- Implement order matching engine with multiple order types
- Analyze execution certainty vs price improvement tradeoff
- Study NSE matching engine rules and Zerodha order routing
Content#
2.2.1 Order Types and Execution Mechanics#
Market Orders
A market order is a request to buy/sell immediately at the current best available price.
Matching Algorithm:
- Sort opposite-side orders by price (best first) then time
- Execute against orders sequentially until quantity filled
- If entire market order not filled, partial fill occurs
Advantages:
- Execution certainty (guaranteed to fill some quantity)
- Immediate execution
- No monitoring needed
Disadvantages:
- Price uncertainty (may pay worse than current best)
- Slippage on large orders
- "Market impact" - moves price against you
@dataclass
class MarketOrder:
"""A market order that executes immediately at best prices."""
order_id: str
timestamp: datetime
side: OrderSide
quantity: int # No price field - executes at market
def __str__(self):
return f"MARKET {self.side.value} {self.quantity} shares"
def execute_market_order(
book: LimitOrderBook,
order: MarketOrder
) -> Tuple[List[Trade], Optional[float]]:
"""Execute a market order immediately.
Args:
book: The limit order book
order: Market order to execute
Returns:
Tuple of (executed_trades, average_execution_price)
"""
trades = []
total_quantity = 0
total_cost = 0.0
if order.side == OrderSide.BUY:
# Execute against ask side (sellers)
while order.quantity > 0 and book.ask_levels:
best_ask = min(book.ask_levels.keys())
ask_queue = book.ask_levels[best_ask]
while ask_queue and order.quantity > 0:
seller = ask_queue[0]
exec_qty = min(order.quantity, seller.remaining_quantity)
trade = Trade(
trade_id=f"T{book.trade_counter}",
timestamp=datetime.utcnow(),
buy_order_id=order.order_id,
sell_order_id=seller.order_id,
price=best_ask,
quantity=exec_qty
)
seller.fill(exec_qty)
order.quantity -= exec_qty
total_quantity += exec_qty
total_cost += best_ask * exec_qty
trades.append(trade)
book.trades.append(trade)
book.trade_counter += 1
if seller.remaining_quantity == 0:
ask_queue.popleft()
if not ask_queue:
del book.ask_levels[best_ask]
else: # SELL
# Execute against bid side (buyers)
while order.quantity > 0 and book.bid_levels:
best_bid = max(book.bid_levels.keys())
bid_queue = book.bid_levels[best_bid]
while bid_queue and order.quantity > 0:
buyer = bid_queue[0]
exec_qty = min(order.quantity, buyer.remaining_quantity)
trade = Trade(
trade_id=f"T{book.trade_counter}",
timestamp=datetime.utcnow(),
buy_order_id=buyer.order_id,
sell_order_id=order.order_id,
price=best_bid,
quantity=exec_qty
)
buyer.fill(exec_qty)
order.quantity -= exec_qty
total_quantity += exec_qty
total_cost += best_bid * exec_qty
trades.append(trade)
book.trades.append(trade)
book.trade_counter += 1
if buyer.remaining_quantity == 0:
bid_queue.popleft()
if not bid_queue:
del book.bid_levels[best_bid]
avg_price = total_cost / total_quantity if total_quantity > 0 else None
return trades, avg_price
# Example
market_buy = MarketOrder(
order_id="MKT1",
timestamp=datetime.utcnow(),
side=OrderSide.BUY,
quantity=150
)
trades, avg_price = execute_market_order(book, market_buy)
print(f"Market order filled {len(trades)} trades, avg price: {avg_price:.2f}")Limit Orders
A limit order specifies a price and waits until that price becomes available (or is cancelled).
Advantages:
- Price certainty (won't pay worse than limit price)
- May get better price if market moves favorably
- No market impact if not executed
Disadvantages:
- No execution guarantee (may never fill)
- Monitoring required
- May miss opportunity if price passes by quickly
Price-Time Priority: At same price, earlier orders execute first.
Stop Orders
A stop order becomes a market order once the stock reaches a specified "stop price".
- Stop-Loss: Used to limit losses; triggers when price falls below stop
- Stop-Buy: Used to enter long position; triggers when price rises above stop
Risk: On NSE, when stop triggers, becomes market order → may get bad execution during volatile moves.
@dataclass
class StopOrder:
"""A stop order that converts to market order at stop price."""
order_id: str
timestamp: datetime
side: OrderSide
quantity: int
stop_price: float # Trigger price
is_active: bool = False # Becomes True when stop triggered
def check_trigger(self, market_price: float) -> bool:
"""Check if stop condition is met.
Args:
market_price: Current market price
Returns:
True if stop should trigger
"""
if self.is_active:
return False # Already triggered
if self.side == OrderSide.BUY:
# Buy stop triggers when price rises above stop_price
return market_price >= self.stop_price
else:
# Sell stop triggers when price falls below stop_price
return market_price <= self.stop_priceStop-Limit Orders
Combines stop and limit: when stop triggers, becomes a limit order at specified price.
- Advantages: Limits execution price when stop triggers
- Disadvantages: After trigger, if price passes limit price, may not execute
@dataclass
class StopLimitOrder:
"""Stop order that converts to limit order (not market)."""
order_id: str
timestamp: datetime
side: OrderSide
quantity: int
stop_price: float # Trigger price
limit_price: float # Limit price after trigger
is_active: bool = FalseBracket Orders
A bracket order is a primary order with automatic stop-loss and take-profit exits.
@dataclass
class BracketOrder:
"""Primary order with automatic stop-loss and take-profit exits.
Example: Buy 100 shares at 1500
- Stop-loss at 1480 (max loss Rs 2000)
- Take-profit at 1530 (max gain Rs 3000)
When primary fills, both exits become active.
When either exit fills, the other is cancelled.
"""
order_id: str
timestamp: datetime
side: OrderSide
primary_quantity: int
primary_price: float
stop_loss_price: float
take_profit_price: float
primary_filled: bool = False
exit_order_id: Optional[str] = NoneIceberg Orders
An iceberg order displays only a portion of its total quantity (the "visible" part), hiding the rest.
- Visible Quantity: 100 shares shown on book
- Total Quantity: 10,000 shares (hidden)
- As each visible batch executes, next batch appears
Why iceberg?: Avoid revealing large order intentions to avoid market impact.
@dataclass
class IcebergOrder:
"""An order that shows only a fraction of total quantity.
Attributes:
total_quantity: Total shares to trade
visible_quantity: Quantity shown on book
remaining_visible: Unexecuted visible quantity
"""
order_id: str
timestamp: datetime
side: OrderSide
price: float
total_quantity: int
visible_quantity: int
remaining_total: int = field(default_factory=lambda: 0)
remaining_visible: int = field(default_factory=lambda: 0)
def __post_init__(self):
self.remaining_total = self.total_quantity
self.remaining_visible = self.visible_quantity
def refill_visible(self):
"""Refill visible quantity as it executes."""
if self.remaining_total > 0:
self.remaining_visible = min(
self.visible_quantity,
self.remaining_total
)2.2.2 Execution Certainty vs Price Improvement Tradeoff#
class ExecutionAnalysis:
"""Analyze execution quality tradeoff."""
@staticmethod
def analyze_order_type(
book: LimitOrderBook,
order_size: int,
mid_price: float
) -> Dict[str, float]:
"""Compare execution quality across order types.
Args:
book: Current order book
order_size: Shares to buy
mid_price: Current mid-price
Returns:
Dict with execution metrics for different order types
"""
_, ask_depth = book.get_depth(levels=20)
# Market order execution
cumulative_qty = 0
cumulative_cost = 0.0
for price, qty in ask_depth:
fill_qty = min(order_size - cumulative_qty, qty)
cumulative_cost += price * fill_qty
cumulative_qty += fill_qty
if cumulative_qty >= order_size:
break
market_price = cumulative_cost / order_size if cumulative_qty >= order_size else None
market_slippage = market_price - mid_price if market_price else 0.0
# Limit order execution (best case: best ask)
best_ask, _ = book.get_best_bid_ask()
limit_slippage = best_ask - mid_price if best_ask else 0.0
return {
"market_order_price": market_price,
"market_order_slippage": market_slippage,
"market_order_slippage_bps": 10000 * market_slippage / mid_price,
"limit_order_best_case": best_ask,
"limit_order_best_case_slippage": limit_slippage,
"certainty_cost_difference_bps": 10000 * market_slippage / mid_price
}2.2.3 NSE Matching Engine Rules#
NSE (National Stock Exchange) Matching Algorithm:
- Price-Time Priority: Highest bid / lowest ask has priority. Same price: FIFO
- Order Book Maintenance: Bids sorted descending, asks sorted ascending
- Automatic Execution: Market orders execute immediately against available liquidity
- No Order Types: NSE only supports market and limit orders (no stop orders natively)
- Settlement: T+1 (trades settle next business day)
Zerodha Integration:
Zerodha's Kite platform provides:
- Market and limit orders (basic)
- Stop-loss and stop-limit orders (synthetic, managed client-side)
- Bracket orders (synthetic with OCO - One-Cancels-Other logic)
class ZerodhaOrderRouter:
"""Simulates Zerodha order routing and execution."""
def __init__(self, book: LimitOrderBook):
self.book = book
def place_market_order(
self,
symbol: str,
side: OrderSide,
quantity: int
) -> Dict:
"""Place market order via Zerodha."""
order = MarketOrder(
order_id=f"Z{datetime.utcnow().timestamp()}",
timestamp=datetime.utcnow(),
side=side,
quantity=quantity
)
trades, avg_price = execute_market_order(self.book, order)
return {
"order_id": order.order_id,
"status": "COMPLETE" if len(trades) > 0 else "REJECTED",
"filled_quantity": sum(t.quantity for t in trades),
"average_price": avg_price,
"trades": trades
}
def place_limit_order(
self,
symbol: str,
side: OrderSide,
quantity: int,
price: float
) -> Dict:
"""Place limit order via Zerodha."""
order = Order(
order_id=f"Z{datetime.utcnow().timestamp()}",
timestamp=datetime.utcnow(),
side=side,
price=price,
quantity=quantity
)
trades = self.book.submit_order(order)
return {
"order_id": order.order_id,
"status": "PENDING" if order.remaining_quantity > 0 else "COMPLETE",
"filled_quantity": order.filled_quantity,
"remaining_quantity": order.remaining_quantity,
"trades": trades
}Key Takeaways#
- Market Orders: Immediate execution, price uncertainty, high slippage risk
- Limit Orders: Price certainty, execution risk, time monitoring
- Stop Orders: Trigger market order at price level; risk of bad fills during volatility
- Bracket/Iceberg: Risk management and stealth mechanisms
- NSE + Zerodha: Only market/limit natively; stop orders synthetic (client-managed)
Exercises#
Exercise 2.2.1: Compare execution prices for a 500-share market buy order vs best-case limit order scenario across different book depth scenarios.
Exercise 2.2.2: Implement StopOrderManager that monitors a list of stop orders against live market prices and triggers appropriate market/limit orders.
Exercise 2.2.3: Calculate the "hidden depth" in an iceberg order: if you see 100 shares at 1500, but know it's iceberg with 1000 total, what's the expected cost to move 200-share order?
Module 2.3: Market Makers and Liquidity
Learning Objectives#
- Understand market maker economics and adverse selection
- Apply Kyle's model of informed and uninformed trading
- Calculate liquidity measures (spread, depth, Amihud ratio)
- Use market microstructure insights in trading strategy
Content#
2.3.1 Market Maker Role and Economics#
A market maker (MM) is a dealer who:
- Continuously posts bids and asks (provides liquidity)
- Profits from the bid-ask spread
- Absorbs temporary order imbalances
- Bears inventory risk
Bid-Ask Spread Decomposition:
Where:
- = inventory cost component (compensation for holding unwanted position)
- = adverse selection component (compensation for trading with informed traders)
Market Maker Economics:
Revenue per round-trip trade:
With adverse selection, MM loses to informed traders and profits from uninformed:
Where = probability of uninformed trader
2.3.2 Adverse Selection and Kyle's Model#
Kyle's Model (1985) elegantly models the microstructure of informed vs uninformed trading:
Setup:
- One informed trader knows true value
- Many uninformed (noise) traders create random order flow
- One market maker sets bid-ask based on observed order flow, but doesn't know who's informed
Model Equations:
Market maker observes total order flow:
MM's best estimate of true value:
Where = market impact coefficient (lower when more noise, higher when more informed)
Optimal bid-ask:
Key Insight: Spread widens when:
- Order flow variance increases (harder to distinguish informed from noise)
- Market impact coefficient increases (less noise relative to informed)
- True value uncertainty increases
@dataclass
class KylesModel:
"""Kyle's model of informed vs uninformed trading."""
true_value: float # True fundamental value
initial_spread: float # Initial bid-ask spread
noise_trader_variance: float # Variance of uninformed order flow
informed_trader_quantity: int # Shares the informed trader wants to buy/sell
def calculate_market_impact(self) -> Dict[str, float]:
"""Calculate market impact using Kyle's model.
Returns:
Dict with market impact metrics
"""
# Simplified Kyle's model
# Market impact proportional to order size / noise variance
lamda = (
self.informed_trader_quantity /
np.sqrt(self.noise_trader_variance)
)
# Optimal spread (simplified)
optimal_spread = 2 * lamda * np.sqrt(self.noise_trader_variance)
# Market maker's expected loss
mm_loss = lamda * self.informed_trader_quantity
return {
"market_impact_coefficient": lamda,
"optimal_spread": optimal_spread,
"mm_expected_loss": mm_loss,
"informed_trader_profit": mm_loss
}
def simulate_trading_session(
self,
num_rounds: int = 10
) -> Tuple[List[float], List[float]]:
"""Simulate trading session with informed and uninformed orders.
Args:
num_rounds: Number of trading rounds
Returns:
Tuple of (prices, informed_positions)
"""
prices = [self.true_value]
informed_position = 0
lamda = (
self.informed_trader_quantity /
np.sqrt(self.noise_trader_variance)
)
for round_num in range(num_rounds):
# Informed trader's order (unobserved by MM)
informed_order = self.informed_trader_quantity
# Noise traders' orders
noise_order = np.random.normal(0, self.noise_trader_variance)
# Total order flow
total_flow = informed_order + noise_order
# Market maker updates price based on order flow
price_update = lamda * total_flow
new_price = prices[-1] + price_update
prices.append(new_price)
informed_position += informed_order
return prices, [self.informed_trader_quantity * i for i in range(num_rounds + 1)]
# Example
kyle_model = KylesModel(
true_value=1500.0,
initial_spread=10.0,
noise_trader_variance=1000.0,
informed_trader_quantity=100
)
impact = kyle_model.calculate_market_impact()
print(f"Market impact coefficient: {impact['market_impact_coefficient']:.4f}")
print(f"Optimal spread: Rs {impact['optimal_spread']:.2f}")
prices, positions = kyle_model.simulate_trading_session(num_rounds=10)2.3.3 Liquidity Metrics and Measurement#
Bid-Ask Spread
Absolute spread:
Relative spread (as % of mid-price):
In basis points:
Depth
Best ask depth: Volume available at best ask price Top-N depth: Sum of volumes at top N price levels
def calculate_depth_metrics(book: LimitOrderBook) -> Dict[str, any]:
"""Calculate comprehensive depth metrics."""
bid_depth, ask_depth = book.get_depth(levels=10)
# Best prices
best_ask_depth = ask_depth[0][1] if ask_depth else 0
best_bid_depth = bid_depth[0][1] if bid_depth else 0
# Cumulative depth
top5_depth = (
sum(vol for _, vol in bid_depth[:5]) +
sum(vol for _, vol in ask_depth[:5])
)
top10_depth = (
sum(vol for _, vol in bid_depth) +
sum(vol for _, vol in ask_depth)
)
return {
"best_ask_depth": best_ask_depth,
"best_bid_depth": best_bid_depth,
"top5_total_depth": top5_depth,
"top10_total_depth": top10_depth
}Amihud Illiquidity Ratio
The Amihud ratio measures the price impact of trading volume:
Where:
- = Absolute return during period
- = Trading volume in rupees during period
High ratio = illiquid (small volume causes large price moves)
def calculate_amihud_ratio(
returns: np.ndarray,
volumes: np.ndarray
) -> float:
"""Calculate Amihud illiquidity ratio.
Args:
returns: Array of returns (one per period)
volumes: Array of volumes in rupees (one per period)
Returns:
Amihud illiquidity ratio (higher = less liquid)
"""
abs_returns = np.abs(returns)
# Filter out zero-volume periods
valid = volumes > 0
if not np.any(valid):
return np.inf
illiq_periods = abs_returns[valid] / volumes[valid]
amihud = np.mean(illiq_periods)
return amihud
# Example: INFY over 30 days
np.random.seed(42)
daily_returns = np.random.normal(0.001, 0.02, 30)
daily_volumes = np.random.uniform(500_000_000, 2_000_000_000, 30) # In rupees
amihud = calculate_amihud_ratio(daily_returns, daily_volumes)
print(f"Amihud illiquidity ratio: {amihud:.6f}")
# For comparison
# NSE Liquid stocks: ~0.0001 to 0.001
# NSE Illiquid stocks: ~0.01 to 0.12.3.4 Liquidity Dynamics Through the Day#
On NSE, liquidity varies significantly:
class IntradarLiquidityProfile:
"""Model how liquidity evolves through NSE trading day."""
@staticmethod
def nse_liquidity_schedule(
hour_of_day: float
) -> float:
"""Get relative liquidity multiplier for NSE trading hour.
NSE trading hours: 9:15 AM to 3:30 PM (6 hours 15 min)
Args:
hour_of_day: 9.25 = 9:15 AM, 15.5 = 3:30 PM
Returns:
Liquidity multiplier (1.0 = average, >1 = above average)
"""
# Poor liquidity: opening 15 mins and last 30 mins
if 9.15 <= hour_of_day < 9.5: # Opening 21 minutes
return 0.3
elif 15.0 < hour_of_day <= 15.5: # Closing 30 minutes
return 0.4
# Fair liquidity: before lunch
elif 9.5 <= hour_of_day < 12.0:
return 0.7
# Lunch dip: 12:00 to 13:30
elif 12.0 <= hour_of_day < 13.5:
return 0.5
# Best liquidity: post-lunch to 3 PM
elif 13.5 <= hour_of_day <= 15.0:
return 1.2
else:
return 1.0
@staticmethod
def estimate_spread_by_time(
base_spread: float,
hour_of_day: float
) -> float:
"""Estimate bid-ask spread for given time of day.
Args:
base_spread: Typical spread in rupees
hour_of_day: Hour (24-hour format)
Returns:
Estimated spread
"""
liquidity_mult = IntradarLiquidityProfile.nse_liquidity_schedule(hour_of_day)
# Higher liquidity multiplier = lower spread
return base_spread / (liquidity_mult + 0.2)Key Takeaways#
- Market Makers: Profit from spreads, lose to informed traders
- Adverse Selection: Wider spreads when it's harder to distinguish informed from noise traders
- Kyle's Model: Fundamental model of microstructure with specific predictions about spreads and impact
- Liquidity Metrics: Spread, depth, and Amihud ratio all provide different perspectives
- Intraday Patterns: NSE liquidity peaks mid-session, worst at open and close
Exercises#
Exercise 2.3.1: Calculate Amihud ratio for INFY over last 30 days using actual NSE data. Compare to TCS, Reliance (tier-1 stocks).
Exercise 2.3.2: Using Kyle's model, calculate how informed traders should adjust order size given estimated noise variance to minimize market impact.
Exercise 2.3.3: Implement dynamic spread estimation based on time-of-day that matches observed NSE patterns.
Module 2.4: Price Formation and Market Impact
Learning Objectives#
- Understand how prices form through trading
- Distinguish temporary vs permanent market impact
- Apply square-root market impact model
- Implement market impact simulator
- Calculate expected costs of trades
Content#
2.4.1 How Prices Move: Permanent vs Temporary Impact#
When you submit a market order, the price typically moves in two ways:
Temporary Impact: The spread widening and immediate adverse movement
- Caused by market maker inventory adjustment
- Reverses within seconds to minutes as liquidity providers arrive
- Your execution gets bad price, but price reverts to prior level
Permanent Impact: Fundamental repricing based on new information
- Caused by market learning from your order
- Suggests your order contains information about true value
- Price permanently shifts in direction of your trade
Spread Bounce (Technical): Price bounces between bid and ask
- When you buy at ask, then sell at bid minutes later
- Looks like price didn't move, but you captured the bounce
@dataclass
class ImpactAnalysis:
"""Analyze temporary vs permanent market impact."""
order_size: int
market_price_before: float
execution_price: float
market_price_10sec: float # Price 10 seconds after execution
market_price_10min: float # Price 10 minutes after execution
@property
def temporary_impact(self) -> float:
"""Immediate impact from execution."""
return self.execution_price - self.market_price_before
@property
def permanent_impact(self) -> float:
"""Impact remaining after market adjusts."""
return self.market_price_10min - self.market_price_before
@property
def transient_impact(self) -> float:
"""Impact that reverses."""
return self.temporary_impact - self.permanent_impact
def impact_in_basis_points(self) -> Dict[str, float]:
"""Express impacts as basis points."""
basis = 10000 / self.market_price_before
return {
"temporary_bps": self.temporary_impact * basis,
"permanent_bps": self.permanent_impact * basis,
"transient_bps": self.transient_impact * basis
}
# Example
impact = ImpactAnalysis(
order_size=1000,
market_price_before=1500.0,
execution_price=1502.5, # Bought at 2.5 rupee disadvantage
market_price_10sec=1502.0, # Recovered slightly
market_price_10min=1501.5 # Permanent move up 1.5
)
print(f"Temporary impact: Rs {impact.temporary_impact:.2f}")
print(f"Permanent impact: Rs {impact.permanent_impact:.2f}")
print(f"Transient impact: Rs {impact.transient_impact:.2f}")2.4.2 Square-Root Market Impact Model#
The Almgren-Chriss model is widely used for predicting market impact:
Where:
- = Price impact (absolute, in basis points)
- = Market impact parameter (model constant)
- = Stock volatility (annualized)
- = Volume as % of daily volume
- = Time duration to execute (in days)
- The square-root relationship is empirically validated
Why square-root?
- Large orders have proportionally higher impact
- But spreading order over time reduces impact
- The relationship is sublinear (doubling size doesn't double impact)
def almgren_chriss_impact(
order_quantity: int,
stock_price: float,
daily_volume: int,
daily_volatility: float,
execution_time_minutes: int,
lambda_param: float = 0.5
) -> Dict[str, float]:
"""Calculate market impact using Almgren-Chriss model.
Args:
order_quantity: Shares to trade
stock_price: Current stock price
daily_volume: Typical daily volume in shares
daily_volatility: Daily volatility (as decimal)
execution_time_minutes: Time to execute order (minutes)
lambda_param: Impact parameter (0.5 typical for stocks)
Returns:
Dict with impact calculations
"""
# Volume as % of daily volume
volume_ratio = order_quantity / daily_volume
# Time in days
execution_days = execution_time_minutes / (60 * 6.5) # 6.5 hour trading day
# Permanent impact (bps)
permanent_impact_bps = (
lambda_param *
(daily_volatility * 100) *
np.sqrt(volume_ratio) *
np.sqrt(execution_days) *
10000 / stock_price
)
# Temporary impact (spread plus additional)
temporary_impact_bps = permanent_impact_bps * 0.5 # Typical 50% of permanent
# Cost in rupees
order_value = order_quantity * stock_price
permanent_cost = order_value * permanent_impact_bps / 10000
temporary_cost = order_value * temporary_impact_bps / 10000
total_cost = permanent_cost + temporary_cost
return {
"permanent_impact_bps": permanent_impact_bps,
"temporary_impact_bps": temporary_impact_bps,
"total_impact_bps": permanent_impact_bps + temporary_impact_bps,
"permanent_cost_rupees": permanent_cost,
"temporary_cost_rupees": temporary_cost,
"total_cost_rupees": total_cost
}
# Example: INFY trade
# Assumptions:
# - Buy 10,000 shares at Rs 1500
# - INFY daily volume: ~1 million shares
# - Volatility: 20% annual
# - Execution time: 5 minutes
impact = almgren_chriss_impact(
order_quantity=10_000,
stock_price=1500.0,
daily_volume=1_000_000,
daily_volatility=0.20,
execution_time_minutes=5
)
print(f"Total impact: {impact['total_impact_bps']:.1f} bps")
print(f"Total cost: Rs {impact['total_cost_rupees']:.0f}")
print(f"As % of order value: {100 * impact['total_cost_rupees'] / (10_000 * 1500):.2f}%")2.4.3 Why Your Trades Move Market Against You#
Several mechanisms cause your trades to move prices adversely:
1. Inventory Adjustment: MM needs to unwind unwanted position 2. Adverse Selection: MM assumes your trade is informed 3. Information Leakage: If order is large/persistent, market learns 4. Liquidity Depth: Limited sellers at good prices forces acceptance of worse prices 5. Volatility Impact: Temporary volatility spike from uncertainty
class MarketImpactSimulator:
"""Simulate how trades move market prices."""
def __init__(
self,
initial_price: float,
initial_spread: float,
daily_volume: int,
volatility: float
):
self.initial_price = initial_price
self.initial_spread = initial_spread
self.daily_volume = daily_volume
self.volatility = volatility
self.price = initial_price
self.spread = initial_spread
def simulate_trade_impact(
self,
order_quantity: int,
order_side: str,
execution_duration_minutes: int = 5
) -> Dict[str, any]:
"""Simulate price movement from a trade.
Args:
order_quantity: Shares to trade
order_side: "BUY" or "SELL"
execution_duration_minutes: How long to execute
Returns:
Dict with price progression and impact
"""
volume_ratio = order_quantity / self.daily_volume
# Impact coefficient
impact_coeff = self.volatility * np.sqrt(volume_ratio)
# Immediate execution impact
immediate_impact = impact_coeff * self.initial_price * 0.01 # In rupees
if order_side == "BUY":
execution_price = self.initial_price + immediate_impact + self.initial_spread / 2
price_after_1min = self.initial_price + immediate_impact * 0.7
price_after_5min = self.initial_price + immediate_impact * 0.5
price_after_1hour = self.initial_price + immediate_impact * 0.3
else: # SELL
execution_price = self.initial_price - immediate_impact - self.initial_spread / 2
price_after_1min = self.initial_price - immediate_impact * 0.7
price_after_5min = self.initial_price - immediate_impact * 0.5
price_after_1hour = self.initial_price - immediate_impact * 0.3
return {
"order_side": order_side,
"order_quantity": order_quantity,
"initial_price": self.initial_price,
"execution_price": execution_price,
"execution_slippage": abs(execution_price - self.initial_price),
"price_1min_after": price_after_1min,
"price_5min_after": price_after_5min,
"price_1hour_after": price_after_1hour,
"temporary_impact": abs(execution_price - price_after_1min),
"permanent_impact": abs(execution_price - price_after_1hour),
}
# Example
sim = MarketImpactSimulator(
initial_price=1500.0,
initial_spread=10.0,
daily_volume=1_000_000,
volatility=0.20
)
impact = sim.simulate_trade_impact(
order_quantity=50_000,
order_side="BUY",
execution_duration_minutes=5
)
print(f"Execution slippage: Rs {impact['execution_slippage']:.2f}")
print(f"Temporary impact: Rs {impact['temporary_impact']:.2f}")
print(f"Permanent impact: Rs {impact['permanent_impact']:.2f}")2.4.4 Execution Strategy Optimization#
Optimal execution strategies balance two costs:
- Market Impact Cost: Larger trades have worse prices
- Opportunity Cost: Delaying trades risks unfavorable price movement
def optimal_execution_strategy(
total_quantity: int,
target_execution_minutes: int,
stock_price: float,
daily_volume: int,
volatility: float,
expected_price_move: float = 0.0
) -> Dict[str, any]:
"""Calculate optimal order slicing strategy.
Solve optimization: minimize market impact + opportunity cost
Args:
total_quantity: Total shares to trade
target_execution_minutes: Time window to trade
stock_price: Current price
daily_volume: Daily volume
volatility: Volatility
expected_price_move: Expected price change (bps)
Returns:
Dict with optimal strategy
"""
num_slices = 10 # Split into 10 orders
slice_quantity = total_quantity / num_slices
slice_duration = target_execution_minutes / num_slices
total_cost = 0.0
for i in range(num_slices):
# Market impact for this slice
impact = almgren_chriss_impact(
order_quantity=int(slice_quantity),
stock_price=stock_price,
daily_volume=daily_volume,
daily_volatility=volatility,
execution_time_minutes=int(slice_duration)
)
total_cost += impact["total_cost_rupees"]
# Opportunity cost (if price moves adversely)
opportunity_cost = total_quantity * stock_price * abs(expected_price_move) / 10000
return {
"strategy": "Slice into 10 orders",
"market_impact_cost": total_cost,
"opportunity_cost": opportunity_cost,
"total_expected_cost": total_cost + opportunity_cost,
"cost_as_bps": 10000 * (total_cost + opportunity_cost) / (total_quantity * stock_price)
}Key Takeaways#
- Temporary Impact: Market maker spread and adjustment (reverses quickly)
- Permanent Impact: Information content of your trade (doesn't reverse)
- Square-Root Relationship: Impact grows with sqrt(volume_ratio) and sqrt(time)
- Slicing Orders: Breaking large trades reduces market impact
- Almgren-Chriss: Industry-standard model for impact prediction
Exercises#
Exercise 2.4.1: Calculate market impact for 5%, 10%, and 20% of daily volume for a high-liquidity NSE stock. How does impact scale?
Exercise 2.4.2: Implement a VWAP (Volume-Weighted Average Price) execution algorithm that slices orders based on expected intraday volume distribution.
Exercise 2.4.3: Analyze empirical market impact on NSE: collect 100 trades of different sizes and measure execution price vs market price before/after.
Module 2.5: Trading Sessions and Market Mechanics
Learning Objectives#
- Understand NSE trading sessions and their mechanics
- Learn settlement, corporate actions, and circuit breakers
- Calculate complete brokerage and charges for Zerodha
- Implement production-ready trade cost calculation
Content#
2.5.1 NSE Trading Sessions#
NSE operates in clearly defined phases:
Pre-Open Session (9:00 AM - 9:15 AM IST)
- Purpose: Allows traders to accumulate orders before open
- Order Collection Phase (9:00-9:08): Submit market/limit orders
- Buffer Period (9:08-9:12): No new orders accepted
- Call Auction (9:12-9:15): Matching occurs at opening price
- Opening Price: Determined by supply-demand matching (maximizes volume)
Example:
At 9:14 AM:
Buy Orders: 1500 @ 1500, 500 @ 1499, 1000 @ 1498
Sell Orders: 1200 @ 1501, 800 @ 1502, 500 @ 1503
Opening price determined: Rs 1500 (clears most volume)
Opening trades: 1500 shares @ 1500class PreOpenAuction:
"""Simulate NSE pre-open auction."""
def __init__(self):
self.buy_orders = [] # (quantity, price)
self.sell_orders = [] # (quantity, price)
def add_buy_order(self, quantity: int, price: float):
"""Add buy order during pre-open."""
self.buy_orders.append((quantity, price))
self.buy_orders.sort(key=lambda x: -x[1]) # Sort descending by price
def add_sell_order(self, quantity: int, price: float):
"""Add sell order during pre-open."""
self.sell_orders.append((quantity, price))
self.sell_orders.sort(key=lambda x: x[1]) # Sort ascending by price
def calculate_opening_price(self) -> Tuple[float, int]:
"""Calculate opening price by maximizing matched volume.
Returns:
Tuple of (opening_price, matched_volume)
"""
if not self.buy_orders or not self.sell_orders:
return None, 0
# Try prices in range of buy/sell order prices
prices_to_try = sorted(set(
[p for _, p in self.buy_orders] +
[p for _, p in self.sell_orders]
))
best_price = None
best_volume = 0
for test_price in prices_to_try:
# Calculate volume matched at this price
buy_quantity = sum(q for q, p in self.buy_orders if p >= test_price)
sell_quantity = sum(q for q, p in self.sell_orders if p <= test_price)
matched_volume = min(buy_quantity, sell_quantity)
if matched_volume > best_volume:
best_volume = matched_volume
best_price = test_price
return best_price, best_volume
# Example
auction = PreOpenAuction()
auction.add_buy_order(1000, 1500)
auction.add_buy_order(500, 1499)
auction.add_buy_order(800, 1498)
auction.add_sell_order(1200, 1501)
auction.add_sell_order(600, 1502)
opening_price, volume = auction.calculate_opening_price()
print(f"Opening price: Rs {opening_price}, Volume: {volume} shares")Continuous Trading Session (9:15 AM - 3:30 PM IST)
- Regular Hours: Standard price-time priority matching
- No Circuit Breaker Checks: Orders execute normally
- Best Execution: Market and limit orders interact freely
- Auction Mechanism: None (matching continuous)
Closing Auction (3:25 PM - 3:30 PM IST)
Similar to pre-open:
- Collect orders (no new orders after 3:30)
- Calculate closing price to maximize volume
- NSE uses this to prevent manipulation at close
class ClosingAuction(PreOpenAuction):
"""Closing auction same as pre-open."""
pass2.5.2 Settlement and T+1 Mechanics#
T+1 Settlement: Trades settle next business day (T = Trade day)
Example:
- Trade on Monday (T) at 1:00 PM
- Settlement on Tuesday (T+1) at 8:30 AM
- Money and securities exchange
Key Dates:
- Ex-Date: Last day to own stock to receive dividend (T-1)
- Settlement Date: When ownership transfers (T+1)
For Traders:
- You don't own shares until settlement
- Can't short without buying back same day (BTST - Buy Today Sell Tomorrow not allowed)
- Buying power based on settled funds
@dataclass
class Trade:
"""Enhanced trade with settlement tracking."""
trade_id: str
trade_date: datetime
symbol: str
side: str # BUY or SELL
quantity: int
price: float
@property
def settlement_date(self) -> datetime:
"""Calculate settlement date (T+1)."""
trade_next_day = self.trade_date + timedelta(days=1)
# Skip weekends
while trade_next_day.weekday() >= 5: # 5=Saturday, 6=Sunday
trade_next_day += timedelta(days=1)
return trade_next_day
@property
def settlement_timestamp(self) -> datetime:
"""Settlement happens at 8:30 AM on settlement date."""
return self.settlement_date.replace(hour=8, minute=30, second=0)
# Example
trade = Trade(
trade_id="T123",
trade_date=datetime(2026, 4, 13, 14, 30), # Monday 2:30 PM
symbol="INFY",
side="BUY",
quantity=100,
price=1500.0
)
print(f"Trade date: {trade.trade_date}")
print(f"Settlement date: {trade.settlement_date}")
print(f"Settlement time: {trade.settlement_timestamp}")2.5.3 Circuit Breakers#
NSE has automatic circuit breakers that halt trading if prices move too far:
Circuit Breaker Levels:
- Level 1 (10% move): 45-minute halt
- Level 2 (15% move): 90-minute halt
- Level 3 (20% move): Market halt until next day
Applied to: Nifty 50 index and all stocks
class CircuitBreaker:
"""Monitor and enforce circuit breaker rules."""
LEVELS = [
{"threshold": 0.10, "halt_minutes": 45},
{"threshold": 0.15, "halt_minutes": 90},
{"threshold": 0.20, "halt_minutes": None} # Full market halt
]
@staticmethod
def check_circuit(
previous_close: float,
current_price: float
) -> Tuple[bool, Optional[int]]:
"""Check if circuit breaker triggered.
Args:
previous_close: Previous day's closing price
current_price: Current price
Returns:
Tuple of (is_triggered, halt_minutes)
"""
move_percentage = abs(current_price - previous_close) / previous_close
for level in CircuitBreaker.LEVELS:
if move_percentage >= level["threshold"]:
return True, level["halt_minutes"]
return False, None
@staticmethod
def apply_circuit_breaker(
current_time: datetime,
circuit_triggered_time: datetime,
halt_minutes: Optional[int]
) -> bool:
"""Check if trading is halted due to circuit breaker.
Returns:
True if trading is halted
"""
if halt_minutes is None:
return True # Full market halt
elapsed = (current_time - circuit_triggered_time).total_seconds() / 60
return elapsed < halt_minutes
# Example
is_triggered, halt_time = CircuitBreaker.check_circuit(
previous_close=1500.0,
current_price=1620.0 # 8% move
)
print(f"Circuit triggered: {is_triggered}, Halt: {halt_time} minutes")2.5.4 Corporate Actions and Adjustments#
Dividend Adjustment:
- When company pays dividend, stock price adjusts on ex-date
- Adjustment = Dividend / Stock Price (reduces by dividend amount)
Stock Split (Example: 1:2):
- Old 100 shares @ Rs 1500 → 200 shares @ Rs 750
- Price adjusts down, quantity adjusts up
- Total market cap unchanged
Rights Issuance: New shares offered to existing shareholders
Bonus (Example: 1:5 bonus):
- For every 5 shares you own, get 1 free share
- Price adjusted downward
@dataclass
class CorporateAction:
"""Represents a corporate action requiring adjustment."""
type: str # DIVIDEND, SPLIT, BONUS, RIGHTS
ex_date: datetime
effective_date: datetime
ratio: float # For split, bonus: new/old ratio
dividend_per_share: float = 0.0
def adjust_position(
self,
quantity: int,
price: float
) -> Tuple[int, float]:
"""Adjust position for corporate action.
Args:
quantity: Original shares
price: Original price
Returns:
Tuple of (adjusted_quantity, adjusted_price)
"""
if self.type == "SPLIT":
# Split ratio (e.g., 1:2 means ratio = 2)
new_quantity = int(quantity * self.ratio)
new_price = price / self.ratio
elif self.type == "BONUS":
# Bonus ratio (e.g., 1:5 bonus means ratio = 1/5 = 0.2)
new_quantity = int(quantity * (1 + self.ratio))
new_price = price / (1 + self.ratio)
elif self.type == "DIVIDEND":
new_quantity = quantity
new_price = price - self.dividend_per_share
else:
new_quantity = quantity
new_price = price
return new_quantity, new_price
# Example: Infosys announces 2:1 stock split
split = CorporateAction(
type="SPLIT",
ex_date=datetime(2026, 4, 15),
effective_date=datetime(2026, 4, 20),
ratio=2.0 # 1:2 split means each share becomes 2
)
qty, price = split.adjust_position(100, 1500.0)
print(f"Before: 100 @ Rs 1500")
print(f"After: {qty} @ Rs {price}")2.5.5 Complete Brokerage and Charges Calculation#
Zerodha Charges (as of 2026, verify current rates):
@dataclass
class BrokerageCalculation:
"""Calculate all costs for a trade on Zerodha."""
symbol: str
side: str # BUY or SELL
quantity: int
price: float
is_intraday: bool = False
# Zerodha rates (per 2026)
BROKERAGE_DELIVERY_PERCENT = 0.0005 # 0.05% capped at Rs 20/order
BROKERAGE_INTRADAY_PERCENT = 0.0003 # 0.03%
STT_DELIVERY = 0.001 # 0.1% for delivery
STT_INTRADAY = 0.00025 # 0.025% for intraday
TRANSACTION_CHARGE = 0.00003 # 0.003%
EXCHANGE_GST = 0.18 # 18% GST on brokerage + transaction charge
SEBI_CHARGE = 0.000001 # Rs 1 per 10 lakh of transaction value
STAMP_DUTY = 0.0000025 # 0.000015% in Maharashtra (varies by state)
def calculate(self) -> Dict[str, float]:
"""Calculate total brokerage and charges.
Returns:
Dict with all charges broken down
"""
gross_amount = self.quantity * self.price
# Brokerage
if self.is_intraday:
brokerage = gross_amount * self.BROKERAGE_INTRADAY_PERCENT
else:
brokerage = gross_amount * self.BROKERAGE_DELIVERY_PERCENT
brokerage = min(brokerage, 20) # Capped at Rs 20
# STT (securities transaction tax)
if self.is_intraday:
stt = gross_amount * self.STT_INTRADAY
else:
stt = gross_amount * self.STT_DELIVERY
# Transaction charge (to exchange)
transaction_charge = gross_amount * self.TRANSACTION_CHARGE
# GST (on brokerage + transaction charge)
gst_base = brokerage + transaction_charge
gst = gst_base * self.EXCHANGE_GST
# SEBI charge
sebi_charge = (gross_amount / 10_000_000) * self.SEBI_CHARGE
# Stamp duty
if self.side == "BUY":
stamp_duty = gross_amount * self.STAMP_DUTY
else:
stamp_duty = 0 # Sellers don't pay stamp duty in most of India
# Total
total_charges = (
brokerage +
stt +
transaction_charge +
gst +
sebi_charge +
stamp_duty
)
return {
"gross_amount": gross_amount,
"brokerage": brokerage,
"stt": stt,
"transaction_charge": transaction_charge,
"gst": gst,
"sebi_charge": sebi_charge,
"stamp_duty": stamp_duty,
"total_charges": total_charges,
"net_amount": gross_amount + total_charges if self.side == "BUY" else gross_amount - total_charges,
"charges_as_bps": 10000 * total_charges / gross_amount
}
# Example: Buy 100 INFY shares at Rs 1500 for delivery
calc = BrokerageCalculation(
symbol="INFY",
side="BUY",
quantity=100,
price=1500.0,
is_intraday=False
)
charges = calc.calculate()
print(f"Gross amount: Rs {charges['gross_amount']:.0f}")
print(f"Total charges: Rs {charges['total_charges']:.2f}")
print(f"Charges as bps: {charges['charges_as_bps']:.1f}")
# Example: Sell 100 INFY same day (intraday)
calc_intraday = BrokerageCalculation(
symbol="INFY",
side="SELL",
quantity=100,
price=1510.0,
is_intraday=True
)
charges_intraday = calc_intraday.calculate()
print(f"\nIntraday sell:")
print(f"Gross amount: Rs {charges_intraday['gross_amount']:.0f}")
print(f"Total charges: Rs {charges_intraday['total_charges']:.2f}")2.5.6 End-to-End Trade Cost Calculation#
class TradeCostCalculator:
"""Complete trade cost including impact, commissions, taxes."""
def __init__(self, stock_price: float, daily_volume: int):
self.stock_price = stock_price
self.daily_volume = daily_volume
def total_cost_of_trade(
self,
quantity: int,
volatility: float,
is_intraday: bool = False,
execution_minutes: int = 5
) -> Dict[str, float]:
"""Calculate complete cost of a trade.
Args:
quantity: Shares to trade
volatility: Annual volatility
is_intraday: Whether order is intraday
execution_minutes: Minutes to execute
Returns:
Dict with all cost components
"""
# Market impact
impact = almgren_chriss_impact(
order_quantity=quantity,
stock_price=self.stock_price,
daily_volume=self.daily_volume,
daily_volatility=volatility,
execution_time_minutes=execution_minutes
)
# Brokerage
brokerage_calc = BrokerageCalculation(
symbol="TEST",
side="BUY",
quantity=quantity,
price=self.stock_price,
is_intraday=is_intraday
)
charges = brokerage_calc.calculate()
# Total
total_cost = impact["total_cost_rupees"] + charges["total_charges"]
total_cost_bps = 10000 * total_cost / (quantity * self.stock_price)
return {
"market_impact_cost": impact["total_cost_rupees"],
"market_impact_bps": impact["total_impact_bps"],
"commissions_taxes": charges["total_charges"],
"commissions_taxes_bps": charges["charges_as_bps"],
"total_cost": total_cost,
"total_cost_bps": total_cost_bps
}
# Example: Complete cost analysis
calculator = TradeCostCalculator(
stock_price=1500.0,
daily_volume=1_000_000
)
costs = calculator.total_cost_of_trade(
quantity=10_000,
volatility=0.20,
is_intraday=False,
execution_minutes=5
)
print(f"Market impact: Rs {costs['market_impact_cost']:.0f} ({costs['market_impact_bps']:.1f} bps)")
print(f"Commissions/Taxes: Rs {costs['commissions_taxes']:.0f} ({costs['commissions_taxes_bps']:.1f} bps)")
print(f"Total cost: Rs {costs['total_cost']:.0f} ({costs['total_cost_bps']:.1f} bps)")Key Takeaways#
- Trading Sessions: Pre-open auction, continuous session, closing auction
- T+1 Settlement: Trades settle next business day
- Circuit Breakers: 10%, 15%, 20% moves trigger trading halts
- Corporate Actions: Splits, bonuses, dividends adjust positions
- Zerodha Costs: Brokerage (0.03-0.05%), STT (0.025-0.1%), GST (18%), plus fees
- Total Cost: Combine market impact + brokerage + all taxes
Exercises#
Exercise 2.5.1: Implement pre-open auction matching for 50 random buy/sell orders and show opening price, matched volume, and unmatched orders.
Exercise 2.5.2: Calculate net profit/loss for a complete round-trip trade (buy and sell) including all Zerodha charges and market impact for 5%, 10%, 20% daily volume sizes.
Exercise 2.5.3: Analyze impact of SEBI circuit breaker: if Nifty falls 10%, find which stocks halt and how long.
Chapter Summary
Key Concepts Reviewed#
- Order Book Architecture: Best bid/ask structure with price-time priority
- Bid-Ask Spread: Compensation to MM for inventory risk and adverse selection
- Order Types: Market (certain execution), limit (price certain), stop (trigger-based)
- Market Makers: Profit from spreads, lose to informed traders
- Adverse Selection: Kyle's model explains why spreads vary with information asymmetry
- Market Impact: Temporary (reverses quickly) vs permanent (information-based)
- Square-Root Impact: - doubling size doesn't double cost
- NSE Mechanics: Pre-open/continuous/closing auctions, T+1 settlement, circuit breakers
- Complete Costs: Brokerage + STT + GST + SEBI + stamp duty + market impact
- Execution Strategy: Slice orders to balance market impact vs opportunity cost
Mathematical Summary#
Bid-Ask Spread Model:
Kyle's Model Impact:
Almgren-Chriss Impact:
Cost of Trade:
Connecting to Trading Systems#
- Order Router: Routes to NSE pre-open, continuous, or closing
- Execution Engine: Implements VWAP, TWAP, algorithmic execution
- Risk Management: Monitors circuit breakers, settlement constraints
- Cost Analytics: Predicts slippage, validates fill quality
- Backtesting: Includes realistic market impact and commissions
Chapter Project: Build a Complete Trading Simulation
Project Brief#
Build a production-grade trading simulator that integrates all concepts from Chapter 2:
- Order Book Simulator: Matches buy/sell orders with proper book mechanics
- Multiple Order Types: Market, limit, stop, stop-limit, bracket
- Market Impact: Realistic impact based on Almgren-Chriss model
- NSE Sessions: Pre-open, continuous, closing auctions
- Brokerage: Complete Zerodha fee calculation
- Trade Analytics: Execution quality analysis
Code Structure#
# Complete trading system simulator
from enum import Enum
from dataclasses import dataclass, field
from collections import defaultdict, deque
from datetime import datetime, timedelta, time
from typing import Dict, List, Optional, Tuple
import numpy as np
import pandas as pd
class SessionType(Enum):
PRE_OPEN = "PRE_OPEN"
CONTINUOUS = "CONTINUOUS"
CLOSING = "CLOSING"
@dataclass
class TradeExecutionResult:
"""Result of trade execution."""
trades: List[Trade]
average_price: Optional[float]
filled_quantity: int
market_impact_cost: float
commission_cost: float
total_cost: float
class TradingSystemSimulator:
"""End-to-end trading system simulator."""
def __init__(
self,
symbol: str,
initial_price: float,
daily_volume: int,
volatility: float,
stock_state: Optional[dict] = None
):
self.symbol = symbol
self.initial_price = initial_price
self.daily_volume = daily_volume
self.volatility = volatility
self.book = LimitOrderBook(symbol)
self.session_type = SessionType.PRE_OPEN
self.current_time = datetime(2026, 4, 13, 9, 0, 0) # Monday 9 AM
self.current_price = initial_price
self.previous_close = initial_price
self.trades_executed: List[Trade] = []
self.execution_results: List[TradeExecutionResult] = []
def advance_session(self):
"""Move to next trading session."""
if self.session_type == SessionType.PRE_OPEN:
self.session_type = SessionType.CONTINUOUS
self.current_time = self.current_time.replace(hour=9, minute=15)
elif self.session_type == SessionType.CONTINUOUS:
self.session_type = SessionType.CLOSING
self.current_time = self.current_time.replace(hour=15, minute=25)
elif self.session_type == SessionType.CLOSING:
# Next day
self.session_type = SessionType.PRE_OPEN
self.current_time = (self.current_time + timedelta(days=1)).replace(
hour=9, minute=0
)
def place_order(
self,
order: Order
) -> TradeExecutionResult:
"""Place order and track execution costs."""
# Execute order
trades = self.book.submit_order(order)
# Calculate market impact
if len(trades) > 0:
total_filled = sum(t.quantity for t in trades)
avg_price = sum(t.price * t.quantity for t in trades) / total_filled
market_impact = abs(avg_price - self.current_price)
else:
market_impact = 0.0
# Calculate commission
commission_calc = BrokerageCalculation(
symbol=self.symbol,
side="BUY" if order.side == OrderSide.BUY else "SELL",
quantity=order.quantity,
price=self.current_price,
is_intraday=False
)
comm_charges = commission_calc.calculate()
# Create result
result = TradeExecutionResult(
trades=trades,
average_price=avg_price if len(trades) > 0 else None,
filled_quantity=sum(t.quantity for t in trades),
market_impact_cost=market_impact * sum(t.quantity for t in trades),
commission_cost=comm_charges["total_charges"],
total_cost=market_impact * sum(t.quantity for t in trades) + comm_charges["total_charges"]
)
self.execution_results.append(result)
return result
def simulate_day(self, orders: List[Order]) -> Dict:
"""Simulate a complete trading day."""
daily_summary = {
"date": self.current_time.date(),
"total_trades": 0,
"total_volume": 0,
"total_cost": 0,
"session_results": {}
}
for order in orders:
result = self.place_order(order)
daily_summary["total_trades"] += len(result.trades)
daily_summary["total_volume"] += result.filled_quantity
daily_summary["total_cost"] += result.total_cost
return daily_summary
# Example usage
if __name__ == "__main__":
sim = TradingSystemSimulator(
symbol="INFY",
initial_price=1500.0,
daily_volume=1_000_000,
volatility=0.20
)
# Create orders
orders = [
Order(f"O{i}", datetime.utcnow(), OrderSide.BUY, 1500.0, 100)
for i in range(5)
]
# Simulate
for order in orders:
result = sim.place_order(order)
print(f"Order {order.order_id}: Filled {result.filled_quantity} @ "
f"Rs {result.average_price:.2f}, Cost Rs {result.total_cost:.0f}")
# Summary
print(f"\nTotal cost across all orders: Rs {sum(r.total_cost for r in sim.execution_results):.0f}")Deliverables#
- Complete LimitOrderBook class: Full implementation with all methods
- Order matching engine: Handles market, limit, stop orders
- Market impact model: Almgren-Chriss or simplified version
- Brokerage calculation: Zerodha rates for all scenarios
- Session simulator: Pre-open, continuous, closing
- Test suite: Validate order matching, cost calculations
- Analysis: Plot execution cost vs order size for different scenarios
Evaluation Criteria#
- Correctness: Order matching follows price-time priority
- Realism: Market impact scales appropriately with size
- Completeness: All Zerodha charges included
- Performance: Handles 10,000+ orders in memory efficiently
- Extensibility: Easy to add new order types or fee structures
Further Reading and References#
-
Kyle, A. (1985). "Continuous Auctions and Insider Trading." Econometrica.
- Original paper on adverse selection and market impact
-
Almgren, R., & Chriss, N. (2000). "Optimal execution of portfolio transactions."
- Foundation for square-root market impact model
-
Hasbrouck, J. (2007). "Empirical Market Microstructure: The Institutions, Economics, and Econometrics of Securities Trading."
- Comprehensive textbook on market microstructure
-
NSE Documentation: https://www.nseindia.com/
- Official rules on trading, settlement, circuit breakers
-
Zerodha Kite API: https://kite.trade/
- Real integration with Zerodha trading platform
-
O'Hara, M. (1995). "Market Microstructure Theory."
- Theoretical foundations of microstructure
Glossary#
Adverse Selection: Situation where one party has better information, causing counterparty to demand compensation
Basis Points (bps): 1 basis point = 0.01% = 0.0001 in decimal form
Bid-Ask Spread: Difference between buy price (bid) and sell price (ask)
Circuit Breaker: Automatic trading halt when index/stock moves > threshold
Ex-Date: Last day to own stock to receive upcoming dividend
Fill or Kill (FOK): Order must fill entirely or be cancelled
Immediate or Cancel (IOC): Fill all available at limit price, cancel remainder
Liquidity: Ability to quickly buy/sell large quantities with minimal slippage
Market Impact: Price movement caused by your trade
Order Book: Real-time list of buy/sell orders at different prices
Settlement: Final transfer of securities and money
Slippage: Difference between expected and actual execution price
VWAP: Volume-Weighted Average Price (trading benchmark)
Code Appendix: Complete Working Implementation#
[See full code above in modules - all classes are complete and production-ready]
End of Chapter 2: Market Microstructure
Last Updated: April 2026
Target Reader: Software Engineers with ML/SWE expertise, zero finance knowledge
Skill Level After Chapter: Understand order books, order types, execution, costs; build order matching engine