Skip to content

A practical client-server orderbook for learning purposes

License

Notifications You must be signed in to change notification settings

Reidmen/LostExchangeSystem

Repository files navigation

CodeBase MIT Black

LostExchangeSystem

A Lost Stock Exchange System trying to find its way back! Implements a Server/Client protocol for creating orders and storing them in an efficient way. As a toy example, its purpose is for learning the mechanism of book keeping.

Run Server

To run a server, you must specify the host ip address and the port. To execute it on the localhost 127.0.0.1 with port 10001, use:

cd lostexchangesystem/
python3 Server.py 127.0.0.1 10001 

Interaction

We assume the server is running in the local host 127.0.0.1 and port 10001. In order to interact with the server, use:

nc 127.0.0.1 10001

Add Orders

Orders can be added to the queue by using the add method. To add an order, e.g. 10 long on APPL at strike price $130, use:

/ADD Order(APPL, LONG, 10, 130.0) 

The server will prompt a digested hash of the order information, useful to cancel it.

Cancel Orders

Order can be removed from the queue by using the cancel method. To cancel an order its enough to specify the hash, use (example hash):

/CANCEL Order(0ecbb9115ef1380b2f7194f84d38a776)

Ideas

  • Introduce the Account, Member and Order classes that define a brokage system.
  • Use python for high-level implementation of them.
  • Overview of the Exchange system is provided in the lostexchangesystem/ folder.
  • Use c++ for implementation of the order book, and pybind11 for python usage.
  • Other attempts will be given in ideas/ folder.

How to build a Fast Limit Order Book

The Nasdaq TotalView ITCH feed, which is every event in every instrument traded in the Nasdaq, can have data rates of 20+ GB/day with spikes of 3 MB/sec or more. Each message averages about 20 bytes each so this means handling 100,000 - 200,000 messsages per second during high volume periods ref.

A (fast) limit order book must implement three primary functions.

  1. Add: Places order at the end of a list or orders to be executed a limit price.

  2. Cancel: Remove an arbitrary order.

  3. Execute: Remove an order from the inside of the book, inside meaning the oldest buy at highest price and oldest sell and lowest price.

These operations should be implemented in $O(1)$ time while making it possible for the trading model to efficiently ask questions lie what are the best bid and offer? or what is order x's current position?.

Python style

Under MIT License

Releases

No releases published

Packages

No packages published