{"id":13812894,"url":"https://github.com/Crypto-toolbox/HFT-Orderbook","last_synced_at":"2025-05-14T22:31:14.624Z","repository":{"id":41142685,"uuid":"98400312","full_name":"Crypto-toolbox/HFT-Orderbook","owner":"Crypto-toolbox","description":"Limit Order Book for high-frequency trading (HFT), as described by WK Selph, implemented in Python3 and C","archived":false,"fork":false,"pushed_at":"2023-08-02T16:47:12.000Z","size":298,"stargazers_count":892,"open_issues_count":8,"forks_count":253,"subscribers_count":46,"default_branch":"master","last_synced_at":"2024-02-15T15:33:57.019Z","etag":null,"topics":["avl-tree","bst","c","doubly-linked-list","high-frequency-trading","limit-order-book","order-management","orderbook","python3","self-balancing-trees"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Crypto-toolbox.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-07-26T08:42:19.000Z","updated_at":"2024-02-15T06:43:00.000Z","dependencies_parsed_at":"2024-01-13T15:00:22.308Z","dependency_job_id":null,"html_url":"https://github.com/Crypto-toolbox/HFT-Orderbook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Crypto-toolbox%2FHFT-Orderbook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Crypto-toolbox%2FHFT-Orderbook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Crypto-toolbox%2FHFT-Orderbook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Crypto-toolbox%2FHFT-Orderbook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Crypto-toolbox","download_url":"https://codeload.github.com/Crypto-toolbox/HFT-Orderbook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254239522,"owners_count":22037721,"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","bst","c","doubly-linked-list","high-frequency-trading","limit-order-book","order-management","orderbook","python3","self-balancing-trees"],"created_at":"2024-08-04T04:00:57.469Z","updated_at":"2025-05-14T22:31:09.604Z","avatar_url":"https://github.com/Crypto-toolbox.png","language":"C","readme":"# HFT-Orderbook\nLimit Order Book for high-frequency trading (HFT), as described by WK Selph, implemented in C.\n\nBased on WK Selph's Blogpost:\n\nhttp://howtohft.wordpress.com/2011/02/15/how-to-build-a-fast-limit-order-book/\n\nAvailable at Archive.org's WayBackMachine:\n\nhttps://goo.gl/KF1SRm\n\n\n    \"There are three main operations that a limit order book (LOB) has to\n    implement: add, cancel, and execute.  The goal is to implement these\n    operations in O(1) time while making it possible for the trading model to\n    efficiently ask questions like “what are the best bid and offer?”, “how much\n    volume is there between prices A and B?” or “what is order X’s current\n    position in the book?”.\n\n    The vast majority of the activity in a book is usually made up of add and\n    cancel operations as market makers jockey for position, with executions a\n    distant third (in fact I would argue that the bulk of the useful information\n    on many stocks, particularly in the morning, is in the pattern of adds and\n    cancels, not executions, but that is a topic for another post).  An add\n    operation places an order at the end of a list of orders to be executed at\n    a particular limit price, a cancel operation removes an order from anywhere\n    in the book, and an execution removes an order from the inside of the book\n    (the inside of the book is defined as the oldest buy order at the highest\n    buying price and the oldest sell order at the lowest selling price).  Each\n    of these operations is keyed off an id number (Order.idNumber in the\n    pseudo-code below), making a hash table a natural structure for tracking\n    them.\n\n    Depending on the expected sparsity of the book (sparsity being the\n    average distance in cents between limits that have volume, which is\n    generally positively correlated with the instrument price), there are a\n    number of slightly different implementations I’ve used.  First it will help\n    to define a few objects:\n\n        Order\n          int idNumber;\n          bool buyOrSell;\n          int shares; // order size\n          int limit;\n          int entryTime;\n          int eventTime;\n          Order *nextOrder;\n          Order *prevOrder;\n          Limit *parentLimit;\n\n        Limit  // representing a single limit price\n          int limitPrice;\n          int size;\n          int totalVolume;\n          Limit *parent;\n          Limit *leftChild;\n          Limit *rightChild;\n          Order *headOrder;\n          Order *tailOrder;\n\n        Book\n          Limit *buyTree;\n          Limit *sellTree;\n          Limit *lowestSell;\n          Limit *highestBuy;\n\n    The idea is to have a binary tree of Limit objects sorted by limitPrice,\n    each of which is itself a doubly linked list of Order objects.  Each side\n    of the book, the buy Limits and the sell Limits, should be in separate trees\n    so that the inside of the book corresponds to the end and beginning of the\n    buy Limit tree and sell Limit tree, respectively.  Each order is also an\n    entry in a map keyed off idNumber, and each Limit is also an entry in a\n    map keyed off limitPrice.\n\n    With this structure you can easily implement these key operations with\n    good performance:\n\n    Add – O(log M) for the first order at a limit, O(1) for all others\n    Cancel – O(1)\n    Execute – O(1)\n    GetVolumeAtLimit – O(1)\n    GetBestBid/Offer – O(1)\n\n    where M is the number of price Limits (generally \u003c\u003c N the number of orders).\n    Some strategy for keeping the limit tree balanced should be used because the\n    nature of markets is such that orders will be being removed from one side\n    of the tree as they’re being added to the other.  Keep in mind, though,\n    that it is important to be able to update Book.lowestSell/highestBuy\n    in O(1) time when a limit is deleted (which is why each Limit has a Limit\n    *parent) so that GetBestBid/Offer can remain O(1).\"\n","funding_links":[],"categories":["C"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCrypto-toolbox%2FHFT-Orderbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCrypto-toolbox%2FHFT-Orderbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCrypto-toolbox%2FHFT-Orderbook/lists"}