This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathiqscrape.py
80 lines (62 loc) · 2.71 KB
/
iqscrape.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
#!/usr/bin/env python
'''
Scrape data from a basket of stocks from IQfeed
'''
import os, sys, logging, optparse, time, csv
import pyqfeed.Client
import pyqfeed.Listener
class IQTestListener(pyqfeed.Listener.Listener):
def __init__(self, outfile=None):
self.outfile = outfile
if self.outfile:
self.fd = open(self.outfile, "wb")
else:
self.fd = sys.stdout
def on_error(self, message):
pass
def on_message(self, message):
if message.startswith('F'):
self.fd.write(message + "\n")
def loadSymbolsFromFile(filename, count=500, offset=0):
base, ext = os.path.splitext(filename)
symbols = []
if ext.lower() == ".csv":
for row in csv.reader(open(filename, "rb")):
symbols.append( row[0] )
return symbols[offset:]
def scrapeData(host, port, symbols, output_filename=None):
listener = IQTestListener(output_filename)
# Set up the IQfeed client.
client = pyqfeed.Client.Client((host, port))
client.start()
client.set_listener('', listener)
# Watch all the symbols we're interested in.
for symbol in symbols:
client.send("w%s" % symbol)
# Wait for disconnect or Ctrl-C
try:
while 1:
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
client.stop()
def main():
# Vanilla command-line arg parsing. Run with -h to see pretty output"
parser = optparse.OptionParser()
parser.add_option('-p', dest="port", type="int", default=5009, help="IQFeed service port. Default=5009")
parser.add_option('-s', dest="host", type="string", default="127.0.0.1", help="IQFeed service host. Default=localhost")
parser.add_option('-i', dest="input_file", type="string", help="Input file")
parser.add_option('-n', dest="count", type="int", default=500, help="Numbe of lines to read from input")
parser.add_option('--off', dest="offset", type="int", default=0, help="Offset in input file to read from")
parser.add_option('-o', dest="output_file", type="string", default=None, help="Save IQfeed output to this file")
parser.add_option("--debug", action="store_true", dest="debug", default=False, help="Debug this script?")
(options, args) = parser.parse_args()
if options.debug:
logging.basicConfig(level=logging.DEBUG)
if not options.input_file:
parser.error("Please specify an input file with -i")
symbols = loadSymbolsFromFile(options.input_file, options.count, options.offset)
scrapeData(options.host, options.port, symbols, options.output_file)
if __name__ == "__main__":
main()