Skip to content

Commit

Permalink
added responsibiliteis to exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Nov 28, 2024
1 parent 928f178 commit cb69555
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
34 changes: 12 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# rs_trader

```py
from uuid import UUID

from rs_trader import Exchange
from rs_trader.interfaces import DatabaseInterface
from rs_trader.storage import JsonDatabase
Expand All @@ -12,29 +10,21 @@ from rs_trader.structs import Order, OrderStatus, OrderType
database = JsonDatabase()
exchange = Exchange(database=database)

# Place a buy order
buy_order = Order(
user_id=1, item_id=1001, order_type=OrderType.BUY, quantity=10, price=150
)
exchange.place_order(buy_order)

# Place a sell order
sell_order = Order(
user_id=2, item_id=1001, order_type=OrderType.SELL, quantity=5, price=140
exchange.place_order(
Order(user_id=2, item_id=123, order_type=OrderType.SELL, quantity=1000, price=1)
)
exchange.place_order(
Order(user_id=2, item_id=123, order_type=OrderType.SELL, quantity=100, price=3)
)
exchange.place_order(sell_order)

# TODO: this will probably move to exchange.get_orders()
orders = database.get_orders()
[print(o) for o in orders]

first_order = orders[0].model_dump(mode="json")
first_order_id = first_order.pop("order_id")
# Place a buy order
buy_order = Order(user_id=1, item_id=123, order_type=OrderType.BUY, quantity=150, price=5)
exchange.place_order(buy_order)

print(
first_order,
"remaining quantity:",
database.get_order_remaining_quantity(UUID(first_order_id)),
)
orders = exchange.get_orders(item_id=123)
_ = [print(o, database.get_order_remaining_quantity(o.order_id)) for o in orders]

order_parts = exchange.get_order_parts(order_id=buy_order.order_id)
_ = [print(o)for o in order_parts]
```
37 changes: 31 additions & 6 deletions rs_trader/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from datetime import datetime
from typing import Optional
from uuid import UUID

from rs_trader.interfaces.database import DatabaseInterface
from rs_trader.structs.structs import (
Expand Down Expand Up @@ -32,12 +34,6 @@ def match_buy_order(self, buy_order: Order) -> None:
)
# Sort by price in descending order (highest price first)
open_sell_orders.sort(key=lambda o: o.price, reverse=True)
print()
for s in open_sell_orders:
_s = s.model_dump(mode="json")
_s.pop("order_id")

print(_s)
self._fulfill_order(buy_order, open_sell_orders)

def match_sell_order(self, sell_order: Order) -> None:
Expand Down Expand Up @@ -97,3 +93,32 @@ def _fulfill_order(self, order: Order, matching_orders: list[Order]) -> None:
self.database.update_order_status(order.order_id, OrderStatus.CLOSED)
else:
order.quantity = remaining_quantity

def get_order(
self,
order_id: Optional[UUID] = None,
item_id: Optional[int] = None,
user_id: Optional[int] = None,
order_type: Optional[OrderType] = None,
status: Optional[OrderStatus] = None,
max_price: Optional[int] = None,
min_price: Optional[int] = None,
) -> list[Order]:
return self.database.get_orders(
order_id=order_id,
item_id=item_id,
user_id=user_id,
order_type=order_type,
status=status,
max_price=max_price,
min_price=min_price,
)

def get_order_parts(
self,
order_id: Optional[UUID] = None,
order_part_id: Optional[UUID] = None,
) -> list[OrderPart]:
return self.database.get_order_parts(
order_id=order_id, order_part_id=order_part_id
)
1 change: 0 additions & 1 deletion test.json

This file was deleted.

0 comments on commit cb69555

Please sign in to comment.