This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
order_analysis.py
165 lines (143 loc) · 6.04 KB
/
order_analysis.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, time
from collections import defaultdict
from pitch import PitchMessageReader
from pitch import TIME, ADD_ORDER_L, ADD_ORDER_S, ORDER_EXECUTED, ORDER_EXECUTED_AT_PRICE_SIZE, REDUCE_SIZE_L
from pitch import REDUCE_SIZE_S, MODIFY_ORDER_S, MODIFY_ORDER_L, DELETE_ORDER, TRADE_L, TRADE_S, TRADE_BREAK, END_OF_SESSION
class Order(object):
def __init__(self, order_id, symbol, side, size, price):
self.order_id, self.symbol, self.side, self.size, self.price = order_id, symbol, side, size, price
self.entry_at_top = False
self.entry_time = None
self.exit_time = None
def __repr__(self):
return '%s %s' % (self.__class__.__name__, self.__dict__)
class NaiveOrderBook(object):
def __init__(self, symbol):
self.symbol = symbol
self.bids_by_price = defaultdict(dict)
self.asks_by_price = defaultdict(dict)
self.ask_min = sys.maxint
self.bid_max = 0
def __repr__(self):
return '%s %s %s<->%s' % (self.__class__.__name__, self.symbol, self.bid_max, self.ask_min)
def add_order(self, order, msg_time):
if order.entry_time is None:
order.entry_time = msg_time
order_price = order.price
if order.side == 'B':
self.bids_by_price[order_price][order.order_id] = order
if order_price >= self.bid_max:
order.entry_at_top = True
if order_price > self.bid_max:
for o in self.bids_by_price[self.bid_max].values():
if o.exit_time is None:
o.exit_time = msg_time
self.bid_max = order_price
else:
self.asks_by_price[order_price][order.order_id] = order
if order_price <= self.ask_min:
order.entry_at_top = True
if order_price < self.ask_min:
for o in self.asks_by_price[self.ask_min].values():
if o.exit_time is None:
o.exit_time = msg_time
self.ask_min = order_price
def remove_order(self, order, msg_time):
if order.exit_time is None:
order.exit_time = msg_time
order_price = order.price
if order.side == 'B':
orders = self.bids_by_price[order_price]
del orders[order.order_id]
if len(orders) == 0:
del self.bids_by_price[order_price]
if order_price >= self.bid_max:
prices = sorted(self.bids_by_price.keys(), reverse=True)
if prices:
self.bid_max = prices[0]
else:
self.bid_max = 0
else:
orders = self.asks_by_price[order_price]
del orders[order.order_id]
if len(orders) == 0:
del self.asks_by_price[order_price]
if order_price <= self.ask_min:
prices = sorted(self.asks_by_price.keys())
if prices:
self.ask_min = prices[0]
else:
self.ask_min = sys.maxint
def main():
start = time.time()
n = 0
books = {}
orders = {}
abs_time = 0
with PitchMessageReader(sys.stdin) as reader:
while True:
msg = reader.read_message()
if msg is None:
break
if msg.type in (TRADE_L, TRADE_S, TRADE_BREAK, END_OF_SESSION):
continue
if msg.type == TIME:
abs_time = msg.time
continue
#symbol = getattr(msg, 'symbol', getattr(orders.get(getattr(msg, 'order_id', None)), 'symbol', None))
#if symbol != 'PGm ':
# continue
#print books.get(symbol)
#print msg
n += 1
msg_time = abs_time + msg.time_offset / 1000000000.0
if msg.type in (ADD_ORDER_L, ADD_ORDER_S):
order = orders[msg.order_id] = Order(msg.order_id, msg.symbol, msg.side, msg.shares, msg.price)
book = books.get(order.symbol)
if book is None:
book = books[order.symbol] = NaiveOrderBook(order.symbol)
book.add_order(order, msg_time)
continue
order = orders[msg.order_id]
book = books[order.symbol]
if msg.type in (MODIFY_ORDER_L, MODIFY_ORDER_S):
order.size = msg.shares
if order.price != msg.price:
book.remove_order(order, msg_time)
order.price = msg.price
book.add_order(order, msg_time)
elif order.size <= 0:
book.remove_order(order, msg_time)
continue
if msg.type == DELETE_ORDER:
book.remove_order(order, msg_time)
continue
if msg.type == ORDER_EXECUTED:
order.size -= msg.executed_shares
if order.size <= 0:
book.remove_order(order, msg_time)
continue
if msg.type == ORDER_EXECUTED_AT_PRICE_SIZE:
order.size -= msg.executed_shares
if msg.remaining_shares <= 0:
book.remove_order(order, msg_time)
continue
if msg.type in (REDUCE_SIZE_L, REDUCE_SIZE_S):
order.size -= msg.canceled_shares
if order.size <= 0:
book.remove_order(order, msg_time)
continue
duration_by_symbol = defaultdict(float)
count_by_symbol = defaultdict(int)
for o in orders.values():
if o.entry_at_top:
duration_by_symbol[o.symbol] += (o.exit_time - o.entry_time)
count_by_symbol[o.symbol] += 1
print 'symbol avg duration'
for s, d in duration_by_symbol.items():
print '%s %12.2f' % (s, d / count_by_symbol[s])
print 'Parsed %s message(s) in %s sec(s).' % (n, (time.time() - start))
if __name__ == '__main__':
main()