/
market_spread.py
265 lines (224 loc) · 8.84 KB
/
market_spread.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright 2018 The Wallaroo Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
from enum import IntEnum
import struct
import time
import wallaroo
FIXTYPE_ORDER = 1
FIXTYPE_MARKET_DATA = 2
SIDETYPE_BUY = 1
SIDETYPE_SELL = 2
def application_setup(args):
input_addrs = wallaroo.tcp_parse_input_addrs(args)
inputs = {ip[0]: {"host": ip[1], "port": ip[2]} for ip in input_addrs}
out_host, out_port = wallaroo.tcp_parse_output_addrs(args)[0]
orders = wallaroo.source("Orders",
wallaroo.TCPSourceConfig("Orders", inputs["Orders"]["host"],
inputs["Orders"]["port"],
order_decoder))
market_data = wallaroo.source("Market Data",
wallaroo.TCPSourceConfig("Market Data", inputs["Market Data"]["host"],
inputs["Market Data"]["port"],
market_data_decoder))
pipeline = (orders.merge(market_data)
.key_by(extract_symbol)
.to(check_market_data)
.to_sink(wallaroo.TCPSinkConfig(out_host, out_port,
order_result_encoder)))
return wallaroo.build_application("Market Spread", pipeline)
class SerializedTypes(IntEnum):
"""
Define the types of data that can be serialised
"""
CHECKMARKETDATA = 1
EXTRACTSYMBOL = 2
ORDERRESULTENCODER = 3
MARKETDATAMESSAGE = 4
MARKETDATADECODER = 5
ORDERMESSAGE = 6
ORDERDECODER = 7
SYMBOLDATA = 8
def serialize(o):
"""
Pack objects according to the following schema:
0 - 4b object class (SerializedTypes)
1 - ?b object data (if required)
"""
if isinstance(o, check_market_data.__class__):
s = struct.Struct(">I")
return s.pack(SerializedTypes.CHECKMARKETDATA)
elif isinstance(o, extract_symbol.__class__):
s = struct.Struct(">I")
return s.pack(SerializedTypes.EXTRACTSYMBOL)
elif isinstance(o, order_result_encoder.__class__):
s = struct.Struct(">I")
return s.pack(SerializedTypes.ORDERRESULTENCODER)
elif isinstance(o, MarketDataMessage):
s = struct.Struct(">I4s21sdd")
return s.pack(SerializedTypes.MARKETDATAMESSAGE,
o.symbol, o.transact_time, o.bid, o.offer)
elif isinstance(o, market_data_decoder.__class__):
s = struct.Struct(">I")
return s.pack(SerializedTypes.MARKETDATADECODER)
elif isinstance(o, Order):
s = struct.Struct(">IBI6s4sdd21s")
return s.pack(SerializedTypes.ORDERMESSAGE, o.side, o.account,
o.order_id, o.symbol, o.qty, o.price, o.transact_time)
elif isinstance(o, order_decoder.__class__):
s = struct.Struct(">I")
return s.pack(SerializedTypes.ORDERDECODER)
elif isinstance(o, SymbolData):
s = struct.Struct(">Idd?")
return s.pack(SerializedTypes.SYMBOLDATA,
o.last_bid, o.last_offer, o.should_reject_trades)
else:
print("Don't know how to serialize {}".format(type(o).__name__))
return None
def deserialize(bs):
"""
Unpack objects according to the following schema:
0 - 4b object class (SerializedTypes)
1 - ?b object data (if required)
"""
(obj_type,), bs = struct.unpack(">I", bs[:4]), bs[4:]
if obj_type == SerializedTypes.CHECKMARKETDATA:
return check_market_data
elif obj_type == SerializedTypes.EXTRACTSYMBOL:
return extract_symbol
elif obj_type == SerializedTypes.ORDERRESULTENCODER:
return order_result_encoder
elif obj_type == SerializedTypes.MARKETDATAMESSAGE:
(symbol, time, bid, offer) = struct.unpack(">4s21sdd", bs)
return MarketDataMessage(symbol, time, bid, offer)
elif obj_type == SerializedTypes.MARKETDATADECODER:
return market_data_decoder
elif obj_type == SerializedTypes.ORDERMESSAGE:
(side, acct, oid, symbol, qty, price, t_time) = struct.unpack(
">BI6s4sdd21s", bs)
return Order(side, acct, oid, symbol, qty, price, t_time)
elif obj_type == SerializedTypes.ORDERDECODER:
return order_decoder
elif obj_type == SerializedTypes.SYMBOLDATA:
(last_bid, last_offer, should_reject_trades) = struct.unpack(">dd?", bs)
return SymbolData(last_bid, last_offer, should_reject_trades)
else:
print("Don't know how to deserialize: {}".format(obj_type))
return None
class MarketSpreadError(Exception):
pass
class SymbolData(object):
def __init__(self, last_bid=0.0, last_offer=0.0, should_reject_trades=True):
self.last_bid = last_bid
self.last_offer = last_offer
self.should_reject_trades = should_reject_trades
@wallaroo.key_extractor
def extract_symbol(data):
return data.symbol
@wallaroo.state_computation(name="Check Market Data", state=SymbolData)
def check_market_data(data, state):
if data.is_order:
if state.should_reject_trades:
ts = int(time.time() * 100000)
return OrderResult(data, state.last_bid, state.last_offer, ts)
return None
else:
offer_bid_difference = data.offer - data.bid
should_reject_trades = ((offer_bid_difference >= 0.05) or
((offer_bid_difference / data.mid) >= 0.05))
state.last_bid = data.bid
state.last_offer = data.offer
state.should_reject_trades = should_reject_trades
return None
class Order(object):
def __init__(self, side, account, order_id, symbol, qty, price,
transact_time):
self.is_order = True
self.side = side
self.account = account
self.order_id = order_id
self.symbol = symbol
self.qty = qty
self.price = price
self.transact_time = transact_time
class MarketDataMessage(object):
def __init__(self, symbol, transact_time, bid, offer):
self.is_order = False
self.symbol = symbol
self.transact_time = transact_time
self.bid = bid
self.offer = offer
self.mid = (bid + offer) / 2.0
@wallaroo.decoder(header_length=4, length_fmt=">I")
def order_decoder(bs):
"""
0 - 1b - FixType (U8)
1 - 1b - side (U8)
2 - 4b - account (U32)
6 - 6b - order id (String)
12 - 4b - symbol (String)
16 - 8b - order qty (F64)
24 - 8b - price (F64)
32 - 21b - transact_time (String)
"""
order_type = struct.unpack(">B", bs[0:1])[0]
if order_type != FIXTYPE_ORDER:
raise MarketSpreadError("Wrong Fix message type. Did you connect "
"the senders the wrong way around?")
side = struct.unpack(">B", bs[1:2])[0]
account = struct.unpack(">I", bs[2:6])[0]
order_id = struct.unpack("6s", bs[6:12])[0]
symbol = struct.unpack("4s", bs[12:16])[0]
qty = struct.unpack(">d", bs[16:24])[0]
price = struct.unpack(">d", bs[24:32])[0]
transact_time = struct.unpack("21s", bs[32:53])[0]
return Order(side, account, order_id, symbol, qty, price,
transact_time)
@wallaroo.decoder(header_length=4, length_fmt=">I")
def market_data_decoder(bs):
"""
0 - 1b - FixType (U8)
1 - 4b - symbol (String)
5 - 21b - transact_time (String)
26 - 8b - bid_px (F64)
34 - 8b - offer_px (F64)
"""
order_type = struct.unpack(">B", bs[0:1])[0]
if order_type != FIXTYPE_MARKET_DATA:
raise MarketSpreadError("Wrong Fix message type. Did you connect "
"the senders the wrong way around?")
symbol = struct.unpack(">4s", bs[1:5])[0]
transact_time = struct.unpack(">21s", bs[5:26])[0]
bid = struct.unpack(">d", bs[26:34])[0]
offer = struct.unpack(">d", bs[34:42])[0]
return MarketDataMessage(symbol, transact_time, bid, offer)
class OrderResult(object):
def __init__(self, order, last_bid, last_offer, timestamp):
self.order = order
self.bid = last_bid
self.offer = last_offer
self.timestamp = timestamp
@wallaroo.encoder
def order_result_encoder(data):
p = struct.pack(">HI6s4sddddQ",
data.order.side,
data.order.account,
data.order.order_id,
data.order.symbol,
data.order.qty,
data.order.price,
data.bid,
data.offer,
data.timestamp)
out = struct.pack(">I{}s".format(len(p)), len(p), p)
return out