Variations & Follow-ups

📖 8 min read 📄 Part 8 of 10

Stock Trading Platform - Variations and Follow-up Questions

Common Variations

1. Options Trading

Problem: Trade options contracts with complex pricing

Additional Requirements:

Options Specifics:
- Strike price, expiration date
- Call vs Put options
- American vs European style
- Greeks calculation (Delta, Gamma, Theta, Vega)

Pricing:
- Black-Scholes model
- Implied volatility
- Real-time Greeks updates

Risk Management:
- Naked vs covered positions
- Margin requirements (higher than stocks)
- Assignment risk
- Early exercise (American options)

Challenges:
- Complex pricing models
- Higher computational cost
- More risk factors
- Regulatory complexity

2. Cryptocurrency Trading

Problem: 24/7 trading with high volatility

Key Differences:

Trading Hours:
- 24/7/365 (no market close)
- No circuit breakers
- Continuous operation

Volatility:
- 10x more volatile than stocks
- Flash crashes common
- Wider spreads

Settlement:
- Blockchain settlement (minutes to hours)
- Wallet management
- Private key security

Challenges:
- Always-on infrastructure
- Extreme volatility handling
- Blockchain integration
- Security (hacking risk)

3. Algorithmic Trading

Problem: Automated trading strategies

Implementation:

Strategy Engine:
- User-defined algorithms
- Backtesting framework
- Paper trading
- Live execution

Common Strategies:
- Market making
- Arbitrage
- Momentum trading
- Mean reversion

API Requirements:
- Streaming market data
- Fast order execution
- Position management
- Risk controls

Challenges:
- Strategy isolation
- Resource limits
- Fair access
- Market manipulation prevention

4. Dark Pools

Problem: Anonymous trading for large orders

Features:

Anonymity:
- No pre-trade transparency
- Hidden order book
- Post-trade reporting only

Matching:
- Midpoint pricing
- Size priority
- Time priority

Benefits:
- Reduced market impact
- Better prices for large orders
- No information leakage

Challenges:
- Regulatory scrutiny
- Price discovery
- Fairness concerns

5. Margin Trading

Problem: Trade with borrowed funds

Implementation:

Margin Calculation:
- Initial margin: 50% (Reg T)
- Maintenance margin: 25%
- Real-time margin checks

Margin Call:
- Trigger when below maintenance
- Forced liquidation if not met
- Automatic position closing

Risk Management:
- Position limits
- Concentration limits
- Leverage limits (2x for stocks)

Challenges:
- Real-time margin calculation
- Forced liquidation logic
- Risk of losses > account value

Follow-up Questions

Technical Deep Dives

Q1: How do you achieve microsecond latency?

Solution: Multi-Layer Optimization

1. Hardware:
   - High-frequency CPUs (5+ GHz)
   - Low-latency RAM (DDR5)
   - 100 Gbps NICs
   - NVMe SSDs

2. Network:
   - Co-location with exchange (<50 μs)
   - Kernel bypass (DPDK)
   - RDMA for inter-server
   - Direct fiber connections

3. Software:
   - Lock-free data structures
   - Zero-copy operations
   - CPU pinning
   - Huge pages (2 MB)

4. Algorithm:
   - In-memory order books
   - Pre-allocated memory
   - Cache-line alignment
   - Branchless code

Latency Budget:
- Network: 50 μs
- Order validation: 20 μs
- Risk check: 100 μs
- Matching: 500 μs
- Response: 50 μs
Total: <1 millisecond

Q2: How do you prevent race conditions in order matching?

Solution: Lock-Free Data Structures

Implementation:
1. Atomic Operations:
   - Compare-and-swap (CAS)
   - Atomic increment/decrement
   - Memory barriers

2. Lock-Free Order Book:
   - Lock-free skip list for price levels
   - Lock-free queue for orders at each price
   - Atomic best bid/ask pointers

3. Ordering Guarantees:
   - Timestamp orders (nanosecond precision)
   - Sequence numbers
   - FIFO within price level

4. Conflict Resolution:
   - CAS retry loop
   - Exponential backoff
   - Max 3 retries

Example:
while (!atomic_compare_exchange(&best_bid, &expected, &new_value)) {
    // Retry with updated expected value
    expected = atomic_load(&best_bid);
}

Benefits:
- No lock contention
- High concurrency
- Predictable latency

Q3: How do you handle market data distribution to 1M users?

Solution: Multicast + Regional Servers

Architecture:
Exchange → Market Data Gateway → Multicast → Regional Servers → Users

Multicast:
- Single send reaches all regional servers
- UDP multicast (224.0.0.0/4)
- No per-subscriber overhead
- 1 Gbps → 10 Gbps with multicast

Regional Servers:
- 100 servers globally
- 10K users per server
- Local caching
- <10ms latency to users

Optimization:
- Delta encoding (send only changes)
- Compression (10x reduction)
- Selective subscription (only subscribed symbols)
- Batching (group updates)

Throughput:
- 115K ticks/second from exchange
- 1M subscribers
- 115B updates/second total
- With optimization: 11.5B updates/second

Q4: How do you ensure ACID guarantees for trades?

Solution: Write-Ahead Logging + Optimistic Locking

Implementation:
1. Write-Ahead Log (WAL):
   - Write to log before applying
   - Sequential writes (fast)
   - Durability guarantee

2. Optimistic Locking:
   - Version numbers on accounts
   - CAS for updates
   - Retry on conflict

3. Transaction:
BEGIN TRANSACTION;
  -- Update buyer
  UPDATE accounts 
  SET cash = cash - :amount, version = version + 1
  WHERE account_id = :buyer AND version = :expected_version;
  
  -- Update seller
  UPDATE accounts 
  SET cash = cash + :amount, version = version + 1
  WHERE account_id = :seller AND version = :expected_version;
  
  -- Record trade
  INSERT INTO trades (...) VALUES (...);
COMMIT;

4. Recovery:
   - Replay WAL on crash
   - Idempotent operations
   - <1 second recovery

Benefits:
- ACID compliance
- Fast commits (async)
- Zero data loss

Q5: How do you handle circuit breakers?

Solution: Real-Time Price Monitoring

Implementation:
1. Price Limits:
   - Limit Up/Down: ±10% from previous close
   - Collar: ±5% from last trade
   - Dynamic adjustment

2. Volatility Detection:
   - Track price changes
   - Calculate volatility
   - Trigger on threshold

3. Circuit Breaker Levels:
   - Level 1: 7% drop → 15-minute halt
   - Level 2: 13% drop → 15-minute halt
   - Level 3: 20% drop → Close market

4. Actions:
   - Halt trading immediately
   - Cancel pending orders
   - Notify users
   - Resume after cooldown

5. Implementation:
if (price_change > threshold) {
    halt_trading(symbol);
    cancel_pending_orders(symbol);
    notify_users(symbol, reason);
    schedule_resume(symbol, cooldown_period);
}

Regulatory: Required by SEC

Scalability Questions

Q6: How would you scale to 10x order volume?

Current: 1M orders/second
Target: 10M orders/second

Scaling Strategy:
1. Matching Engine:
   - 100 → 1000 servers (10x)
   - More symbols per server
   - Better load balancing

2. Network:
   - 100 Gbps → 400 Gbps NICs
   - More cross-connects
   - Additional co-location racks

3. Database:
   - More shards (10 → 100)
   - Faster SSDs (NVMe)
   - Larger write buffers

4. Market Data:
   - More regional servers
   - Better compression
   - Smarter routing

Cost: $70M/year (10x current)

Q7: How do you handle database hotspots for popular stocks?

Problem: AAPL gets 10K orders/second, other stocks get 10

Solution: Dedicated Resources

1. Matching Engine:
   - AAPL: Dedicated server
   - GOOGL, MSFT: Dedicated servers
   - Others: Shared servers

2. Database:
   - Hot stocks: Dedicated shards
   - Cold stocks: Shared shards
   - Separate write buffers

3. Caching:
   - Hot stocks: Always in cache
   - Cold stocks: LRU eviction
   - Pre-warm on market open

4. Monitoring:
   - Detect hot stocks
   - Auto-scale resources
   - Rebalance load

Benefits:
- Isolated performance
- No cross-stock impact
- Predictable latency

Business Logic Questions

Q8: How do you calculate margin requirements?

Margin Calculation:

Initial Margin (Reg T):
- Stocks: 50% of purchase price
- Options: Complex formula based on risk

Maintenance Margin:
- Stocks: 25% of current value
- Options: Higher (varies by position)

Example:
Buy 100 shares of AAPL at $150:
- Total cost: $15,000
- Initial margin: $7,500 (50%)
- Borrowed: $7,500
- Maintenance: $3,750 (25% of $15,000)

Margin Call:
If account value < maintenance margin:
1. Notify user
2. Give 2-5 days to deposit funds
3. If not met: Force liquidation

Real-Time Calculation:
- Update on every trade
- Update on price changes
- Check before new orders
- <100 microseconds

Q9: How do you prevent front-running?

Prevention Measures:

1. Fair Access:
   - FIFO order matching
   - No preferential treatment
   - Timestamp all orders

2. Monitoring:
   - Detect suspicious patterns
   - Analyze order flow
   - Flag potential violations

3. Audit Trail:
   - Complete order history
   - Immutable logs
   - Regulatory reporting

4. Technology:
   - Encrypted connections
   - Access controls
   - Employee monitoring

5. Compliance:
   - Regular audits
   - Employee training
   - Whistleblower protection

Penalties:
- Account suspension
- Fines
- Criminal charges

Q10: How do you handle after-hours trading?

After-Hours Trading:

Extended Hours:
- Pre-market: 4:00 AM - 9:30 AM ET
- After-hours: 4:00 PM - 8:00 PM ET

Differences:
- Lower liquidity
- Wider spreads
- More volatility
- Limit orders only (no market orders)

Implementation:
- Separate order books
- Different matching rules
- Risk warnings to users
- Limited symbols (major stocks only)

Challenges:
- Lower volume
- Price gaps
- News impact
- Regulatory differences

These variations and follow-up questions demonstrate the complexity of building a high-frequency trading platform that handles extreme performance requirements while maintaining fairness and regulatory compliance.