-
Notifications
You must be signed in to change notification settings - Fork 3
/
market_maker.py
51 lines (38 loc) · 1.49 KB
/
market_maker.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
from enum import Enum
class Dir(str, Enum):
BUY = "BUY"
SELL = "SELL"
class MarketMaker():
def __init__(self, exchange):
self._exchange = exchange
self._BASE_OID = 4_000_000
self._delta = 1
self._assets = ['BOND', 'GS', 'MS', 'VALBZ', 'VALE', 'WFC', 'XLF']
# one unique order id for each asset and side, e.g. 'WFC' and 'B'
self._oid = {}
for asset in self._assets:
self._oid[asset+'B'] = self._BASE_OID + 1
self._oid[asset+'S'] = self._BASE_OID + 2
self._BASE_OID += 2
def listen(self, msg):
'''
If a newly confirmed trade is broadcasted, cancel all pending
orders for said asset and place two new ones, a buy order at
txn_price-1 and a sell order at txn_price+1.
'''
if msg['type'] != 'trade':
return
if msg['symbol'] not in self._assets:
return
sym = msg['symbol']
prc = msg['price']
b_prc = prc - self._delta
s_prc = prc + self._delta
b_oid = self._oid[sym+'B']
s_oid = self._oid[sym+'S']
# cancel existing orders
self._exchange.send_cancel_message(order_id=b_oid)
self._exchange.send_cancel_message(order_id=s_oid)
# add new orders
self._exchange.send_add_message(order_id=b_oid, symbol=sym, dir=Dir.BUY , price=b_prc, size=1)
self._exchange.send_add_message(order_id=s_oid, symbol=sym, dir=Dir.SELL, price=s_prc, size=1)