self.buy_orders[highest_buy_price] = [o for o in buy_orders_at_price if o.quantity>0]
self.sell_orders[lowest_sell_price] = [o for o in sell_orders_at_price if o.quantity>0]
if not self.buy_orders[highest_buy_price]:
del self.buy_orders[highest_buy_price]
buy_prices.pop(0)
if not self.sell_orders[lowest_sell_price]:
del self.sell_orders[lowest_sell_price]
sell_prices.pop(0)
else:
break
return matches
# Example usage
order_book = OrderBook()
# Add some buy and sell orders
order_book.add_order(Order(1, 'buy', 100, 10))
order_book.add_order(Order(2, 'buy', 100, 20))
order_book.add_order(Order(3, 'sell', 100, 15))
order_book.add_order(Order(4, 'sell', 100, 5))
# Match orders
matches = order_book.match_orders()
for match in matches:
print(f"Order {match[0]} matched at price {match[1]} for {match[2]:.2f} units")
import heapq
from collections import defaultdict
class Order:
def __init__(self, order_id, order_type, price, quantity):
self.order_id = order_id
self.order_type = order_type # 'buy' or 'sell'
self.price = price
self.quantity = quantity
self.timestamp = None
class OrderBook:
def __init__(self):
self.buy_orders = defaultdict(list) # Buy orders grouped by price
self.sell_orders = defaultdict(list) # Sell orders grouped by price
self.timestamp_counter = 0
def add_order(self, order):
order.timestamp = self.timestamp_counter
self.timestamp_counter += 1
if order.order_type == 'buy':
self.buy_orders[order.price].append(order)
else:
self.sell_orders[order.price].append(order)
def match_orders(self):
matches = []
# Sort buy and sell prices
buy_prices = sorted(self.buy_orders.keys(), reverse=True)
sell_prices = sorted(self.sell_orders.keys())
while buy_prices and sell_prices:
highest_buy_price = buy_prices[0]
lowest_sell_price = sell_prices[0]
if highest_buy_price >= lowest_sell_price:
buy_orders_at_price = self.buy_orders[highest_buy_price]
sell_orders_at_price = self.sell_orders[lowest_sell_price]
# Calculate total buy and sell quantities at this price
total_buy_quantity = sum(order.quantity for order in buy_orders_at_price)
total_sell_quantity = sum(order.quantity for order in sell_orders_at_price)
# Determine the amount to trade based on the smaller side
quantity_to_trade = min(total_buy_quantity, total_sell_quantity)
# Pro-rata allocation for buy and sell orders
for order in buy_orders_at_price:
proportion = order.quantity / total_buy_quantity
traded_quantity = quantity_to_trade * proportion
matches.append((order.order_id, lowest_sell_price, traded_quantity))
order.quantity -= traded_quantity
for order in sell_orders_at_price:
proportion = order.quantity / total_sell_quantity
traded_quantity = quantity_to_trade * proportion
matches.append((order.order_id, highest_buy_price, traded_quantity))
order.quantity -= traded_quantity
# Remove fully matched orders
self.buy_orders[highest_buy_price] = [o for o in buy_orders_at_price if o.quantity > 0]
self.sell_orders[lowest_sell_price] = [o for o in sell_orders_at_price if o.quantity > 0]
if not self.buy_orders[highest_buy_price]:
del self.buy_orders[highest_buy_price]
buy_prices.pop(0)
if not self.sell_orders[lowest_sell_price]:
del self.sell_orders[lowest_sell_price]
sell_prices.pop(0)
else:
break
return matches
# Example usage
order_book = OrderBook()
# Add some buy and sell orders
order_book.add_order(Order(1, 'buy', 100, 10))
order_book.add_order(Order(2, 'buy', 100, 20))
order_book.add_order(Order(3, 'sell', 100, 15))
order_book.add_order(Order(4, 'sell', 100, 5))
# Match orders
matches = order_book.match_orders()
for match in matches:
print(f"Order {match[0]} matched at price {match[1]} for {match[2]:.2f} units")
import heapq
from collections import defaultdict
class Order:
def __init__(self, order_id, order_type, price, quantity):
self.order_id = order_id
self.order_type = order_type # 'buy' or 'sell'
self.price = price
self.quantity = quantity
self.timestamp = None
class OrderBook:
def __init__(self):
self.buy_orders = defaultdict(list) # Buy orders grouped by price
self.sell_orders = defaultdict(list) # Sell orders grouped by price
self.timestamp_counter = 0
def add_order(self, order):
order.timestamp = self.timestamp_counter
self.timestamp_counter += 1
if order.order_type == 'buy':
self.buy_orders[order.price].append(order)
else:
self.sell_orders[order.price].append(order)
def match_orders(self):
matches = []
# Sort buy and sell prices
buy_prices = sorted(self.buy_orders.keys(), reverse=True)
sell_prices = sorted(self.sell_orders.keys())
while buy_prices and sell_prices:
highest_buy_price = buy_prices[0]
lowest_sell_price = sell_prices[0]
if highest_buy_price >= lowest_sell_price:
buy_orders_at_price = self.buy_orders[highest_buy_price]
sell_orders_at_price = self.sell_orders[lowest_sell_price]
# Calculate total buy and sell quantities at this price
total_buy_quantity = sum(order.quantity for order in buy_orders_at_price)
total_sell_quantity = sum(order.quantity for order in sell_orders_at_price)
# Determine the amount to trade based on the smaller side
quantity_to_trade = min(total_buy_quantity, total_sell_quantity)
# Pro-rata allocation for buy and sell orders
for order in buy_orders_at_price:
proportion = order.quantity / total_buy_quantity
traded_quantity = quantity_to_trade * proportion
matches.append((order.order_id, lowest_sell_price, traded_quantity))
order.quantity -= traded_quantity
for order in sell_orders_at_price:
proportion = order.quantity / total_sell_quantity
traded_quantity = quantity_to_trade * proportion
matches.append((order.order_id, highest_buy_price, traded_quantity))
order.quantity -= traded_quantity
# Remove fully matched orders
self.buy_orders[highest_buy_price] = [o for o in buy_orders_at_price if o.quantity > 0]
self.sell_orders[lowest_sell_price] = [o for o in sell_orders_at_price if o.quantity > 0]
if not self.buy_orders[highest_buy_price]:
del self.buy_orders[highest_buy_price]
buy_prices.pop(0)
if not self.sell_orders[lowest_sell_price]:
del self.sell_orders[lowest_sell_price]
sell_prices.pop(0)
else:
break
return matches
# Example usage
order_book = OrderBook()
# Add some buy and sell orders
order_book.add_order(Order(1, 'buy', 100, 10))
order_book.add_order(Order(2, 'buy', 100, 20))
order_book.add_order(Order(3, 'sell', 100, 15))
order_book.add_order(Order(4, 'sell', 100, 5))
# Match orders
matches = order_book.match_orders()
for match in matches:
print(f"Order {match[0]} matched at price {match[1]} for {match[2]:.2f} units")