This project is a simulation of a stock exchange server, designed and implemented in Rust. It uses tonic
-based gRPC to handle requests and manage connections. The project includes a server for processing requests, investor clients for sending orders, and subscribers for receiving market data.
- Stock Exchange Server: Accepts and processes RPC requests, logs activities to the console.
- Investor Client: Tests server connections through RPC, reads instructions from a file and sends them as RPC order requests.
- Subscriber Client: Subscribes to the server for market data, receives historical and incremental events.
- bin_server: Entrypoint to start the server.
- server: Manages RPC connections and processes requests via the submodule portal.
- portal: Core logic processor for every request, outputs tasks for server dispatch.
- orderbook_manager: Manages an order book for each ticker.
- event_history: Manages event logs.
- order_info: Manages order details.
- account_manager: Manages a list of investors' accounts.
- stock_manager: Manages static stock information.
- The investor client is used for testing server connections and sending pre-set instructions.
- The subscriber client receives market data feed from the server.
To start a new server:
$ cargo run --bin server <investor config file> <stock list file>
To start a new subscriber:
$ cargo run --bin subscriber
To start an investor tester:
$ cargo run --bin investor <investor instructions>
Unit tests are implemented for orderbook
, order_info
, and event_history
. To test the system, run the server, investor, and subscriber clients in separate terminals.
The Stock Exchange Simulation project is structured to mimic real-world stock exchange operations, focusing on the following workflow:
- Server Initialization:
- The
StockExchangeServer
initializes with a predefined list of stocks and investors. - It launches two primary RPC services:
SendOrderService
for processing investor orders andSubscribeService
for managing market data subscriptions.
- The
- Request Processing:
- Upon receiving a
RpcXXXRequest
, the server parses it into a correspondingPortalRequest
and forwards it to thePortal
. - The
Portal
handles the request using its various components likeOrderbook
,EventHistory
,AccountManager
,StockManager
, andOrderInfo
.
- Upon receiving a
- Order Handling:
- New orders (
NewOrderRequest
) and order cancellations (CancelOrderRequest
) are validated and processed through theOrderbook
. - Generated
OrderbookLog
entries are converted intoPortalTasks
for state updates acrossEventHistory
,AccountManager
, andOrderInfo
.
- New orders (
- Response Generation:
- The server processes
PortalTasks
and converts them into appropriateRpcXXXResponse
messages, which are then dispatched to the relevant investor or subscriber sessions.
- The server processes
- Investor Sessions: Multiple logins to the same account are prevented, with new requests being rejected if an account is already active.
- Market Orders: It is assumed that market orders do not rest on the order book.
- Stock Uniqueness: Each stock ticker is unique within this exchange.
- Order Processing: Orders are assumed to have integer sizes and lot sizes. Market buy orders are matched with the most competitive (lowest) sell orders available, subject to investor's cash balance.
- Precision of Prices: The use of
f32
for representing prices may lead to precision issues in certain scenarios.
Run these commands in seperate terminals: