-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmakedict.py
81 lines (70 loc) · 2.25 KB
/
makedict.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
from ITCHController import ITCHController
import ITCHMessages
from multiprocessing import Process, Queue
'''
This is a pretty weak example, but I figured it would be very
useful real world.
I see a lot of requests for the entire list of stock symbols.
Every symbol will generate a Directory Message before the market
opens for the day. This example will grab each of those tickers
and write them to a file, giving you the complete list.
Ryan Day
'''
tickerfile = file("tickerlist","w")
def handleEvents(msg):
''' Handle system event messages '''
if msg.code == 'O':
print "Start of messages!"
elif msg.code == 'Q':
print "Market open, dictionary messages done!"
tickerfile.close()
def handleDict(msg):
tickerfile.write(msg.ticker.rstrip() + "\n")
'''
Now we can instantiate our ITCH Controller, and
register our handlers
'''
itch = ITCHController()
itch.AddHandler(ITCHMessages.StockDirectoryMessage, handleDict)
itch.AddHandler(ITCHMessages.SystemEventMessage, handleEvents)
p = Process(target=itch.run)
p.start()
'''
Our controller is now waiting for messages. So we can read
a data file (ftp://emi.nasdaq.com/ITCH/) and start processing
messages.
'''
cachesize = 16384000*4
data = file("partial", "rb")
haveData = True
ptr = buflen = 0
dataBuffer = data.read(cachesize)
buflen = len(dataBuffer)
while haveData is True:
byte = dataBuffer[ptr:ptr+1]
ptr = ptr + 1
if ptr == buflen:
''' If we are out of buffer '''
ptr = 0
dataBuffer = data.read(cachesize)
buflen = len(dataBuffer)
if byte == b'\x00':
length = ord(dataBuffer[ptr:ptr+1])
ptr = ptr + 1
if (ptr+length) > buflen:
''' If we don't have the entire message in our buffer '''
temp = dataBuffer[ptr:buflen]
dataBuffer = temp + data.read(cachesize)
buflen = len(dataBuffer)
ptr = 0
message = dataBuffer[ptr:ptr+length]
ptr = ptr + length
itch.messageQueue.put(message)
if ptr == buflen:
''' Finally, check to see if we hit the end of the buffer '''
ptr = 0
dataBuffer = data.read(cachesize)
buflen = len(dataBuffer)
else:
print "Byte was %d" % ord(byte)