-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverorder.py
57 lines (45 loc) · 1.91 KB
/
serverorder.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
48
49
50
51
52
53
54
55
56
57
import time
class Counter:
def __init__(self):
self.value = 0
def get_value(self):
value = self.value
self.value += 1
return value
counter = Counter()
class ServerOrder:
""" A server order contains full details about the order """
def __init__(self, way, instrument_identifier, quantity, price, counterparty,
identifier=counter.get_value(), timestamp=None,
canceled_quantity=0.0, executed_quantity=0.0):
if not counterparty:
raise Exception('Counterparty not set')
self.identifier = identifier
self.way = way
self.instrument_identifier = instrument_identifier
self.quantity = quantity
self.canceled_quantity = canceled_quantity
self.executed_quantity = executed_quantity
self.price = price
self.counterparty = counterparty
if timestamp:
self.timestamp = timestamp
else:
# TODO: improve timestamp precision to milliseconds
self.timestamp = int(time.time())
def get_remaining_quantity(self):
remaining_quantity = self.quantity - self.executed_quantity - self.canceled_quantity
if remaining_quantity < 0.0:
raise Exception('Remaining quantity cannot be negative')
return remaining_quantity
def __str__(self):
return '{} {} {} @ {} from {} ({})'.format(str(self.way),
self.instrument_identifier,
self.get_remaining_quantity(),
self.price,
self.counterparty,
self.timestamp)
def __cmp__(self, other):
return self.__dict__ == other.__dict__
def __eq__(self, other):
return self.__dict__ == other.__dict__