A professional-grade orderbook server for cryptocurrency trading with real-time WebSocket updates, Redis pub/sub, and support for multiple trading pairs.
- Multiple Trading Pairs: BTC/USD, ETH/USD, LTC/USD
- Order Types: Market orders, Limit orders
- Time in Force: GTC (Good Till Cancel), IOC (Immediate or Cancel), FOK (Fill or Kill)
- Efficient Data Structures: Price-level trees for O(log n) operations
- Real-time Matching: Price-time priority matching engine
- WebSocket Server: Live orderbook updates, trade feeds
- Redis Pub/Sub: Scalable message distribution
- Event Streaming: Order updates, trade executions, market data
- TypeScript: Type-safe development
- Express.js: RESTful API server
- Redis: Message queuing and pub/sub
- TimescaleDB: Time-series data storage
- Docker: Containerized deployment
-
Open WSL and navigate to the project:
# From Windows, open WSL wsl # Navigate to your project (adjust path as needed) cd /exchange
-
Start all services:
docker-compose up --build
-
Services will be available at:
- HTTP API:
http://localhost:3000 - WebSocket:
ws://localhost:3001 - Redis:
localhost:6379 - TimescaleDB:
localhost:5432
- HTTP API:
-
Setup dependencies (from Windows PowerShell or WSL):
# Windows PowerShell .\setup.ps1 # Or from WSL chmod +x setup.sh ./setup.sh # Or manually cd orderbook-server npm install npm run build
-
Start Redis and TimescaleDB (from WSL):
docker-compose up redis timescaledb
-
Start the orderbook server (from Windows or WSL):
# From orderbook-server directory npm run dev
GET /healthReturns service status and statistics.
GET /api/v1/pairsReturns all available trading pairs.
GET /api/v1/orderbook/:symbol?depth=20Returns orderbook snapshot for a trading pair.
Example:
curl "http://localhost:3000/api/v1/orderbook/BTCUSD?depth=10"POST /api/v1/order
Content-Type: application/json
{
"tradingPair": "BTCUSD",
"side": "buy",
"orderType": "limit",
"price": 50000,
"quantity": 0.1,
"timeInForce": "GTC",
"userId": "user123"
}Order Types:
- Limit Order: Requires
price - Market Order: Executes at best available price
Time in Force:
- GTC: Good Till Cancel (default)
- IOC: Immediate or Cancel
- FOK: Fill or Kill
DELETE /api/v1/order/:orderId
Content-Type: application/json
{
"tradingPair": "BTCUSD"
}GET /api/v1/stats/:symbol?Returns market statistics for one or all trading pairs.
GET /api/v1/trades/:symbol?limit=50Returns recent trade history.
Connect to ws://localhost:3001 for real-time updates.
const ws = new WebSocket("ws://localhost:3001");
ws.onopen = () => {
console.log("Connected to trading exchange");
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received:", data);
};ws.send(
JSON.stringify({
type: "subscribe",
channel: "market:BTCUSD:orderbook",
})
);ws.send(
JSON.stringify({
type: "subscribe",
channel: "market:BTCUSD:trades",
})
);ws.send(
JSON.stringify({
type: "getOrderbook",
tradingPair: "BTCUSD",
depth: 20,
})
);ws.send(
JSON.stringify({
type: "getMarketStats",
tradingPair: "BTCUSD", // Optional: omit for all pairs
})
);curl -X POST http://localhost:3000/api/v1/order \
-H "Content-Type: application/json" \
-d '{
"tradingPair": "BTCUSD",
"side": "buy",
"orderType": "limit",
"price": 49000,
"quantity": 0.05,
"timeInForce": "GTC"
}'curl -X POST http://localhost:3000/api/v1/order \
-H "Content-Type: application/json" \
-d '{
"tradingPair": "ETHUSD",
"side": "sell",
"orderType": "market",
"quantity": 1.0,
"timeInForce": "IOC"
}'curl "http://localhost:3000/api/v1/orderbook/BTCUSD?depth=5"Response:
{
"tradingPair": "BTCUSD",
"bids": [
{ "price": 49500, "quantity": 0.1, "orderCount": 1 },
{ "price": 49400, "quantity": 0.2, "orderCount": 1 }
],
"asks": [
{ "price": 50500, "quantity": 0.1, "orderCount": 1 },
{ "price": 50600, "quantity": 0.2, "orderCount": 1 }
],
"timestamp": "2025-08-10T12:00:00.000Z"
}The server supports these trading pairs by default:
| Symbol | Base | Quote | Min Size | Max Size | Price Precision | Quantity Precision |
|---|---|---|---|---|---|---|
| BTCUSD | BTC | USD | 0.00001 | 1000 | 2 | 8 |
| ETHUSD | ETH | USD | 0.001 | 10000 | 2 | 6 |
| LTCUSD | LTC | USD | 0.01 | 50000 | 2 | 4 |
The server initializes with sample liquidity for demonstration:
BTCUSD:
- Bids: 49500, 49400, 49300, 49200, 49100
- Asks: 50500, 50600, 50700, 50800, 50900
ETHUSD:
- Bids: 2980, 2970, 2960
- Asks: 3020, 3030, 3040
LTCUSD:
- Bids: 98, 97
- Asks: 102, 103
subscribe- Subscribe to a channelunsubscribe- Unsubscribe from a channelping- Heartbeat pinggetOrderbook- Request orderbook snapshotgetMarketStats- Request market statistics
connected- Connection establishedsubscribed- Subscription confirmedorderbook- Orderbook snapshot/updatetrade- Trade executionmarketStats- Market statisticspong- Heartbeat responseerror- Error message
orderbook-server/
├── src/
│ ├── data-structures/ # Efficient orderbook data structures
│ │ └── PriceLevelTree.ts
│ ├── orderbook/ # Core trading engine
│ │ ├── Orderbook.ts
│ │ └── OrderbookManager.ts
│ ├── redis/ # Redis pub/sub service
│ │ └── RedisService.ts
│ ├── websocket/ # WebSocket server
│ │ └── WebSocketServer.ts
│ ├── index.ts # Main server
│ └── types.ts # TypeScript definitions
├── Dockerfile
├── package.json
└── tsconfig.json
npm run build # Build TypeScript
npm run start # Start production server
npm run dev # Start development server
npm run watch # Start with auto-reloadHTTP_PORT=3000 # HTTP server port
WS_PORT=3001 # WebSocket server port
REDIS_URL=redis://localhost:6379 # Redis connection URL- Order Matching: O(log n) operations using price-level trees
- WebSocket: Efficient event broadcasting to subscribed clients
- Redis: High-performance message queuing and pub/sub
- Memory: Optimized data structures for minimal memory usage
Access real-time statistics:
curl http://localhost:3000/healthReturns:
- Service status
- Redis connectivity
- WebSocket connections
- Active orderbooks
This enhanced orderbook server provides a solid foundation for building a complete trading exchange. Future enhancements could include:
- User Authentication: JWT-based authentication system
- Database Integration: Persistent order and trade history
- Risk Management: Position limits, margin trading
- Market Making: Automated liquidity provision
- Advanced Orders: Stop-loss, take-profit, trailing stops
- Frontend Interface: React/Next.js trading dashboard
This project is for educational and demonstration purposes.