{"id":24879965,"url":"https://github.com/martishin/order-book","last_synced_at":"2025-03-27T05:16:04.750Z","repository":{"id":272091025,"uuid":"915502454","full_name":"martishin/order-book","owner":"martishin","description":"Basic order book implementation","archived":false,"fork":false,"pushed_at":"2025-01-12T22:31:47.000Z","size":29,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T10:18:07.711Z","etag":null,"topics":["clean-architecture","golang","matching-engine"],"latest_commit_sha":null,"homepage":"","language":"Go","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/martishin.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":"2025-01-12T02:31:13.000Z","updated_at":"2025-01-12T20:48:50.000Z","dependencies_parsed_at":"2025-01-12T03:25:59.589Z","dependency_job_id":"7b5ba498-5863-4a96-82a6-b30f577369e4","html_url":"https://github.com/martishin/order-book","commit_stats":null,"previous_names":["martishin/order-book"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martishin%2Forder-book","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martishin%2Forder-book/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martishin%2Forder-book/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martishin%2Forder-book/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/martishin","download_url":"https://codeload.github.com/martishin/order-book/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245785821,"owners_count":20671634,"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":["clean-architecture","golang","matching-engine"],"created_at":"2025-02-01T10:18:11.300Z","updated_at":"2025-03-27T05:16:04.724Z","avatar_url":"https://github.com/martishin.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Order Book\n\nAn exchange allows the buyers and sellers of a product to discover each other and trade. Buyers and sellers (traders)\nsubmit orders to the exchange, and the exchange applies simple rules to determine if a trade has occurred. The dominant\nkind of exchange is a central limit order book (CLOB) where orders are matched using ‘price time priority’.\n\nWhen placing an order, traders specify if they wish to buy or sell, the limit price i.e., the worst possible price they will\ntrade at, and the quantity (number of shares) they wish to trade. On our exchange trades only occur during the\nprocessing of a newly posted order, and happen immediately, which is known as ‘continuous trading’.\n\n**Run Locally**: `make run \u003c test1.txt`  \n**Run tests**: `make test` \n\n### Matching example\n\nAs orders arrive at the exchange, they are considered for aggressive matching first against the opposite side of the\nbook.\nOnce this is completed, any remaining order quantity will rest on their own side of the book.\nConsider 3 orders\nhave been submitted to the exchange, in the following order:\n\n● Buy 1000 @ 99\n\n● Buy 1200 @ 98\n\n● Buy 500 @ 99\n\nAs there are no Sell orders, yet, they rest on the order book as follows (note Buy for 98 is lowest priority):\n\n| Bids (Buying) | Bids (Buying) | Asks (Selling) | Asks (Selling) |\n|---------------|---------------|----------------|----------------|\n| Quantity      | Price         | Price          | Quantity       |\n| 1000          | 99            |                |                |\n| 500           | 99            |                |                |\n| 1200          | 98            |                |                |\n\nPrice time priority refers to the order in which orders in the book are eligible to be matched during the aggressive\nphase. Orders are first matched in order of the price (most aggressive to least aggressive), then by arrival time into the\nbook (oldest to newest). A **Sell** order is now submitted, with a limit price that does not cross with any of the\nexisting resting orders:\n\n● Sell 2000 @ 101\n\n| Bids (Buying) | Bids (Buying) | Asks (Selling) | Asks (Selling) |\n|---------------|---------------|----------------|----------------|\n| Quantity      | Price         | Price          | Quantity       |\n| 1000          | 99            | 101            | 2000           |\n| 500           | 99            |                |                |\n| 1200          | 98            |                |                |\n\nA **Sell** order is now submitted that is aggressively-priced:\n\n● Sell 2000 @ 95\n\nThis triggers a matching event as there are orders on the **Buy** side that match with the new **Sell** order.\n\nThe orders are matched in price time priority (first by price, then by arrival time into the book) i.e.\n\n`    ● Buy 1000 @ 99 is matched first (as it is the oldest order at the highest price level)`\n\n`    ● Buy 500 @ 99 is matched second (as it is at the highest price level and arrived after the BUY 1000 @ 99 order)`\n\n`    ● Buy 500 @ 98 is matched third (as it is at a lower price. This partially fills the resting order of 1200, leaving 700 in the order book)`\n\n| Bids (Buying) | Bids (Buying) | Asks (Selling) | Asks (Selling) |\n|---------------|---------------|----------------|----------------|\n| Quantity      | Price         | Price          | Quantity       |\n| 700           | 98            | 101            | 2000           |\n\n### Limit order handling\n\nThe task is to produce executable code that will accept orders from standard input, and to emit to standard output\nthe trades as they occur. Once standard input ends, the program should print the final contents of the order book.\n\nOrder inputs will be given as a comma separated values, one order per line of the input, delimited by a new line\ncharacter.\nThe fields are: order-id, side, price, quantity.\nSide will have a value of ‘B’ for **Buy** or ‘S’ for **sale\n**.\nPrice and quantity will both be integers.\norder-id should be handled as a string.\n\nExample 1\n\nIn this example, no buyer or seller is willing to pay the opposing price, so no trades occur\n\n``` text\n$ cat test1.txt \n10000,B,98,25500\n10005,S,105,20000\n10001,S,100,500\n10002,S,100,10000\n10003,B,99,50000\n10004,S,103,100\n$ ./exchange \u003c test1.txt\n     50,000     99 |    100         500\n     25,500     98 |    100      10,000\n                   |    103         100 \n                   |    105      20,000\n$\n```    \nMD5 (output) = 8ff13aad3e61429bfb5ce0857e846567\n\n\nWhich represents the following order book:\n\n| Bids (Buying) | Bids (Buying) | Asks (Selling) | Asks (Selling) |\n|---------------|---------------|----------------|----------------|\n| Quantity      | Price         | Price          | Quantity       |\n| 50000         | 99            | 100            | 500            |\n| 25500         | 98            | 100            | 10000          |\n|               |               | 103            | 100            |\n|               |               | 105            | 20000          |\n\nExample 2\n\nIf an order is then submitted to Buy 16000 @ 105p, it will fill completely against the resting orders, producing the\nfollowing output:\n\n``` text\n$ cp test1.txt test2.txt\n$ echo \"10006,B,105,16000\" \u003e\u003e test2.txt \n$ ./exchange \u003c test2.txt\ntrade 10006,10001,100,500\ntrade 10006,10002,100,10000\ntrade 10006,10004,103,100\ntrade 10006,10005,105,5400\n      50,000     99 |    105      14,600\n      25,500     98 |\n$\n```  \n\nMD5 (output) = ce8e7e5ab26ab5a7db6b7d30759cf02e\n\nTrade output must indicate the aggression order-id, the resting order-id, the price of the match and the quantity\ntraded, followed by a newline.\n\nThe order book output should be formatted to a fixed width using the following format:\n\n000,000,000 000000 | 000000 000,000,000\n\nIf a value is too small to cover the whole reserved area, it should be left padded with spaces\n\nPlease note that once submitted, orders are not modified by further input. There is no need to maintain more than one\norder book, all orders are for the same product.\n\n### Constraints\nQuantities bigger than 999,999,999 will not be provided as input.  \nPrices bigger than 999,999 will not be provided as input.  \nWidth truncation of the output will never be required.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartishin%2Forder-book","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartishin%2Forder-book","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartishin%2Forder-book/lists"}