forked from danielktaylor/PyLimitBook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
orderList.py
86 lines (74 loc) · 2.34 KB
/
orderList.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
from order import Order
class OrderList(object):
def __init__(self):
self.headOrder = None
self.tailOrder = None
self.length = 0
self.volume = 0 # Total share volume
self.last = None
def __len__(self):
return self.length
def __iter__(self):
self.last = self.headOrder
return self
def next(self):
if self.last == None:
raise StopIteration
else:
returnVal = self.last
self.last = self.last.nextOrder
return returnVal
def appendOrder(self, order):
"""
:param order:
:type order: Order
:return:
"""
if len(self) == 0:
order.nextOrder = None
order.prevOrder = None
self.headOrder = order
self.tailOrder = order
else:
order.prevOrder = self.tailOrder
order.nextOrder = None
self.tailOrder.nextOrder = order
self.tailOrder = order
self.length += 1
self.volume += order.qty
def removeOrder(self, order):
self.volume -= order.qty
self.length -= 1
if len(self) == 0:
return
# Remove from list of orders
nextOrder = order.nextOrder
prevOrder = order.prevOrder
if nextOrder != None and prevOrder != None:
nextOrder.prevOrder = prevOrder
prevOrder.nextOrder = nextOrder
elif nextOrder != None:
nextOrder.prevOrder = None
self.headOrder = nextOrder
elif prevOrder != None:
prevOrder.nextOrder = None
self.tailOrder = prevOrder
def moveTail(self, order):
if order.prevOrder != None:
order.prevOrder.nextOrder = self.nextOrder
else:
# Update the head order
self.headOrder = order.nextOrder
order.nextOrder.prevOrder = order.prevOrder
# Set the previous tail order's next order to this order
self.tailOrder.nextOrder = order
self.tailOrder = order
order.prevOrder = self.tailOrder
order.nextOrder = None
def __str__(self):
from cStringIO import StringIO
file_str = StringIO()
for order in self:
file_str.write("%s\n" % str(order))
return file_str.getvalue()