{"id":173,"date":"2025-06-19T02:55:05","date_gmt":"2025-06-19T02:55:05","guid":{"rendered":"https:\/\/blog.alltick.co\/ja-jp\/?p=173"},"modified":"2025-06-19T02:55:06","modified_gmt":"2025-06-19T02:55:06","slug":"a-complete-guide-to-the-order-matching-engine","status":"publish","type":"post","link":"https:\/\/blog.alltick.co\/ja-jp\/a-complete-guide-to-the-order-matching-engine\/","title":{"rendered":"\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a8\u30f3\u30b8\u30f3\u5b8c\u5168\u30ac\u30a4\u30c9"},"content":{"rendered":"\n<p>\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a8\u30f3\u30b8\u30f3\u306f\u3001\u3042\u3089\u3086\u308b\u53d6\u5f15\u30b7\u30b9\u30c6\u30e0\u306e\u4e2d\u6838\u3092\u62c5\u3046\u5b58\u5728\u3067\u3059\u3002\u30de\u30c3\u30c1\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3001\u30c7\u30fc\u30bf\u7ba1\u7406\u3001\u901a\u4fe1\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306a\u3069\u3001\u3044\u304f\u3064\u304b\u306e\u4e3b\u8981\u306a\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304b\u3089\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u305d\u306e\u4e3b\u306a\u5f79\u5272\u306f\u3001\u5e02\u5834\u53c2\u52a0\u8005\u306e\u58f2\u8cb7\u6ce8\u6587\u3092\u30de\u30c3\u30c1\u30f3\u30b0\u3057\u3001\u53d6\u5f15\u3092\u5b9f\u884c\u3057\u3001\u53d6\u5f15\u8a18\u9332\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u6bce\u65e5\u81a8\u5927\u306a\u6d41\u52d5\u6027\u304c\u91d1\u878d\u5e02\u5834\u3092\u901a\u3058\u3066\u79fb\u52d5\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u52b9\u7387\u7684\u306b\u5b9f\u884c\u3059\u308b\u306e\u304c\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a8\u30f3\u30b8\u30f3\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a8\u30f3\u30b8\u30f3\u304c\u306a\u3051\u308c\u3070\u3001\u53d6\u5f15\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\uff08FX\u3001\u5546\u54c1\u3001\u682a\u5f0f\u3001CFD\u306a\u3069\u8cc7\u7523\u306e\u7a2e\u985e\u3092\u554f\u308f\u305a\uff09\u306f\u5b9f\u969b\u306e\u53d6\u5f15\u3092\u6210\u7acb\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002\u5b9f\u969b\u3001\u53d6\u5f15\u30b7\u30b9\u30c6\u30e0\u5185\u306e\u4ed6\u306e\u3059\u3079\u3066\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u3001\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a8\u30f3\u30b8\u30f3\u3092\u652f\u3048\u308b\u305f\u3081\u306b\u5b58\u5728\u3059\u308b\u3068\u8a00\u3063\u3066\u3082\u904e\u8a00\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u306e\u7c21\u5358\u306a\u6b74\u53f2<\/strong><\/h2>\n\n\n\n<p>\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u3068\u3044\u3046\u6982\u5ff5\u306f\u300119\u4e16\u7d00\u306b\u9061\u308a\u307e\u3059\u3002\u5f53\u6642\u306e\u53d6\u5f15\u306f\u30aa\u30fc\u30d7\u30f3\u30a2\u30a6\u30c8\u30af\u30e9\u30a4\u65b9\u5f0f\u306b\u3088\u3063\u3066\u624b\u52d5\u3067\u884c\u308f\u308c\u3066\u3044\u307e\u3057\u305f\u3002\u30cb\u30e5\u30fc\u30e8\u30fc\u30af\u8a3c\u5238\u53d6\u5f15\u6240\uff08NYSE\uff09\u306e\u3088\u3046\u306a\u53d6\u5f15\u6240\u3067\u306f\u3001\u30c8\u30ec\u30fc\u30c0\u30fc\u305f\u3061\u304c\u53d6\u5f15\u30d5\u30ed\u30a2\u3067\u300c\u8cb7\u3044\u300d\u300c\u58f2\u308a\u300d\u3068\u5927\u58f0\u3067\u53eb\u3073\u306a\u304c\u3089\u6ce8\u6587\u3092\u51fa\u3057\u3001\u53d6\u5f15\u3092\u6210\u7acb\u3055\u305b\u3066\u3044\u307e\u3057\u305f\u3002\u3053\u308c\u306f\u5f53\u6642\u3068\u3057\u3066\u306f\u52b9\u679c\u7684\u306a\u65b9\u6cd5\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u305d\u306e\u5f8c\u3001\u96fb\u5b50\u6280\u8853\u306e\u9032\u6b69\u306b\u3088\u308a\u3001\u5e02\u5834\u306f\u5f90\u3005\u306b\u81ea\u52d5\u5316\u3055\u308c\u305f\u30b7\u30b9\u30c6\u30e0\u3078\u3068\u79fb\u884c\u3057\u3066\u3044\u304d\u307e\u3057\u305f\u30021971\u5e74\u306b\u306f\u3001NASDAQ\u304c\u4e16\u754c\u521d\u306e\u96fb\u5b50\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a8\u30f3\u30b8\u30f3\u3092\u5c0e\u5165\u3057\u3001\u4eba\u3067\u306f\u306a\u304f\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30fc\u304c\u53d6\u5f15\u306e\u5b9f\u884c\u3092\u62c5\u3046\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u3053\u306e\u9769\u65b0\u306b\u3088\u308a\u3001\u30b9\u30d4\u30fc\u30c9\u30fb\u52b9\u7387\u6027\u30fb\u62e1\u5f35\u6027\u304c\u98db\u8e8d\u7684\u306b\u5411\u4e0a\u3057\u3001\u73fe\u4ee3\u306e\u96fb\u5b50\u53d6\u5f15\u306e\u57fa\u76e4\u304c\u7bc9\u304b\u308c\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u306e\u4ed5\u7d44\u307f<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u30b9\u30c6\u30c3\u30d7<\/th><th>\u8aac\u660e<\/th><\/tr><\/thead><tbody><tr><td>1. \u6ce8\u6587\u306e\u53d7\u4fe1<\/td><td>\u30c8\u30ec\u30fc\u30c0\u30fc\u306f\u53d6\u5f15\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u3092\u901a\u3058\u3066\u3001\u4fa1\u683c\u30fb\u6570\u91cf\u30fb\u6ce8\u6587\u306e\u7a2e\u985e\uff08\u6210\u884c\u307e\u305f\u306f\u6307\u5024\uff09\u306a\u3069\u306e\u8a73\u7d30\u3092\u542b\u3080\u58f2\u8cb7\u6ce8\u6587\u3092\u63d0\u51fa\u3057\u307e\u3059\u3002<\/td><\/tr><tr><td>2. \u6ce8\u6587\u306e\u30ad\u30e5\u30fc\u5316<\/td><td>\u30de\u30c3\u30c1\u30f3\u30b0\u30a8\u30f3\u30b8\u30f3\u306f\u3001\u4e8b\u524d\u306b\u5b9a\u3081\u3089\u308c\u305f\u30eb\u30fc\u30eb\uff08\u901a\u5e38\u306f\u4fa1\u683c\u512a\u5148\u30fb\u6b21\u306b\u6642\u9593\u512a\u5148\uff09\u306b\u5f93\u3063\u3066\u6ce8\u6587\u3092\u30ad\u30e5\u30fc\u306b\u4e26\u3079\u307e\u3059\u3002<\/td><\/tr><tr><td>3. \u6ce8\u6587\u306e\u30de\u30c3\u30c1\u30f3\u30b0<\/td><td>\u30a8\u30f3\u30b8\u30f3\u306f\u677f\u60c5\u5831\u3092\u30b9\u30ad\u30e3\u30f3\u3057\u3001\u30de\u30c3\u30c1\u3059\u308b\u76f8\u624b\u3092\u63a2\u3057\u307e\u3059\u3002\u8cb7\u3044\u6ce8\u6587\u306e\u4fa1\u683c\u304c\u58f2\u308a\u6ce8\u6587\u306e\u4fa1\u683c\u4ee5\u4e0a\u3067\u3042\u308c\u3070\u3001\u53d6\u5f15\u304c\u6210\u7acb\u3057\u307e\u3059\u3002<\/td><\/tr><tr><td>4. \u53d6\u5f15\u306e\u78ba\u8a8d<\/td><td>\u30de\u30c3\u30c1\u30f3\u30b0\u304c\u6210\u7acb\u3059\u308b\u3068\u3001\u30a8\u30f3\u30b8\u30f3\u306f\u53d6\u5f15\u3092\u78ba\u8a8d\u3057\u3001\u53cc\u65b9\u3068\u6e05\u7b97\u30b7\u30b9\u30c6\u30e0\u306b\u901a\u77e5\u3092\u9001\u4fe1\u3057\u307e\u3059\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6ce8\u6587\u677f\u306e\u7ba1\u7406<\/strong><\/h2>\n\n\n\n<p>\u6ce8\u6587\u677f\uff08\u30aa\u30fc\u30c0\u30fc\u30d6\u30c3\u30af\uff09\u306f\u3001\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a8\u30f3\u30b8\u30f3\u306b\u3068\u3063\u3066\u91cd\u8981\u306a\u69cb\u6210\u8981\u7d20\u3067\u3042\u308a\u3001\u73fe\u5728\u306e\u3059\u3079\u3066\u306e\u6307\u5024\u6ce8\u6587\u3092\u8ffd\u8de1\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8cb7\u3044\u6ce8\u6587\u677f<\/strong>\uff1a\u9ad8\u3044\u4fa1\u683c\u9806\u306b\u8cb7\u3044\u6ce8\u6587\u3092\u30ea\u30b9\u30c8\u3002\u4fa1\u683c\u304c\u9ad8\u3044\u6ce8\u6587\u307b\u3069\u512a\u5148\u7684\u306b\u7d04\u5b9a\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u58f2\u308a\u6ce8\u6587\u677f<\/strong>\uff1a\u4f4e\u3044\u4fa1\u683c\u9806\u306b\u58f2\u308a\u6ce8\u6587\u3092\u30ea\u30b9\u30c8\u3002\u4fa1\u683c\u304c\u4f4e\u3044\u6ce8\u6587\u304b\u3089\u9806\u306b\u57f7\u884c\u3055\u308c\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6ce8\u6587\u677f\u3092\u9069\u5207\u306b\u7ba1\u7406\u3059\u308b\u3053\u3068\u306f\u3001\u5e02\u5834\u306e\u6d41\u52d5\u6027\u3092\u7dad\u6301\u3057\u3001\u4fa1\u683c\u767a\u898b\u30d7\u30ed\u30bb\u30b9\u3092\u652f\u3048\u308b\u4e0a\u3067\u4e0d\u53ef\u6b20\u3067\u3059\u3002\u5e02\u5834\u72b6\u6cc1\u306e\u5909\u5316\u306b\u5fdc\u3058\u3066\u3001\u6ce8\u6587\u677f\u306f\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u3067\u66f4\u65b0\u3055\u308c\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6210\u884c\u6ce8\u6587\u3068\u6307\u5024\u6ce8\u6587\u306e\u9055\u3044<\/strong><\/h2>\n\n\n\n<p><strong>\u6210\u884c\u6ce8\u6587\uff08\u30de\u30fc\u30b1\u30c3\u30c8\u30aa\u30fc\u30c0\u30fc\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u73fe\u5728\u306e\u6700\u826f\u4fa1\u683c\u3067\u5373\u6642\u306b\u57f7\u884c\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30b9\u30d4\u30fc\u30c9\u3068\u78ba\u5b9f\u306a\u57f7\u884c\u3092\u512a\u5148\u3057\u307e\u3059\u304c\u3001\u4fa1\u683c\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/li>\n\n\n\n<li>\u6d41\u52d5\u6027\u304c\u4f4e\u3044\u5e02\u5834\u3067\u306f\u3001\u30b9\u30ea\u30c3\u30da\u30fc\u30b8\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6307\u5024\u6ce8\u6587\uff08\u30ea\u30df\u30c3\u30c8\u30aa\u30fc\u30c0\u30fc\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c8\u30ec\u30fc\u30c0\u30fc\u304c\u5e0c\u671b\u3059\u308b\u6700\u4f4e\u58f2\u5374\u4fa1\u683c\u307e\u305f\u306f\u6700\u9ad8\u8cfc\u5165\u4fa1\u683c\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u6ce8\u6587\u677f\u306b\u767b\u9332\u3055\u308c\u3001\u6307\u5b9a\u4fa1\u683c\u306b\u9054\u3057\u305f\u3068\u304d\u306b\u306e\u307f\u57f7\u884c\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u4fa1\u683c\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u306f\u53ef\u80fd\u3067\u3059\u304c\u3001\u5fc5\u305a\u3057\u3082\u6ce8\u6587\u304c\u7d04\u5b9a\u3055\u308c\u308b\u3068\u306f\u9650\u308a\u307e\u305b\u3093\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u822c\u7684\u306a\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\uff08\u4f8b\u4ed8\u304d\uff09<\/strong><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001\u73fe\u5728\u306e\u5e02\u5834\u3067\u5e83\u304f\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b3\u3064\u306e\u4ee3\u8868\u7684\u306a\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u3001\u305d\u308c\u305e\u308c\u306e\u7c21\u5358\u306a\u5b9f\u88c5\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. \u4fa1\u683c\u30fb\u6642\u9593\u512a\u5148\u65b9\u5f0f\uff08FIFO\uff09<\/strong><\/h4>\n\n\n\n<p>\u3053\u308c\u306f\u73fe\u4ee3\u306e\u5e02\u5834\u3067\u6700\u3082\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u6ce8\u6587\u306f\u307e\u305a\u4fa1\u683c\u306b\u3088\u3063\u3066\u512a\u5148\u9806\u4f4d\u304c\u6c7a\u307e\u308a\uff08\u3088\u308a\u6709\u5229\u306a\u4fa1\u683c\u304c\u5148\uff09\u3001\u540c\u4e00\u4fa1\u683c\u306e\u5834\u5408\u306f\u6642\u9593\u9806\uff08\u65e9\u304f\u51fa\u3055\u308c\u305f\u6ce8\u6587\u304c\u5148\uff09\u3067\u51e6\u7406\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4f8b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8cb7\u3044\u6ce8\u6587A\uff1a100\u682a @ 500\u5186\uff0810:00\uff09<\/li>\n\n\n\n<li>\u8cb7\u3044\u6ce8\u6587B\uff1a100\u682a @ 500\u5186\uff0810:01\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u2192 \u58f2\u308a\u6ce8\u6587\u304c500\u5186\u3067\u6765\u305f\u5834\u5408\u3001\u6ce8\u6587A\u304c\u512a\u5148\u7684\u306b\u30de\u30c3\u30c1\u30f3\u30b0\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import heapq\n\nclass Order:\n    def __init__(self, order_id, order_type, price, quantity):\n        self.order_id = order_id\n        self.order_type = order_type  # 'buy' or 'sell'\n        self.price = price\n        self.quantity = quantity\n        self.timestamp = None\n\n    def __lt__(self, other):\n        if self.order_type == 'buy':\n            # For buy orders, higher prices have priority; if prices are equal, earlier orders have priority\n            return (self.price, -self.timestamp) > (other.price, -other.timestamp)\n        else:\n            # For sell orders, lower prices have priority; if prices are equal, earlier orders have priority\n            return (self.price, self.timestamp) &lt; (other.price, other.timestamp)\n\nclass OrderBook:\n    def __init__(self):\n        self.buy_orders = []  # Max-heap for buy orders (negative prices for max-heap)\n        self.sell_orders = []  # Min-heap for sell orders\n        self.timestamp_counter = 0\n\n    def add_order(self, order):\n        order.timestamp = self.timestamp_counter\n        self.timestamp_counter += 1\n\n        if order.order_type == 'buy':\n            heapq.heappush(self.buy_orders, order)\n        else:\n            heapq.heappush(self.sell_orders, order)\n\n    def match_orders(self):\n        matches = []\n\n        while self.buy_orders and self.sell_orders:\n            highest_buy_order = self.buy_orders[0]\n            lowest_sell_order = self.sell_orders[0]\n\n            if highest_buy_order.price >= lowest_sell_order.price:\n                quantity_to_trade = min(highest_buy_order.quantity, lowest_sell_order.quantity)\n                matches.append((highest_buy_order.order_id, lowest_sell_order.order_id, quantity_to_trade))\n\n                highest_buy_order.quantity -= quantity_to_trade\n                lowest_sell_order.quantity -= quantity_to_trade\n\n                if highest_buy_order.quantity == 0:\n                    heapq.heappop(self.buy_orders)\n                if lowest_sell_order.quantity == 0:\n                    heapq.heappop(self.sell_orders)\n            else:\n                break\n\n        return matches\n\n# Example usage\norder_book = OrderBook()\n\n# Add some buy and sell orders\norder_book.add_order(Order(1, 'buy', 101, 10))\norder_book.add_order(Order(2, 'buy', 102, 5))\norder_book.add_order(Order(3, 'sell', 100, 8))\norder_book.add_order(Order(4, 'sell', 99, 10))\n\n# Match orders\nmatches = order_book.match_orders()\n\nfor match in matches:\n    print(f\"Matched Buy Order {match[0]} with Sell Order {match[1]} for {match[2]} units\")<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. \u30d7\u30ed\u30e9\u30bf\uff08\u6bd4\u4f8b\u914d\u5206\uff09\u65b9\u5f0f<\/strong><\/h4>\n\n\n\n<p>\u540c\u4e00\u4fa1\u683c\u5e2f\u306b\u8907\u6570\u306e\u6ce8\u6587\u304c\u3042\u308b\u5834\u5408\u3001\u6ce8\u6587\u30b5\u30a4\u30ba\u306b\u5fdc\u3058\u3066\u6bd4\u4f8b\u7684\u306b\u30de\u30c3\u30c1\u30f3\u30b0\u304c\u884c\u308f\u308c\u307e\u3059\u3002\u3088\u308a\u5927\u304d\u306a\u6ce8\u6587\u306b\u306f\u3001\u3088\u308a\u591a\u304f\u306e\u6570\u91cf\u304c\u5272\u308a\u5f53\u3066\u3089\u308c\u307e\u3059\u3002\u3053\u306e\u65b9\u5f0f\u306f\u5148\u7269\u53d6\u5f15\u6240\u306a\u3069\u3067\u3088\u304f\u63a1\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4f8b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8cb7\u3044\u6ce8\u6587A\uff1a300\u682a @ 1000\u5186<\/li>\n\n\n\n<li>\u8cb7\u3044\u6ce8\u6587B\uff1a700\u682a @ 1000\u5186<\/li>\n<\/ul>\n\n\n\n<p>\u2192 \u58f2\u308a\u6ce8\u6587\u304c500\u682a @ 1000\u5186\u3067\u51fa\u3055\u308c\u305f\u5834\u5408\u3001\u6ce8\u6587A\u306b150\u682a\u3001\u6ce8\u6587B\u306b350\u682a\u304c\u5272\u308a\u5f53\u3066\u3089\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import heapq\nfrom collections import defaultdict\n\nclass Order:\n    def __init__(self, order_id, order_type, price, quantity):\n        self.order_id = order_id\n        self.order_type = order_type  # 'buy' or 'sell'\n        self.price = price\n        self.quantity = quantity\n        self.timestamp = None\n\nclass OrderBook:\n    def __init__(self):\n        self.buy_orders = defaultdict(list)  # Buy orders grouped by price\n        self.sell_orders = defaultdict(list)  # Sell orders grouped by price\n        self.timestamp_counter = 0\n\n    def add_order(self, order):\n        order.timestamp = self.timestamp_counter\n        self.timestamp_counter += 1\n\n        if order.order_type == 'buy':\n            self.buy_orders[order.price].append(order)\n        else:\n            self.sell_orders[order.price].append(order)\n\n    def match_orders(self):\n        matches = []\n\n        # Sort buy and sell prices\n        buy_prices = sorted(self.buy_orders.keys(), reverse=True)\n        sell_prices = sorted(self.sell_orders.keys())\n\n        while buy_prices and sell_prices:\n            highest_buy_price = buy_prices[0]\n            lowest_sell_price = sell_prices[0]\n\n            if highest_buy_price >= lowest_sell_price:\n                buy_orders_at_price = self.buy_orders[highest_buy_price]\n                sell_orders_at_price = self.sell_orders[lowest_sell_price]\n\n                # Calculate total buy and sell quantities at this price\n                total_buy_quantity = sum(order.quantity for order in buy_orders_at_price)\n                total_sell_quantity = sum(order.quantity for order in sell_orders_at_price)\n\n                # Determine the amount to trade based on the smaller side\n                quantity_to_trade = min(total_buy_quantity, total_sell_quantity)\n\n                # Pro-rata allocation for buy and sell orders\n                for order in buy_orders_at_price:\n                    proportion = order.quantity \/ total_buy_quantity\n                    traded_quantity = quantity_to_trade * proportion\n                    matches.append((order.order_id, lowest_sell_price, traded_quantity))\n                    order.quantity -= traded_quantity\n\n                for order in sell_orders_at_price:\n                    proportion = order.quantity \/ total_sell_quantity\n                    traded_quantity = quantity_to_trade * proportion\n                    matches.append((order.order_id, highest_buy_price, traded_quantity))\n                    order.quantity -= traded_quantity\n\n                # Remove fully matched orders\n                self.buy_orders[highest_buy_price] = [o for o in buy_orders_at_price if o.quantity > 0]\n                self.sell_orders[lowest_sell_price] = [o for o in sell_orders_at_price if o.quantity > 0]\n\n                if not self.buy_orders[highest_buy_price]:\n                    del self.buy_orders[highest_buy_price]\n                    buy_prices.pop(0)\n\n                if not self.sell_orders[lowest_sell_price]:\n                    del self.sell_orders[lowest_sell_price]\n                    sell_prices.pop(0)\n            else:\n                break\n\n        return matches\n\n# Example usage\norder_book = OrderBook()\n\n# Add some buy and sell orders\norder_book.add_order(Order(1, 'buy', 100, 10))\norder_book.add_order(Order(2, 'buy', 100, 20))\norder_book.add_order(Order(3, 'sell', 100, 15))\norder_book.add_order(Order(4, 'sell', 100, 5))\n\n# Match orders\nmatches = order_book.match_orders()\n\nfor match in matches:\n    print(f\"Order {match[0]} matched at price {match[1]} for {match[2]:.2f} units\")<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3. \u30cf\u30a4\u30d6\u30ea\u30c3\u30c9\u65b9\u5f0f<\/strong><\/h4>\n\n\n\n<p>\u4fa1\u683c\u30fb\u6642\u9593\u512a\u5148\u65b9\u5f0f\u3068\u30d7\u30ed\u30e9\u30bf\u65b9\u5f0f\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002\u5e02\u5834\u69cb\u9020\u3084\u53c2\u52a0\u8005\u306e\u884c\u52d5\u306b\u5fdc\u3058\u3066\u3001\u30a8\u30f3\u30b8\u30f3\u304c\u30de\u30c3\u30c1\u30f3\u30b0\u30ed\u30b8\u30c3\u30af\u3092\u52d5\u7684\u306b\u8abf\u6574\u3057\u307e\u3059\u3002\u67d4\u8edf\u304b\u3064\u516c\u5e73\u6027\u306e\u9ad8\u3044\u30bd\u30ea\u30e5\u30fc\u30b7\u30e7\u30f3\u3092\u63d0\u4f9b\u3059\u308b\u76ee\u7684\u3067\u8a2d\u8a08\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4f8b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5e02\u5834\u304c\u6d41\u52d5\u7684\u306a\u5834\u5408\u306f\u4fa1\u683c\u30fb\u6642\u9593\u512a\u5148\u3092\u9069\u7528\u3057\u3001\u6d41\u52d5\u6027\u304c\u4f4e\u4e0b\u3057\u305f\u5834\u5408\u306b\u306f\u6bd4\u4f8b\u914d\u5206\u306e\u8981\u7d20\u3092\u5f37\u5316\u3059\u308b\u306a\u3069\u306e\u5bfe\u5fdc\u304c\u53ef\u80fd\u3067\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import heapq\nfrom collections import defaultdict\n\nclass Order:\n    def __init__(self, order_id, order_type, price, quantity):\n        self.order_id = order_id\n        self.order_type = order_type  # 'buy' or 'sell'\n        self.price = price\n        self.quantity = quantity\n        self.timestamp = None\n\n    def __lt__(self, other):\n        return self.timestamp &lt; other.timestamp\n\nclass OrderBook:\n    def __init__(self):\n        self.buy_orders = defaultdict(list)  # Buy orders grouped by price\n        self.sell_orders = defaultdict(list)  # Sell orders grouped by price\n        self.timestamp_counter = 0\n\n    def add_order(self, order):\n        order.timestamp = self.timestamp_counter\n        self.timestamp_counter += 1\n\n        if order.order_type == 'buy':\n            heapq.heappush(self.buy_orders[order.price], order)\n        else:\n            heapq.heappush(self.sell_orders[order.price], order)\n\n    def match_orders(self):\n        matches = []\n\n        # Sort buy and sell prices\n        buy_prices = sorted(self.buy_orders.keys(), reverse=True)\n        sell_prices = sorted(self.sell_orders.keys())\n\n        while buy_prices and sell_prices:\n            highest_buy_price = buy_prices[0]\n            lowest_sell_price = sell_prices[0]\n\n            if highest_buy_price >= lowest_sell_price:\n                buy_orders_at_price = self.buy_orders[highest_buy_price]\n                sell_orders_at_price = self.sell_orders[lowest_sell_price]\n\n                # Calculate total buy and sell quantities at this price\n                total_buy_quantity = sum(order.quantity for order in buy_orders_at_price)\n                total_sell_quantity = sum(order.quantity for order in sell_orders_at_price)\n\n                # Determine the amount to trade based on the smaller side\n                quantity_to_trade = min(total_buy_quantity, total_sell_quantity)\n\n                # Priority based matching and then pro-rata allocation\n                while quantity_to_trade > 0 and buy_orders_at_price and sell_orders_at_price:\n                    buy_order = buy_orders_at_price[0]\n                    sell_order = sell_orders_at_price[0]\n\n                    if buy_order.quantity &lt;= sell_order.quantity:\n                        matches.append((buy_order.order_id, sell_order.order_id, buy_order.quantity))\n                        sell_order.quantity -= buy_order.quantity\n                        quantity_to_trade -= buy_order.quantity\n                        heapq.heappop(buy_orders_at_price)\n                        if sell_order.quantity == 0:\n                            heapq.heappop(sell_orders_at_price)\n                    else:\n                        matches.append((buy_order.order_id, sell_order.order_id, sell_order.quantity))\n                        buy_order.quantity -= sell_order.quantity\n                        quantity_to_trade -= sell_order.quantity\n                        heapq.heappop(sell_orders_at_price)\n                        if buy_order.quantity == 0:\n                            heapq.heappop(buy_orders_at_price)\n\n                # Remove empty price levels\n                if not buy_orders_at_price:\n                    del self.buy_orders[highest_buy_price]\n                    buy_prices.pop(0)\n\n                if not sell_orders_at_price:\n                    del self.sell_orders[lowest_sell_price]\n                    sell_prices.pop(0)\n\n            else:\n                break\n\n        return matches\n\n# Example usage\norder_book = OrderBook()\n\n# Add some buy and sell orders\norder_book.add_order(Order(1, 'buy', 100, 15))\norder_book.add_order(Order(2, 'buy', 100, 25))\norder_book.add_order(Order(3, 'sell', 100, 30))\norder_book.add_order(Order(4, 'sell', 100, 10))\n\n# Match orders\nmatches = order_book.match_orders()\n\nfor match in matches:\n    print(f\"Buy Order {match[0]} matched with Sell Order {match[1]} for {match[2]:.2f} units\")<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\u6587\u30de\u30c3\u30c1\u30f3\u30b0\u30a8\u30f3\u30b8\u30f3\u306f\u3001\u3042\u3089\u3086\u308b\u53d6\u5f15\u30b7\u30b9\u30c6\u30e0\u306e\u4e2d\u6838\u3092\u62c5\u3046\u5b58\u5728\u3067\u3059\u3002\u30de\u30c3\u30c1\u30f3\u30b0\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3001\u30c7\u30fc\u30bf\u7ba1\u7406\u3001\u901a\u4fe1\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306a\u3069\u3001\u3044\u304f\u3064\u304b\u306e\u4e3b\u8981\u306a\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304b\u3089\u69cb\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u305d\u306e\u4e3b\u306a\u5f79\u5272\u306f\u3001\u5e02\u5834\u53c2\u52a0\u8005\u306e\u58f2\u8cb7\u6ce8\u6587\u3092 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":174,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide"],"featured_image_src":"https:\/\/i0.wp.com\/blog.alltick.co\/ja-jp\/wp-content\/uploads\/sites\/2\/2025\/06\/8b448e388b0345019aece572732acfad.jpg?fit=1600%2C900&ssl=1","author_info":{"display_name":"admin","author_link":"https:\/\/blog.alltick.co\/ja-jp\/author\/admin\/"},"_links":{"self":[{"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":1,"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/posts\/173\/revisions\/175"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/media\/174"}],"wp:attachment":[{"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alltick.co\/ja-jp\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}