-
Notifications
You must be signed in to change notification settings - Fork 3
/
Engine.py
69 lines (56 loc) · 2.75 KB
/
Engine.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
from orderbook import OrderBook
"""
import MySQLdb
db = MySQLdb.connect("localhost", "root", "root", "alphex")
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print "Database version : %s " % data
db.close()"""
# Create an order book
order_book = OrderBook()
# Create some limit orders
limit_orders = [
{ "type": "limit", "side": "ask", "quantity": 8.87314461, "price": 0.01911999, "trade_id": 100 },
{ "type": "limit", "side": "ask", "quantity": 8.87314461, "price": 0.01811999, "trade_id": 101 },
{ "type": "limit", "side": "ask", "quantity": 8.87314461, "price": 0.01711999, "trade_id": 102 },
{ "type": "limit", "side": "ask", "quantity": 8.87314461, "price": 0.01611999, "trade_id": 103 },
{ "type": "limit", "side": "bid", "quantity": 8.87314461, "price": 0.01511999, "trade_id": 100 },
{ "type": "limit", "side": "bid", "quantity": 8.87314461, "price": 0.01411999, "trade_id": 101 },
{ "type": "limit", "side": "bid", "quantity": 8.87314461, "price": 0.01311999, "trade_id": 102 },
{ "type": "limit", "side": "bid", "quantity": 8.87314461, "price": 0.01211999, "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