Skip to content

kaijun123/TradingSystem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Trading System

This work attempts to create a working version of a trading system.

Choice of language

  • This work is done in Python, even though a lower level language is more ideal, simply because I wanted to gain more hands-on experience with python development.
  • Implementation in a lower level language will likely come at a later time.

Limit Order Book Design Overview:

  • Reference: https://github.com/Kautenja/limit-order-book/blob/master/notes/engines/engine.c

  • In this implementation, the limit order book is represented using a map (pricePoints) which contains a mapping of price to an instance of PricePoint.

    • PricePoint stores a linked list of outstanding buy/sell orders in the order book at the respective price.
    • The orders in the PricePoint are sorted in chronological order (ie Price-Time basis).
  • Each outstanding limit order is represented by an instance of struct Order.

  • askMin and bidMax are variables that maintain starting points, at which the matching algorithm initiates its search.

    • askMin holds the lowest price that contains at least one outstanding sell order.
    • Analogously, bidMax represents the maximum price point that contains at least one outstanding buy order.
  • Matching Strategy:

    • When a Buy order arrives, we search the book for outstanding Sell orders that cross with the incoming order. We start the search at askMin and proceed upwards, incrementing askMin until:

      • a) The incoming Buy order is filled.
      • b) We reach a price point that no longer crosses with the incoming limit price (askMin > BuyOrder.price) In case b), we create a new orderBookEntry to record the remainder of the incoming Buy order and add it to the global order book by appending it to the linked list at pricePoints[BuyOrder.price].
    • Incoming Sell orders are handled analogously, except that we start at bidMax and proceed downwards.

  • Rather than storing the orders in a DB, it is stored in-memory to reduce latency.

  • The code is written intentionally such that adding and cancelling each individual order is O(1) time complexity. Though executing the trade will have an average of O(n) time complexity

  • Cancellation of orders is done by setting the amount to 0, to prevent the need to shift the pointers. This especially crucial since the adding and cancelling orders are the most frequent operations, with successful trades being only a small fraction in a working stock exchange.

TODO

  • Correctness Test, Fuzz Test for limit order book
  • Add in Market Order functionalities
  • Think about stop loss/ take profit mechanisms
  • Add in TCP/ UDP server to form a fully-functional server/ queue consumer depending on the architecture

About

Order Book implementation with Price/Time priority

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages