A high-performance, scalable trading engine simulation built in C# .NET 8.0 that models markets.
- Efficient order book management using advanced data structures (SortedSet, Dictionary)
- Support for multiple matching algorithms (FIFO and Pro-rata)
- Real-time order matching and trade execution
- Scalable architecture capable of handling multiple financial instruments
- Unit and integration testing suite
- Custom logger with text and console logging capabilities
The trading engine is built on a modular architecture:
Orderbook: Manages the order book for each financial instrumentMatchingEngine: Implements order matching algorithms (FIFO and Pro-rata)Order: Represents individual orders in the systemTrade: Represents executed tradesSecurity: Represents financial instruments
-
Clone the repository:
-
Open the solution in Visual Studio or your preferred C# IDE.
-
Restore NuGet packages.
-
Build the solution.
To use the trading engine in your project (see integration tests and TradingEngineServer class for additional examples):
-
Initialize the
OrderbookManagerandMatchingEngine:var orderbookManager = new OrderbookManager(); var matchingEngine = new FIFOMatchingEngine(); // or ProRataMatchingEngine
-
Create a security and add it to the system:
var security = new Security(1, "AAPL"); orderbookManager.CreateOrderbook(security, matchingEngine);
-
Add orders to the orderbook:
var orderbook = orderbookManager.GetOrderbook(security); var buyOrder = new Order(new OrderCore(1, "user1", security.Id), 100, 10, true); orderbook.AddOrder(buyOrder);
-
Perform matching:
var matchResult = orderbook.Match();
-
Process the match results:
foreach (var trade in matchResult.Trades) { Console.WriteLine($"Trade executed: {trade.Quantity} @ {trade.Price}"); }
Run the included unit and integration tests to verify the system's functionality:
dotnet test
- Implement additional matching algorithms
- Add support for market orders and other order types
- Integrate with a real-time market data feed
- Implement risk management features
- Add a RESTful API for external integrations