forked from HFTConsultancy/FIPO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseItch.py
131 lines (113 loc) · 3.99 KB
/
parseItch.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
#!/usr/bin/python3
#/usr/bin/env python3
import struct
from Itch41 import *
#### Parameters for Execution
# Download from here: ftp://emi.nasdaq.com/ITCH/11092013.NASDAQ_ITCH41.gz
fileName = "/Users/john/Downloads/11092013.NASDAQ_ITCH41"
#fileName = "11092013.NASDAQ_ITCH41"
outputFile = "Itch.dat"
saveMessageTypes = [ 'A' ]
numberOfMessagesToSave = 2
#fileName = "Itch.dat"
def createUnitTestCode(message, rawBytes):
lineLen = 8
conv = [ "{0:#0{1}x}".format(x, 4) for x in rawBytes]
#line = "\n\t- ".join( [ " ".join( conv[i:i+lineLen] ) for i in range(0, len(conv), lineLen) ] )
lines = [ ", ".join( conv[i:i+lineLen] ) for i in range(0, len(conv), lineLen) ]
print("\n\tdef test_create_{}(self):".format( MessageType(message.MessageType) ))
print("\t\t# GIVEN")
print("\t\trawMessage = bytearray()")
for line in lines:
print("\t\trawMessage.extend( [ {0} ] )".format(line))
print("\n\t\t# WHEN")
print("\t\tmessage = ItchMessageFactory.createFromBytes( rawMessage )")
print("\n\t\t# THEN")
for spec in message.specs:
leftVal = getattr(message, spec[3])
rightVal = "message." + spec[3]
if spec[2] is str:
if spec[1] == 1:
leftVal = "'" + leftVal + "'"
else:
leftVal = "\"" + leftVal + "\""
print("\t\tself.assertEqual( {:>10}, {:<30} )".format( leftVal, rightVal ))
print("\n")
global counter
counter = 0
global orderBook
orderBook = { }
def OrderBook(itchMessage):
global counter
global orderBook
counter += 1
ticker = "AAPL"
messageType = MessageType( itchMessage.getValue( Field.MessageType ))
if messageType == MessageType.AddOrder or messageType == MessageType.AddOrderWithMPID:
if itchMessage.getValue( Field.Stock ) == ticker:
orderRefNum = itchMessage.getValue(Field.OrderRefNum)
if orderRefNum in orderBook:
print("Already exists")
orderBook[orderRefNum] = itchMessage
if counter == 1500000:
print("Number of messages in orderbook: {}".format( len(orderBook.keys() )))
for orderRefNum in orderBook.keys():
#orderRefNum = order.getValue(Field.OrderRefNum)
order = orderBook[orderRefNum]
price = order.getValue(Field.Price)
shares = order.getValue(Field.Shares)
#print("OrderRefNum: {}, Price: {}, Shares: {}".format( orderRefNum, price, shares))
return True
return False
global uniqueCounter
uniqueCounter = { }
def dumpOneOfEach(itchMessage):
global uniqueCounter
messageType = MessageType( itchMessage.getValue( Field.MessageType ))
if not messageType in uniqueCounter:
#itchMessage.dumpPretty()
#itchMessage.dumpRawBytes()
createUnitTestCode( itchMessage, itchMessage.rawMessage )
uniqueCounter[messageType] = itchMessage
if len(uniqueCounter.keys()) == 18:
return True
return False
#fptr = OrderBook
fptr = dumpOneOfEach
cacheSize = 1024
fin = open(fileName, "rb")
buffer = fin.read(cacheSize)
bufferLen = len(buffer)
ptr = 0
haveData = True
while haveData:
byte = buffer[ptr:ptr+1]
ptr += 1
if ptr == bufferLen:
ptr = 0
buffer = fin.read(cacheSize)
bufferLen = len(buffer)
if len(byte) == 0:
print("BREAK-len(byte) == 0")
break
if byte == b'\x00':
length = ord(buffer[ptr:ptr+1])
ptr += 1
if (ptr+length) > bufferLen:
temp = buffer[ptr:bufferLen]
buffer = temp + fin.read(cacheSize)
bufferLen = len(buffer)
ptr = 0
message = buffer[ptr:ptr+length]
ptr += length
import struct
preamble = struct.pack("!h", length)
rawMessage = preamble + message
itchMessage = ItchMessageFactory.createFromBytes(rawMessage)
if fptr(itchMessage):
break
if ptr == bufferLen:
ptr = 0
buffer = fin.read(cacheSize)
bufferLen = len(buffer)
fin.close()