Finance Notes

ch3.trading_exchange_dynamics

Ch 3. Understanding the Trading Exchange Dynamics

Architecting a Trading Exchange for Large-Scale Order Handling

Purpose of Stock Exchanges

  • Facilitate trading between existing owners and potential purchasers.
  • Function as secondary or tertiary markets rather than primary markets.
  • Enable transactions between shareholders rather than direct asset trading by companies.

Key Components of a Trading Exchange

  1. Order Matching Engine:

    • Function: Matches buy and sell orders based on price and time priority.
    • Efficiency: Must handle high volumes with minimal latency to maintain market integrity.
  2. Market Data Feed:

    • Function: Provides real-time price updates and market depth information.
    • Importance: Essential for transparency and informed trading decisions.
  3. Clearing and Settlement System:

    • Function: Ensures that trades are accurately processed and settled within the agreed timeframe.
    • Security: Must be robust to prevent defaults and manage counterparty risk.
  4. Risk Management System:

    • Function: Monitors and mitigates risks associated with trading activities.
    • Compliance: Ensures adherence to regulatory requirements and internal risk policies.
  5. Order Management System (OMS):

    • Function: Manages the lifecycle of orders from creation to execution, amendment, cancellation, and rejection.
    • Efficiency: Facilitates quick response to market conditions and maintains order integrity.
  6. Communication Gateway:

    • Function: Interfaces with external participants and other exchanges.
    • Scalability: Must handle large volumes of data with low latency.
  7. Regulatory Reporting and Surveillance:

    • Function: Tracks and reports trading activities to regulatory bodies.
    • Compliance: Ensures transparency and adherence to market rules.

Order Handling Process

  1. Receiving Orders:

    • Orders are submitted by traders via gateways.
    • Orders are validated for correctness and completeness.
  2. Order Matching:

    • Orders are entered into the order book.
    • The matching engine pairs buy and sell orders based on price and time priority.
  3. Transaction Execution:

    • Matched orders are executed.
    • Trade details are sent to the clearing and settlement system.
  4. Market Data Dissemination:

    • Trade information is broadcast to market participants via data feeds.
  5. Clearing and Settlement:

    • Trades are cleared and settled to ensure the transfer of assets and funds.
  6. Post-Trade Processing:

    • Positions and risk are updated.
    • Reports are generated for regulatory compliance and internal analysis.

Auction Mechanism

  • Bid: Price at which a buyer is willing to purchase.
  • Ask (Offer): Price at which a seller is willing to sell.
  • Transaction: Occurs when bid and ask prices match, ensuring fair market value determination.

Understanding Features of a Stock Exchange

  • A stock exchange serves as a marketplace where various market players engage in transactions. The exchange aims to streamline transactions, reduce risks, and facilitate price discovery
  1. Listings:

    • Description: Corporations listed on the exchange that have undergone the initial public offering (IPO) process to become public entities.
    • Considerations: Valuation, liquidity, and compliance costs influence the choice of the exchange for listing.
  2. Matching Engine:

    • Function: The automated system that replaces traditional floor trading, matching buy and sell orders in nanoseconds.
    • Operation: Publishes the order book (pending trades) and executes trades based on the matching algorithm.
    • Price Determination: Varies slightly between exchanges; details provided in the General Order Book and Matching Engine section.
  3. Post-Trade:

    • Activities: Involves payment, settlement, and trade reconciliation to ensure correct matching and completion of all orders.
    • Importance: Critical backend processes for maintaining the integrity of transactions.
  4. Market Data:

    • Handling: Manages large volumes of data, which is sold to market participants.
    • Examples: Trade prices, volumes, corporate announcements, filings.
    • Access Speed: Enhanced by co-location services, especially for high-frequency trading (HFT) systems.
  5. Market Participants:

    • Types: Includes clearing and trading members with different qualifications and responsibilities.
    • Collateral: Members place collateral with the exchange to safeguard against defaults, ensuring market stability.
    • Roles:
      • Trading Members: Brokers and proprietary trading houses.
      • Clearing Members: Major participants involved in trade clearing.
  6. Regulation:

    • Variations: Differ by asset class and jurisdiction to prevent money laundering, insider trading, and market manipulation.
    • Monitoring: Exchanges ensure compliance with regulatory disclosures and manage internal and government authority requirements.

Additional Features

  • Collateral Management:
    • Ensures exchanges have the financial backing to cover potential defaults, maintaining overall market integrity.
  • Co-Location Services:
    • Allows traders to place their systems close to the exchange's servers to reduce latency, crucial for HFT operations.

Importance of Regulation

  • Objective: Ensures a transparent and fair marketplace by preventing unethical practices.
  • Enforcement: Requires exchanges to monitor company announcements and market participant activities.

Exchange Architecture

flowchart TD
    TradingSystem[TradingSystem T1] -->|Order| Router
    Router -->|TSLA| FIFO_TSLA[FIFO Queues per price level<br>symbol: TSLA]
    Router -->|AAPL| FIFO_AAPL[FIFO Queues per price level<br>symbol: AAPL]
    Router -->|MSFT| FIFO_MSFT[FIFO Queues per price level<br>symbol: MSFT]
    FIFO_MSFT --> MatchingEngine["Matching Engine (MSFT)<br>Bids and Offers"]

    MatchingEngine --> |"T1 will receive a specific<br>order update (order communication side)"|TradingSystem
    MatchingEngine --> |"All traders receive an update regarding this order <br>(market data communication side)"|AllTraders["All traders including T1"]

Key Functional Components

  1. Trading System (T1):

    • Executes orders received from portfolio managers.
    • Manages and monitors orders during execution.
    • Offers electronic access to various trading venues.
  2. Router:

    • Routes orders based on asset class and instrument symbol.
    • Directs orders to specific FIFO queues for each price level and symbol (e.g., TSLA, AAPL, MSFT).
  3. FIFO Queues:

    • Organizes orders for each price level per symbol (e.g., TSLA, AAPL, MSFT).
    • Ensures orders are processed in a First-In-First-Out manner.
  4. Matching Engine:

    • Matches buy and sell orders based on price and time priority.
    • Updates the order book and communicates changes to traders and market participants.
  5. Market Data Communication:

    • Provides real-time updates to all traders.
    • Ensures transparency and timely information dissemination.
  6. Order Communication:

    • Sends specific order updates back to the originating trading system (e.g., T1).

Business Requirements

  • Trading Capabilities:

    • Support for front, middle, and back-office operations.
    • Accommodates both basic and advanced rule-based and algorithmic trading strategies.
  • Strategy Support:

    • Enables back-testing and live execution of trading strategies throughout their development lifecycle.
  • User Interfaces:

    • Offers desktop, web-based, and mobile applications for trading and blotter displays.
  • Trading as a Service (TaaS):

    • Provides trading capabilities as a utility via open APIs.
    • Supports API integration using the FIX protocol.
  • Global Integration:

    • Facilitates connectivity with a wide range of external parties and supports various financial products.
  • Scalability:

    • Ensures the architecture can handle large volumes of trades and data efficiently.

Process Flow

  1. Order Routing:

    • Orders are routed to specific FIFO queues based on price and symbol.
  2. Order Matching:

    • The matching engine processes orders from the queues.
    • Matches orders one at a time based on predefined algorithms.
  3. Order Book Updates:

    • Changes in the order book are communicated to the originating trader and all market participants.
    • Updates are sent to clearing and post-trade systems if a trade occurs.

Communication Protocols

  • Speed Requirements:

    • High-frequency trading (HFT) requires rapid communication between traders and exchanges.
    • The FIX protocol, while common, may not suffice for microsecond-level trading.
  • Binary Protocols:

    • Exchanges often use binary protocols to achieve the necessary speed for HFT.

General Order Book and Matching Engine

  • Modern Trading:
    • Electronic Trading Systems: Automate the matching of buyers and sellers.
    • High-Frequency Trading (HFT): Requires nanosecond-level transaction speeds for profitability.
flowchart TD
    Order1((1. Order)) --> MatchingEngine
    OrderBook2[2. Order book] --> MatchingEngine
    subgraph RestingOrders[4. RestingOrders]
      RestingOrders1[Resting Order 1]
      RestingOrders2[Resting Order 2]
      RestingOrders3[Resting Order 3]
    end
    subgraph Trades[3. Trade]
      Trade1
      Trade2
    end
    MatchingEngine --> Trades
    Caption["Will be in the order book \nfor next iterations"]
    RestingOrders -.- Caption
    Caption --> OrderBook2
    MatchingEngine --> |4.| RestingOrders
    

A stock exchange facilitates trading by connecting buyers and sellers. It uses an order book and a matching engine to process transactions efficiently.

Key Components

  1. Order:

    • Represents buy or sell instructions from traders.
    • Input to the matching engine for processing.
  2. Order Book:

    • Contains all bids and offers from market participants.
    • Serves as a repository of active orders waiting to be matched.
  3. Matching Engine:

    • Algorithm that matches buy and sell orders to execute trades.
    • Processes each incoming order against the order book.
    • Returns executed trades and updates the order book with any resting orders.
  4. Trades:

    • Result of matched orders executed by the matching engine.
    • Communicated back to traders and market participants.
  5. Resting Orders:

    • Orders that remain in the order book after partial or no execution.
    • Carried forward for subsequent matching attempts.

Matching Engine Algorithm Flow

  1. Order Input:

    • Orders are received from traders and input into the matching engine.
  2. Order Book Input:

    • The existing order book is accessed to find potential matches for incoming orders.
  3. Trade Execution:

    • The matching engine matches orders based on predefined algorithms.
    • Trades are executed, and details are communicated to all traders.
  4. Resting Orders Update:

    • Unmatched or partially matched orders are updated in the order book.
    • These resting orders are available for future matching attempts.

Importance of the Matching Engine

  • Efficiency: Quickly matches buy and sell orders, minimizing latency.
  • Algorithm Variability: Different exchanges use unique algorithms to prioritize and match orders.
  • Performance: Critical for HFT and other time-sensitive trading strategies.

Matching Engine Scenarios

1. Best Price Scenario

  • Situation: A new buy order (#1) wants to purchase an asset at $100.
  • Order Book: Contains sell orders at $99 (order #3) and $100 (order #2).
  • Outcome:
    • The matching engine will match order #1 with order #3 because $99 is the best available price.
    • Result: Trade executed at $99, order #2 remains.

2. Partial Fill Scenario

  • Situation:
    • Order #1 wants to buy 4 shares@100.
    • Order #3 offers to sell 1 share@99, Order #2 sell 2 share@100.
  • Order Book: Insufficient quantity to fully match order #1.
  • Outcome:
    • Order #3 and order #2 will be filled. And #1 is partially filled.
    • Remaining quantity of 1 share from order #1 will stay on the exchange.
    • Result: Two filled orders, one remaining order.

3. No Match Scenario

  • Situation:
    • Order #1 wants to buy at $98.
    • Order book has sell orders at $99 and higher.
  • Outcome:
    • No match because the buy price is lower than the sell prices.
    • Order #1 remains on the exchange.

4. Multiple Orders with the Same Price

  • Situation:
    • Incoming order #1 matches the price of existing orders #2 and #3.
  • Matching Engine Behavior:
    • Depends on the configured algorithm (FIFO or Pro-rata).

Matching Engine Algorithms

  1. FIFO (First In First Out)

    • Principle: Matches orders based on the time they entered the system.
    • Behavior:
      • Orders are stamped with a timestamp upon entering.
      • Orders with lower timestamps are matched first.
      • Modifications to orders result in loss of priority.
  2. Pure Pro-rata

    • Principle: Matches orders proportionally based on quantity.
    • Behavior:
      • Incoming orders are shared evenly among matching counter-orders.
      • Encourages order placement regardless of speed.
    • Example:
      • A buy order at $100 will be distributed proportionally among sell orders at $100, regardless of their timestamps.
      • Say new order #1 Buy 2@100, books contains order#3 sell 2@100, order2 sell 2@100
      • order #1 will be separate to 2 1@100 and partially filled both order #3 and order #2
  3. Pro-rata with Top-order

    • Principle: Combines FIFO and Pro-rata algorithms.
    • Behavior:
      • Oldest counter order is fully filled first.
      • Remaining quantity is distributed proportionally among other orders.
    • Example:
      • The oldest order is given priority for full execution.
      • Remaining quantities are allocated proportionally to other orders.