This project is a basic implementation of an order book system, simulating a financial order book used in markets (stocks, commodities, or cryptocurrency). It supports both market and limit orders, allowing you to view and manage the order book through a command-line interface.
- Market Orders: Execute orders immediately at the best available price.
- Limit Orders: Place orders to buy or sell at a specified price.
- Bid and Ask Sides: Track both buy (bid) and sell (ask) orders.
- Orderbook Visualization: View the current state of the order book, showing best bid and ask prices.
- Order Matching: Market orders match with the best available prices, while limit orders remain in the order book until filled or canceled.
main.cpp
: The main program that handles user interaction, including submitting orders and displaying the order book.orderbook.cpp
: Implements the core functionality of the order book, including order handling, matching, and printing.order.hpp
: Defines theOrder
struct, which represents individual orders.orderbook.hpp
: Defines theOrderbook
class that manages the order book and order matching.print.hpp
: Contains functions for printing the order book and transaction details (e.g., filled orders).test.cpp
: Contains unit tests that verify the order book’s functionality.Makefile
: A build automation file for compiling the project and running tests.
-
Clone the repository:
git clone https://github.com/Ayush272002/Orderbook.git cd Orderbook
-
Install dependencies (if needed): This project requires a C++17 compatible compiler (e.g.,
g++
). -
Build the project: To compile the main program and tests, simply run:
make
-
Run the main program: After building, you can run the program using:
make run
-
Run the tests: To run the unit tests and verify the order book functionality, use:
make run_tests
Once the program is running, you will be presented with a menu to interact with the order book:
- Print Orderbook: Displays the current state of the order book, including the best bid and ask prices, and their corresponding quantities.
- Submit Order: Allows you to submit a new order (either market or limit), specifying quantity, price, and side (buy or sell).
Options
————————————————————
|1. Print Orderbook|
|2. Submit Order |
————————————————————
Choice: 2
Enter order type:
0. Market order
1. Limit order
Selection: 1
Enter side:
0. Buy
1. Sell
Selection: 0
Enter order quantity: 100
Enter limit price: 101.50
Submitting limit buy order for 100 units @ $101.50..
- Market Orders: The system will attempt to fill market orders immediately at the best available price from the order book.
- Limit Orders: If the order cannot be matched immediately, it will be placed in the order book and will remain there until a matching order is found or the order is canceled.
When a limit order is filled, the system will output the following:
Filled 100/100 units @ $101.50 average price. Time taken: 230 nano seconds
To view the current order book, you can select the Print Orderbook option from the menu, which will display the bid (buy) and ask (sell) prices, as well as the quantities for each price level.
This project includes unit tests to verify key functionalities of the order book system. The tests are written in C++ and can be executed with the following command:
make run_tests
The tests include:
testAddOrder
: Verifies that orders are correctly added to the order book.testMarketOrderExecution
: Ensures that market orders are filled at the best available price.testLimitOrderExecution
: Tests that limit orders are executed only when a matching order is available.testBestQuote
: Ensures that the best bid and ask prices are correctly retrieved.
- Order Cancellations: Add functionality for canceling orders from the order book.
- Advanced Order Types: Implement additional order types (e.g., Stop-Loss, Take-Profit).
- Performance Optimizations: Optimize the order book for larger volumes of orders, improving speed and efficiency.
- Web Interface: Create a web-based interface to provide a more user-friendly way to interact with the system.
- Fork this repository.
- Create your feature branch (
git checkout -b feature-name
). - Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature-name
). - Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.