-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatamodel.py
More file actions
51 lines (42 loc) · 1.65 KB
/
Copy pathdatamodel.py
File metadata and controls
51 lines (42 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from typing import Dict, List
class Listing:
"""
A class to represent the
"""
def __init__(self, orderbook: Dict[str, Dict[int, int]], product: str) -> None:
self.buy_orders = orderbook["BUY"] #dict of {price: quantity} top is lowest price
self.sell_orders = orderbook["SELL"] #dict of {price: quantity} top is highest price
self.product = product
class Order:
"""
A class to represent an order sent to the matching engine.
"""
def __init__(self, product: str, price: int, quantity: int):
self.product = product
self.price = price
self.quantity = quantity
def is_valid(self) -> bool:
return (isinstance(self.product, str) and self.product and
isinstance(self.quantity, int) and self.quantity != 0 and
isinstance(self.price, int) and self.price > 0)
def __str__(self):
return f"Order(product={self.product}, price={self.price}, quantity={self.quantity})"
class Portfolio:
"""
A class to represent the trader's current portfolio.
"""
def __init__(self):
self.cash: float = 0
self.quantity: Dict[str, int] = {}
self.pnl: float = 0
def __str__(self):
return f"Portfolio(cash={self.cash}, quantity={self.quantity}, pnl={self.pnl})"
class State:
"""
A class to represent the state of the market and the trader's portfolio.
"""
def __init__(self, orderbook: Dict[str, Dict[int, int]], positions: Dict[str, int], products: List[str], pos_limit: int):
self.orderbook = orderbook
self.positions = positions
self.products = products
self.pos_limit = pos_limit