-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_trade.py
47 lines (38 loc) · 1.4 KB
/
order_trade.py
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
from decimal import Decimal
class Order:
BUY = 'BUY'
SELL = 'SELL'
def __init__(self, side, price, qty, order_type=None,client_id=None, binance_id=None, status=None):
self.client_id = client_id
self.binance_id = binance_id
self.side = side
self.price = price
self.qty = qty
self.fulfilled = False
self.order_type = order_type
self.status = status
def is_active(self):
return self.status in ['NEW', 'PARTIALLY_FILLED']
def __repr__(self):
order_str = f'{self.side} {self.qty} @ {self.price}'
if self.order_type:
order_str += f' - {self.order_type}'
if self.status:
order_str += f' - {self.status}'
if self.binance_id:
order_str = f'binance {self.binance_id} - {order_str}'
if self.client_id:
order_str = f'{self.client_id} - {order_str}'
return f'({order_str})'
@staticmethod
def from_binance(o):
assert(o['side'] in [Order.BUY, Order.SELL])
return Order(o['side'], Decimal(o['price']), Decimal(o['origQty']),
binance_id=o['orderId'], client_id=o['clientOrderId'], status=o['status'])
class Trade:
def __init__(self, price, qty, ts):
self.price = price
self.qty = qty
self.ts = ts
def __repr__(self):
return f'({self.price}, {self.qty}, {self.ts})'