-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.py
More file actions
132 lines (98 loc) · 5.4 KB
/
book.py
File metadata and controls
132 lines (98 loc) · 5.4 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from datetime import datetime
from functools import total_ordering
import itertools
@total_ordering
class Order :
newid = itertools.count()
def __init__(self, quantity, price, buy=True):
self.__quantity = quantity
self.__price = price
self.__buy = buy
self.__date=datetime.now()
self.id = next(self.newid)+1 #"+1" to start at 1
def __str__(self): # human-readable content
return "%s @ %s" % (self.__quantity, self.__price)
def __repr__(self): # unambiguous representation of the object
return "Order(%s, %s, %s)" % (self.__quantity, self.__price,
self.__buy)
def __eq__(self, other): # self == other
return other and self.__quantity == other.__quantity and self.__price == other.__price
def __lt__(self, other): # self < other
return other and self.__price < other.__price
def get_quantity(self):
return self.__quantity
def set_quantity(self, x):
self.__quantity=x
def get_price(self):
return self.__price
def get_buy(self):
return self.__buy
def get_date(self):
return self.__date
class Book :
def __init__(self, name, list_buy_orders=[], list_sell_orders=[] ):
self.__name = name
self.__list_buy_orders = list_buy_orders
self.__list_sell_orders = list_sell_orders
self.__string=""
def __str__(self):
return self.__string
def __repr__(self):
return "Book(%s, %s, %s)" % (self.__name, self.__list_buy_orders,
self.__list_sell_orders)
def insert_buy(self, quantity, price):
self.__list_buy_orders.append(Order(quantity,price,True))#We had the order to the book
n=len(self.__list_buy_orders)-1
temp="--- Insert %s %s | ID : % s| Date : %s | on %s \n" % ("BUY" ,
self.__list_buy_orders[n] ,
self.__list_buy_orders[n].id,
self.__list_buy_orders[n].get_date(),
self.__name)#We make the order public
self.__string+=temp
self.__list_buy_orders.sort(key= lambda o: (o,o.get_date()), reverse=True)
temp+=self.execute_order()
temp+=self.print_book()
print(temp)
def insert_sell(self, quantity, price):
self.__list_sell_orders.append(Order(quantity,price,False))#We had the order to the book
n=len(self.__list_sell_orders)-1
temp="--- Insert %s %s | ID : % s| Date : %s | on %s \n" % ("SELL",
self.__list_sell_orders[n] ,
self.__list_sell_orders[n].id,
self.__list_sell_orders[n].get_date(),
self.__name)#We make the order public
self.__string+=temp
self.__list_sell_orders.sort(key= lambda o: (o,o.get_date()), reverse=False)
self.__list_sell_orders.reverse()
temp+=self.execute_order()
temp+=self.print_book()
print(temp)
def get_list_buy_orders(self):
return self.__list_buy_orders
def get_list_sell_orders(self):
return self.__list_sell_orders
def print_book(self):
temp=""
temp+="Book on %s \n" % (self.__name)
for k in self.__list_sell_orders + self.__list_buy_orders:
temp+="\t %s %s | %s\n" % ("BUY" if k.get_buy() else "SELL", k, k.get_date())
temp+="------------------------\n\n"
self.__string+=temp
return temp
def execute_order(self):
temp=""
while (self.__list_sell_orders and self.__list_buy_orders) and self.__list_sell_orders[-1].get_price()<=self.__list_buy_orders[0].get_price() :
if self.__list_sell_orders[-1].get_quantity()>self.__list_buy_orders[0].get_quantity():
temp+="Execute %s at %s on %s\n" % (self.__list_buy_orders[0].get_quantity(),
self.__list_buy_orders[0].get_price(),
self.__name)
self.__list_sell_orders[-1].set_quantity(self.__list_sell_orders[-1].get_quantity()-self.__list_buy_orders[0].get_quantity())
self.__list_buy_orders.pop(0)
else :
temp+="Execute %s at %s on %s\n" % (self.__list_sell_orders[-1].get_quantity(),
self.__list_buy_orders[0].get_price(),
self.__name)
self.__list_buy_orders[0].set_quantity(self.__list_buy_orders[0].get_quantity()-self.__list_sell_orders[-1].get_quantity())
self.__list_sell_orders.pop()
self.__string+=temp
return temp