-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathob-example.py
99 lines (88 loc) · 3.71 KB
/
ob-example.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
87
88
89
90
91
92
93
94
95
96
97
98
99
from orderbook import OrderBook
from ordertree import OrderTree
from orderlist import OrderList
from order import Order
import sys
order_book = OrderBook()
def main():
limit_orders = [{'type' : 'limit',
'side' : 'ask',
'quantity' : 5,
'price' : 101,
'trade_id' : 100},
{'type' : 'limit',
'side' : 'ask',
'quantity' : 5,
'price' : 103,
'trade_id' : 101},
{'type' : 'limit',
'side' : 'ask',
'quantity' : 5,
'price' : 101,
'trade_id' : 102},
{'type' : 'limit',
'side' : 'ask',
'quantity' : 5,
'price' : 101,
'trade_id' : 103},
{'type' : 'limit',
'side' : 'bid',
'quantity' : 5,
'price' : 99,
'trade_id' : 100},
{'type' : 'limit',
'side' : 'bid',
'quantity' : 5,
'price' : 98,
'trade_id' : 101},
{'type' : 'limit',
'side' : 'bid',
'quantity' : 5,
'price' : 99,
'trade_id' : 102},
{'type' : 'limit',
'side' : 'bid',
'quantity' : 5,
'price' : 97,
'trade_id' : 103},
]
# Add orders to order book
for order in limit_orders:
trades, order_id = order_book.process_order(order, False, False)
# The current book may be viewed using a print
print order_book
# Submitting a limit order that crosses the opposing best price will result in a trade
crossing_limit_order = {'type': 'limit',
'side': 'bid',
'quantity': 2,
'price': 102,
'trade_id': 109}
print crossing_limit_order
trades, order_in_book = order_book.process_order(crossing_limit_order, False, False)
print "Trade occurs as incoming bid limit crosses best ask"
print trades
print order_book
# If a limit crosses but is only partially matched, the remaning volume will
# be placed in the book as an outstanding order
big_crossing_limit_order = {'type': 'limit',
'side': 'bid',
'quantity': 50,
'price': 102,
'trade_id': 110}
print big_crossing_limit_order
trades, order_in_book = order_book.process_order(big_crossing_limit_order, False, False)
print "Large incoming bid limit crosses best ask. Remaining volume is placed in book."
print trades
print order_book
# Market Orders
# Market orders only require that a user specifies a side (bid or ask), a quantity, and their unique trade id
market_order = {'type': 'market',
'side': 'ask',
'quantity': 40,
'trade_id': 111}
trades, order_id = order_book.process_order(market_order, False, False)
print "A market order takes the specified volume from the inside of the book, regardless of price"
print "A market ask for 40 results in:"
print order_book
if __name__ == "__main__":
sys.exit(int(main() or 0))