This project is part of a complete end-to-end trading system:
- Main Repository: fpga-trading-systems
- Project Number: 9 of 30
- Category: C++ Application
- Dependencies: Project 8 (Order Book - UART output)
Platform: Windows/Linux Technology: C++17, Boost.Asio, MQTT (libmosquitto), Kafka (librdkafka) Status: Functional - Performance Testing in Progress
The C++ Order Gateway is the middleware layer of the FPGA trading system, acting as a bridge between the FPGA hardware and multiple application clients. It reads BBO (Best Bid/Offer) data from the FPGA via UART and distributes it to multiple protocols simultaneously.
Data Flow:
FPGA Order Book (UART) → C++ Gateway → TCP/MQTT/Kafka → Applications
┌──────────────────────────────────────────────────────────┐
│ C++ Order Gateway │
│ │
│ ┌────────────────┐ ┌──────────────────────────┐ │
│ │ UART Reader │────→│ BBO Parser │ │
│ │ (Async I/O) │ │ (Hex → Decimal) │ │
│ │ 115200 baud │ │ │ │
│ └────────────────┘ └──────────┬───────────────┘ │
│ │ │
│ ↓ │
│ ┌──────────────────┐ │
│ │ Thread-Safe │ │
│ │ BBO Queue │ │
│ └─────────┬────────┘ │
│ │ │
│ ┌────────────────────────┼────────────────┐ │
│ ↓ ↓ ↓ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐│
│ │ TCP Server │ │ MQTT Publisher│ │Kafka Producer││
│ │ localhost │ │ Mosquitto │ │ ││
│ │ port 9999 │ │ 192.168.0.2 │ │ 192.168.0.203││
│ │ │ │ :1883 │ │ :9092 ││
│ │ JSON output │ │ v3.1.1 │ │ For future ││
│ └──────────────┘ └──────────────┘ └──────────────┘│
└──────────────────────────────────────────────────────────┘
| Protocol | Use Case | Clients | Status |
|---|---|---|---|
| TCP | Java Desktop (low-latency trading terminal) | JavaFX app | Active |
| MQTT | ESP32 IoT + Mobile App (lightweight, mobile-friendly) | ESP32 TFT + .NET MAUI | Active |
| Kafka | Future analytics, data persistence, replay | None yet | 📝 Reserved |
- Async serial port reading using Boost.Asio
- Baud rate: 115200
- Format: ASCII hex strings from FPGA
- Example input:
[BBO:AAPL ]Bid:0x002C46CC (0x0000001E) | Ask:0x002CE55C (0x0000001E) | Spr:0x00001F90
- Parses hex prices to decimal floating-point
- Extracts symbol, bid/ask prices, shares, spread
- Price scaling: divides by 10,000 (e.g.,
0x002C46CC→290.1708)
- Port: 9999 (configurable)
- Protocol: JSON over TCP
- Clients: Java desktop trading terminal
- Format:
{ "type": "bbo", "symbol": "AAPL", "timestamp": 1699824000123456789, "bid": { "price": 290.1708, "shares": 30 }, "ask": { "price": 290.2208, "shares": 30 }, "spread": { "price": 0.05, "percent": 0.017 } }
- Broker: Mosquitto @ 192.168.0.2:1883
- Protocol: MQTT v3.1.1 (for ESP32/mobile compatibility)
- Authentication: trading / trading123
- Topic:
bbo_messages - QoS: 0 (fire-and-forget for low latency)
- Clients: ESP32 IoT display, .NET MAUI mobile app
Why MQTT for IoT/Mobile?
- Lightweight protocol (low power consumption)
- Handles unreliable networks (WiFi/cellular)
- Low latency (< 100ms)
- Native support on ESP32 and mobile platforms
- No dependency issues on Android/iOS
- Broker: 192.168.0.203:9092
- Topic:
bbo_messages - Key: Symbol name (for partitioning)
- Status: Gateway publishes to Kafka, but no consumers implemented yet
Kafka Reserved for Future Use:
- Time-series database integration
- Historical replay for backtesting
- Analytics pipelines (Spark, Flink)
- Machine learning feature generation
- Microservices integration
Why NOT Kafka for mobile/IoT?
- ❌ Heavy protocol overhead (battery drain)
- ❌ Persistent TCP connections required
- ❌ Native library dependencies (Android issues)
- ❌ Designed for backend services, not edge devices
- Logs all BBO updates to CSV file
- Format:
timestamp,symbol,bid_price,bid_shares,ask_price,ask_shares,spread - Useful for debugging and offline analysis
Windows:
- Visual Studio 2019+ with C++17 support
- vcpkg package manager
Linux:
- GCC 7+ or Clang 5+
- CMake 3.15+
# Install vcpkg (if not already installed)
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh # or bootstrap-vcpkg.bat on Windows
./vcpkg integrate install
# Install dependencies
./vcpkg install boost-asio boost-system boost-thread
./vcpkg install nlohmann-json
./vcpkg install librdkafka
./vcpkg install mosquittoWindows (Visual Studio):
# Open solution in Visual Studio
# Build → Build Solution (Ctrl+Shift+B)
# Or use command line:
msbuild 09-order-gateway-cpp.sln /p:Configuration=ReleaseLinux (CMake):
mkdir build
cd build
cmake ..
make -j$(nproc)# Windows
order_gateway.exe COM3
# Linux
./order_gateway /dev/ttyUSB0# Custom TCP port
order_gateway.exe COM3 --tcp-port 9999
# Enable CSV logging
order_gateway.exe COM3 --csv-file bbo_log.csv
# Custom MQTT broker
order_gateway.exe COM3 --mqtt-broker mqtt://192.168.0.2:1883 --mqtt-topic bbo_messages
# Custom Kafka broker
order_gateway.exe COM3 --kafka-broker 192.168.0.203:9092 --kafka-topic bbo_messages
# All options combined
order_gateway.exe COM3 --tcp-port 9999 --csv-file bbo_log.csv --mqtt-broker mqtt://192.168.0.2:1883| Option | Description | Default |
|---|---|---|
uart_port |
Serial port (COM3, /dev/ttyUSB0) | Required |
--tcp-port |
TCP server port | 9999 |
--csv-file |
CSV log file path | None (disabled) |
--mqtt-broker |
MQTT broker URL | mqtt://192.168.0.2:1883 |
--mqtt-topic |
MQTT topic name | bbo_messages |
--kafka-broker |
Kafka broker URL | 192.168.0.203:9092 |
--kafka-topic |
Kafka topic name | bbo_messages |
┌──────────────┐
│ FPGA │ UART
│ Order Book │ @ 115200 baud
│ (8 symbols) │
└──────┬───────┘
│
↓ ASCII hex BBO messages
┌──────────────────────────────┐
│ C++ Order Gateway │
│ - Parse hex → decimal │
│ - Multi-protocol fanout │
└──┬────────┬────────┬─────────┘
│ │ │
│ │ └──→ [Kafka: Future Analytics]
│ │
│ └──→ [MQTT Broker: 192.168.0.2:1883]
│ ↓
│ ┌─────────┬──────────────┐
│ ↓ ↓ ↓
│ ESP32 Mobile App (Future IoT)
│ TFT .NET MAUI
│
└──→ [TCP: localhost:9999]
↓
Java Desktop
Trading Terminal
-
Java Desktop (TCP) - 12-java-desktop-trading-terminal/
- Live BBO table with charts
- Order entry with risk checks
- Real-time updates via TCP JSON stream
-
ESP32 IoT Display (MQTT) - 10-esp32-ticker/
- 1.8" TFT LCD color display
- Real-time ticker for trading floor
- Low power consumption
-
Mobile App (MQTT) - 11-mobile-app/
- .NET MAUI (Android/iOS/Windows)
- Real-time BBO monitoring
- Cross-platform support
- Analytics dashboard (time-series charts)
- Data archival service (InfluxDB, TimescaleDB)
- Backtesting engine (historical replay)
- ML feature pipeline (real-time + historical)
| Stage | Latency | Notes |
|---|---|---|
| UART Read | ~1-2 ms | Async I/O @ 115200 baud |
| BBO Parse | 10.67 µs avg | Hex → decimal (measured) |
| TCP Publish | ~10-50 µs | localhost |
| MQTT Publish | ~50-100 µs | LAN |
| Kafka Publish | ~100-200 µs | LAN |
| Total: FPGA → TCP | ~2-3 ms | End-to-end |
| Total: FPGA → MQTT | ~3-5 ms | End-to-end |
Measured Performance:
=== Project 9 (UART) Performance Metrics ===
Samples: 1,292
Avg: 10.67 μs
Min: 1.42 μs
Max: 86.14 μs
P50: 6.32 μs
P95: 26.33 μs
P99: 50.92 μs
StdDev: 9.82 μs
Test Conditions:
- Duration: 16.9 seconds
- Total messages: 7,000
- Average rate: 415 messages/second
- Errors: 0
- Max BBO rate: > 10,000 updates/sec
- Tested: 415 updates/sec (7,000 messages in 16.9 seconds)
- CPU usage: < 5% on modern CPU
The measured parse latency (10.67 µs avg) is consistent across both Project 9 (UART) and Project 14 (UDP), indicating that the parsing algorithm itself is the primary latency component, not the transport layer. The UART transport adds ~1-2ms overhead compared to UDP's ~10-50µs overhead, making UDP significantly faster for end-to-end latency.
09-order-gateway-cpp/
├── src/
│ ├── main.cpp # Entry point, argument parsing
│ ├── order_gateway.cpp # Main gateway orchestration
│ ├── uart_reader.cpp # Async UART reading (Boost.Asio)
│ ├── bbo_parser.cpp # Hex → decimal parser
│ ├── tcp_server.cpp # JSON TCP server
│ ├── mqtt.cpp # MQTT publisher (libmosquitto)
│ ├── kafka_producer.cpp # Kafka producer (librdkafka)
│ └── csv_logger.cpp # CSV file logging
├── include/
│ ├── order_gateway.h
│ ├── uart_reader.h
│ ├── bbo_parser.h
│ ├── tcp_server.h
│ ├── mqtt.h
│ ├── kafka_producer.h
│ └── csv_logger.h
├── vcpkg.json # Dependency manifest
└── CMakeLists.txt # Build configuration
| Component | Technology | Purpose |
|---|---|---|
| Language | C++17 | Modern C++ with STL |
| Async I/O | Boost.Asio 1.89+ | UART, TCP sockets |
| Threading | Boost.Thread | Multi-threaded architecture |
| JSON | nlohmann/json 3.11+ | TCP output serialization |
| MQTT | libmosquitto 2.0+ | IoT/mobile publish |
| Kafka | librdkafka 2.6+ | Future analytics |
| Logging | std::cout | Console output |
#define DEFAULT_MQTT_BROKER_URL "mqtt://192.168.0.2:1883"
#define DEFAULT_MQTT_CLIENT_ID "order_gateway"
#define DEFAULT_MQTT_USERNAME "trading"
#define DEFAULT_MQTT_PASSWORD "trading123"
#define DEFAULT_MQTT_TOPIC "bbo_messages"
#define DEFAULT_KAFKA_BROKER_URL "192.168.0.203:9092"
#define DEFAULT_KAFKA_CLIENT_ID "order_gateway"
#define DEFAULT_KAFKA_TOPIC "bbo_messages"- Baud Rate: 115200 (matches FPGA UART)
- Data Bits: 8
- Stop Bits: 1
- Parity: None
- Flow Control: None
Cause: Wrong port name or permissions
Solution:
# Windows: Check Device Manager → Ports (COM & LPT)
# Linux: Check available ports
ls /dev/ttyUSB* /dev/ttyACM*
# Linux: Add user to dialout group
sudo usermod -a -G dialout $USER
# Then log out and log back inCause: Mosquitto broker not running or wrong credentials
Solution:
# Test MQTT broker connectivity
mosquitto_sub -h 192.168.0.2 -p 1883 -t bbo_messages -u trading -P trading123 -v
# Check Mosquitto logs
sudo tail -f /var/log/mosquitto/mosquitto.logCause: Kafka broker not running or network issue
Solution:
# Test Kafka connectivity
kafka-console-consumer --bootstrap-server 192.168.0.203:9092 --topic bbo_messages
# Check Kafka status
systemctl status kafkaCause: FPGA not sending BBO updates
Solution:
- Check FPGA is receiving ITCH packets
- Verify UART connection (TX/RX/GND wired correctly)
- Check UART baud rate matches (115200)
- Use serial monitor to verify raw output from FPGA
Gateway Configuration:
UART Port: COM3
UART Baud: 115200
TCP Port: 9999
MQTT Broker: mqtt://192.168.0.2:1883
MQTT Topic: bbo_messages
Kafka Broker: 192.168.0.203:9092
Kafka Topic: bbo_messages
CSV File: (disabled)
Starting UART reader...
Starting TCP server on port 9999...
Connecting to MQTT broker...
Connecting to Kafka broker...
Gateway running. Press Ctrl+C to stop.
[BBO] AAPL Bid: $290.17 (30) | Ask: $290.22 (30) | Spread: $0.05 (0.02%)
[BBO] TSLA Bid: $431.34 (20) | Ask: $432.18 (25) | Spread: $0.84 (0.19%)
[BBO] SPY Bid: $322.96 (50) | Ask: $322.99 (50) | Spread: $0.03 (0.01%)
...
Published to:
- TCP: 1 client connected
- MQTT: success
- Kafka: success
Gateway complete and operational TCP client (Java Desktop) working MQTT clients (ESP32 + Mobile) working Kafka consumers not yet implemented
-
Kafka Consumer Services:
- Time-series database writer (InfluxDB, TimescaleDB)
- Analytics dashboard (Grafana, custom web UI)
- Historical data archival
-
Performance Optimizations:
- Zero-copy buffers for high-frequency data
- Lock-free queues for thread communication
- DPDK for kernel bypass (if needed)
-
Monitoring:
- Prometheus metrics export
- Health check endpoint
- Performance statistics logging
-
Reliability:
- Automatic reconnection for MQTT/Kafka
- Circuit breaker pattern
- Graceful degradation (continue if one protocol fails)
- 08-order-book/ - FPGA order book (data source)
- 10-esp32-ticker/ - ESP32 IoT display (MQTT client)
- 11-mobile-app/ - Mobile app (MQTT client)
- 12-java-desktop-trading-terminal/ - Java desktop (TCP client)
Build Time: ~30 seconds Hardware Status: Tested with FPGA UART output at 115200 baud