This project implements a real-time, low-latency exchange gateway emulator in Python, designed to simulate a high-frequency trading environment. The emulator is architected to handle large volumes of orders with minimal latency, making it suitable for performance testing and strategy development.
-
WebSocket Server: The server is built using Python's
websockets
library, enabling efficient real-time communication with multiple clients. It handles incoming order requests, processes them, and sends back confirmations or rejections. The server is designed to be non-blocking, leveragingasyncio
for concurrent operations. -
Order Matching Engine: The core of the emulator, implemented in
src/order_matching.py
, maintains an in-memory order book. It uses efficient data structures to store and match buy and sell orders. The engine supports various order types and matching algorithms, ensuring high throughput and low latency. -
Client Simulation: The client component, found in
src/client.py
, acts as a trading participant. It can generate random orders or be configured to follow specific trading strategies. The client connects to the server via WebSockets, sending orders and receiving trade confirmations. -
Real-Time Dashboard: The dashboard, implemented using Streamlit, provides a visual interface for monitoring the order book and key performance metrics. It updates in real-time, offering insights into order flow, latency, and throughput.
-
Asynchronous Processing: Utilizes Python's
asyncio
to handle multiple client connections and order processing concurrently, reducing latency and improving scalability. -
Performance Profiling: Integrated with
cProfile
for detailed performance analysis, allowing users to identify bottlenecks and optimize the system. -
Comprehensive Testing: Includes unit tests and stress tests to ensure reliability and performance under load. The stress tests simulate hundreds of concurrent clients to evaluate system robustness.
Here's an overview of the project's file structure:
.
├── src
│ ├── server.py
│ ├── order_matching.py
│ └── client.py
├── dashboards
│ └── order_book_dashboard.py
├── tests
│ ├── stress_test.py
│ ├── test_client_server.py
│ └── test_order_matching.py
├── README.md
├── requirements.txt
└── latency-optimized-exchange-gateway-emulator
└── requirements.txt
- Python 3.8+
- websockets: For real-time bidirectional communication
- asyncio: For asynchronous programming
- Streamlit: For real-time data visualization
- cProfile: For performance profiling
- pytest: For unit testing
-
Clone the repository:
git clone https://github.com/moebqr/exchange-gateway-emulator.git cd exchange-gateway-emulator
-
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install the required dependencies:
pip install -r requirements.txt
-
Start the server:
python src/server.py
-
In a separate terminal, start the client:
python src/client.py
-
(Optional) Start the dashboard:
streamlit run dashboards/order_book_dashboard.py
The client automatically generates and submits random orders. To submit a custom order, modify the generate_random_order
method in src/client.py
or create a new method to generate orders based on your specific requirements.
Trade confirmations are printed to the console by the client. You can also monitor the server logs for more detailed information about order processing and matching.
To run the unit tests, execute the following command from the project root:
python -m pytest tests/test_order_matching.py tests/test_client_server.py
To run the stress test, use the following command:
python tests/stress_test.py
This will simulate 500 concurrent clients sending orders to the server and measure throughput and latency.
Here are some sample performance metrics from our stress tests:
- Throughput: ~6000 orders/second
- Average Latency: 10 ms
- Median Latency: 8 ms
- 95th Percentile Latency: 20 ms
Note: Actual performance may vary depending on hardware and network conditions.
The real-time dashboard provides a visual representation of the order book and key performance metrics. Here's what you can expect to see:
This chart shows the current state of the order book for a given symbol (e.g., AAPL). Buy orders are represented in green, and sell orders in red. The x-axis represents the price, while the y-axis shows the quantity at each price level.
The dashboard also displays key performance metrics in real-time:
- Average Latency: The average time taken to process an order.
- Order Throughput: The number of orders processed per second.
- Total Trades: The cumulative number of executed trades.
To access the dashboard, start the Streamlit server and navigate to the provided URL (typically http://localhost:8501
) in your web browser.
Contributions to this project are welcome! Please fork the repository and submit a pull request with your proposed changes.
This project is licensed under the MIT License - see the LICENSE file for details.