{"id":19389526,"url":"https://github.com/lpthong90/python-order-matching-engine","last_synced_at":"2025-02-24T20:05:31.393Z","repository":{"id":223100173,"uuid":"759312066","full_name":"lpthong90/python-order-matching-engine","owner":"lpthong90","description":"A demo project is a simple order matching engine which using double linked list and AVL tree.","archived":false,"fork":false,"pushed_at":"2024-03-03T10:31:08.000Z","size":171,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-07T09:35:09.896Z","etag":null,"topics":["avl-tree","matching-engine","python","python3","tree"],"latest_commit_sha":null,"homepage":"https://lpthong90.dev/python-order-matching-engine/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lpthong90.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-18T08:29:53.000Z","updated_at":"2024-03-03T10:32:08.000Z","dependencies_parsed_at":"2024-03-03T11:30:48.976Z","dependency_job_id":"19901fb1-e29c-42d4-a1f0-9796f22ceca3","html_url":"https://github.com/lpthong90/python-order-matching-engine","commit_stats":null,"previous_names":["lpthong90/python-order-matching-engine"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpthong90%2Fpython-order-matching-engine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpthong90%2Fpython-order-matching-engine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpthong90%2Fpython-order-matching-engine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpthong90%2Fpython-order-matching-engine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lpthong90","download_url":"https://codeload.github.com/lpthong90/python-order-matching-engine/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240549430,"owners_count":19819137,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avl-tree","matching-engine","python","python3","tree"],"created_at":"2024-11-10T10:16:29.863Z","updated_at":"2025-02-24T20:05:31.369Z","avatar_url":"https://github.com/lpthong90.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://lpthong90.dev/python-order-matching-engine\"\u003e\u003cimg style=\"width:300px\" src=\"https://lpthong90.dev/python-order-matching-engine/assets/images/thumbnail.jpg\" alt=\"Order Matching Engine\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n    \u003cem\u003eA demo project is a simple order matching engine which using double linked list and AVL tree.\u003c/em\u003e\n\u003c/p\u003e\n\n---\n\n**Documentation**: \u003ca href=\"https://lpthong90.dev/python-order-matching-engine\" target=\"_blank\"\u003ehttps://lpthong90.dev/python-order-matching-engine\u003c/a\u003e\n\n**Source  Code**: \u003ca href=\"https://github.com/lpthong90/python-order-matching-engine\" target=\"_blank\"\u003ehttps://github.com/lpthong90/python-order-matching-engine\u003c/a\u003e\n\n---\n\n# Ideas\n\nOrder books involve three primary actions: adding a new order, canceling an existing order, and executing an order. \n\nWhen adding a new order, the system searches for the corresponding price level and then either adds the order to an existing level or initializes a new one. \n\nWhen canceling an order, the system searches for and removes it from the relevant price level's order list.\n\nWhen executing an order, the system searches for all orders on the opposite side that match it. If matching orders exist, the system reduces the volume size of the order and retries until no matching orders remain. If an order is only partially filled, a new price level is created for the remaining volume.\n\nPrice levels without any related orders are removed to maintain efficiency in the order book.\n\n# Diagrams\n\nModel diagram:\n\n\u003cimg src=\"/assets/images/diagram.png\" alt=\"filter_options\"/\u003e\n\n[//]: # (Add new order flow diagram:)\n\n[//]: # ()\n[//]: # (Add cancel order flow diagram:)\n\n[//]: # ()\n[//]: # (Add execute order flow diagram:)\n\n\n# Models\n``` python\nclass Order:\n    def __init__(self, id: ID_TYPE, price: float, volume: float, side: SideType):\n        self.id: ID_TYPE = id\n        self.price: float = price\n        self.volume: float = volume\n        self.origin_volume = volume\n        self.side: SideType = side  # 'BUY' 'SELL'\n\n        self.price_level: Optional[PriceLevel] = None\n\nclass PriceLevel:\n    def __init__(self, price: float):\n        self.price: float = price\n        self.total_volume: float = 0\n        self.orders: LinkedList[int, Order] = LinkedList[int, Order]()\n\nclass PriceLevelAVLTree(AVLTree):\n    def __init__(self):\n        super().__init__()\n\nclass OrderBook:\n    def __init__(self):\n        self.bids_tree: PriceLevelAVLTree = PriceLevelAVLTree()\n        self.asks_tree: PriceLevelAVLTree = PriceLevelAVLTree()\n        self.best_bid_price_level: Optional[PriceLevel] = None\n        self.best_ask_price_level: Optional[PriceLevel] = None\n\n        self.price_levels: Dict = {}\n\nclass MatchingEngine:\n    def __init__(self, order_book: Optional[OrderBook]):\n        self.order_book = order_book or OrderBook()\n\n        self.orders: Dict[int, Order] = {}\n        self.filled_orders: Dict[int, Order] = {}\n```\n\n\n# Complexity\n\nAssume with `m` is number of price levels, `n` is number orders in each price level, `l` is number of matched price levels; then we have:\n\n- Add new order:\n  - `O(log(m))` if related price level isn't existed\n  - `O(1)` if otherwise\n- Cancel order:\n  - `O(log(m))` if related price level is removed\n  - `O(1)` if otherwise\n- Execute order: \n  - `O(n * l * log(m))`\n\n# References\nhttps://web.archive.org/web/20110219163448/http://howtohft.wordpress.com/2011/02/15/how-to-build-a-fast-limit-order-book/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flpthong90%2Fpython-order-matching-engine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flpthong90%2Fpython-order-matching-engine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flpthong90%2Fpython-order-matching-engine/lists"}