Denis Tereshkin 5 years ago
parent
commit
03b2b310e6
  1. 48
      qhp-download-list.py
  2. 62
      qhp-download.py

48
qhp-download-list.py

@ -0,0 +1,48 @@
#!/usr/bin/env python3
import sys
import argparse
import zmq
import io
import json
import csv
import datetime
import struct
def main():
parser = argparse.ArgumentParser(description='QHP client')
parser.add_argument('-q', '--qhp', action='store', dest='qhp', help='QHP endpoint', required=True)
args = parser.parse_args()
ctx = zmq.Context.instance()
s = ctx.socket(zmq.REQ)
s.connect(args.qhp)
rq = {
"get_sec_list" : True,
}
s.send_multipart([bytes(json.dumps(rq), "utf-8")])
resp = s.recv()
if resp != b'OK':
errmsg = s.recv_string()
print("Error:", errmsg)
sys.exit(1)
rawdata = b''
while True:
if s.getsockopt(zmq.RCVMORE) == 0:
break
rawdata += s.recv()
s = rawdata.decode('utf-8')
tickers = s.split(',')
for ticker in tickers:
print(ticker)
if __name__ == '__main__':
main()

62
qhp-download.py

@ -44,7 +44,8 @@ class BarAggregator:
b_low = self.low b_low = self.low
b_close = self.close b_close = self.close
b_volume = self.volume b_volume = self.volume
b_timestamp = self.timestamp if self.current_bar_number is not None:
b_timestamp = datetime.datetime.fromtimestamp(self.current_bar_number * self.timeframe)
self.open_ = open_ self.open_ = open_
self.high = high self.high = high
@ -70,7 +71,7 @@ class BarAggregator:
b_low = self.low b_low = self.low
b_close = self.close b_close = self.close
b_volume = self.volume b_volume = self.volume
b_timestamp = self.timestamp b_timestamp = datetime.datetime.fromtimestamp(self.timeframe * ( self.timestamp.timestamp() // self.timeframe))
return (b_timestamp, b_open, b_high, b_low, b_close, b_volume) return (b_timestamp, b_open, b_high, b_low, b_close, b_volume)
@ -113,44 +114,53 @@ def main():
"timeframe" : period "timeframe" : period
} }
print("Sending request:", rq)
s.send_multipart([bytes(json.dumps(rq), "utf-8")]) s.send_multipart([bytes(json.dumps(rq), "utf-8")])
parts = s.recv_multipart() print("Awaiting response")
resp = s.recv()
print(parts[0]) print(resp)
if parts[0] != b'OK': if resp != b'OK':
print("Error:", parts[1]) errmsg = s.recv_string()
sys.exit(1) print("Error:", errmsg)
sys.exit(1)
line_count = 0 line_count = 0
with open(args.output_file, 'w', newline='') as f: with open(args.output_file, 'w', newline='') as f:
writer = csv.writer(f) writer = csv.writer(f)
writer.writerow(['<TICKER>', '<PER>', '<DATE>', '<TIME>', '<OPEN>', '<HIGH>', '<LOW>', '<CLOSE>', '<VOLUME>']) writer.writerow(['<TICKER>', '<PER>', '<DATE>', '<TIME>', '<OPEN>', '<HIGH>', '<LOW>', '<CLOSE>', '<VOLUME>'])
for line in struct.iter_unpack("<qddddQ", parts[1]):
timestamp = int(line[0]) while True:
open_ = float(line[1]) if s.getsockopt(zmq.RCVMORE) == 0:
high = float(line[2]) break
low = float(line[3]) rawdata = s.recv()
close = float(line[4]) print("Got chunk: {} bytes".format(len(rawdata)))
volume = int(line[5]) for line in struct.iter_unpack("<qddddQ", rawdata):
dt = datetime.datetime.utcfromtimestamp(timestamp) + timedelta
timestamp = int(line[0])
open_ = float(line[1])
high = float(line[2])
low = float(line[3])
close = float(line[4])
volume = int(line[5])
dt = datetime.datetime.utcfromtimestamp(timestamp) + timedelta
if agg:
mbar = agg.push_bar(dt, open_, high, low, close, volume)
if mbar is not None:
line_count += 1
writer.writerow([symbol, agg.timeframe, mbar[0].strftime('%Y%m%d'), mbar[0].strftime('%H%M%S'), str(mbar[1]), str(mbar[2]), str(mbar[3]), str(mbar[4]), str(mbar[5])])
else:
line_count += 1
writer.writerow([symbol, period, dt.strftime('%Y%m%d'), dt.strftime('%H%M%S'), str(open_), str(high), str(low), str(close), str(volume)])
if agg: if agg:
mbar = agg.push_bar(dt, open_, high, low, close, volume) mbar = agg.get_bar()
if mbar is not None: if mbar is not None:
line_count += 1 line_count += 1
writer.writerow([symbol, agg.timeframe, mbar[0].strftime('%Y%m%d'), mbar[0].strftime('%H%M%S'), str(mbar[1]), str(mbar[2]), str(mbar[3]), str(mbar[4]), str(mbar[5])]) writer.writerow([symbol, agg.timeframe, mbar[0].strftime('%Y%m%d'), mbar[0].strftime('%H%M%S'), str(mbar[1]), str(mbar[2]), str(mbar[3]), str(mbar[4]), str(mbar[5])])
else:
line_count += 1
writer.writerow([symbol, period, dt.strftime('%Y%m%d'), dt.strftime('%H%M%S'), str(open_), str(high), str(low), str(close), str(volume)])
if agg:
mbar = agg.get_bar()
if mbar is not None:
line_count += 1
writer.writerow([symbol, agg.timeframe, mbar[0].strftime('%Y%m%d'), mbar[0].strftime('%H%M%S'), str(mbar[1]), str(mbar[2]), str(mbar[3]), str(mbar[4]), str(mbar[5])])
print("Written {} lines".format(line_count)) print("Written {} lines".format(line_count))

Loading…
Cancel
Save