mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-20 12:20:29 +08:00
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
import copy
|
|
import multiprocessing
|
|
import zmq
|
|
import logging
|
|
import json
|
|
|
|
from backtest.util import *
|
|
|
|
class TestClient(object):
|
|
|
|
def __init__(self,feed_address, sync_address):
|
|
self.logger = logging.getLogger()
|
|
self.feed_address = feed_address
|
|
self.sync_address = sync_address
|
|
|
|
def run(self):
|
|
|
|
self.logger.info("running the client")
|
|
self.context = zmq.Context()
|
|
|
|
self.data_feed = self.context.socket(zmq.PULL)
|
|
self.data_feed.connect(self.feed_address)
|
|
|
|
#synchronize with feed
|
|
sync_socket = self.context.socket(zmq.REQ)
|
|
sync_socket.connect(self.sync_address)
|
|
# send a synchronization request to the feed
|
|
sync_socket.send('')
|
|
# wait for synchronization reply from the feed
|
|
sync_socket.recv()
|
|
sync_socket.close()
|
|
|
|
counter = 0
|
|
prev_dt = None
|
|
while True:
|
|
msg = self.data_feed.recv()
|
|
counter += 1
|
|
if(msg == "DONE"):
|
|
self.logger.info("DONE!")
|
|
break
|
|
event = json.loads(msg)
|
|
if(prev_dt != None):
|
|
if(not event['dt'] >= prev_dt):
|
|
raise Exception("message arrived out of order: {date} after {prev}".format(date=event['dt'], prev=prev_dt))
|
|
|
|
prev_dt = event['dt']
|
|
if(counter % 100 == 0):
|
|
self.logger.info("received {n} messages".format(n=counter))
|
|
|
|
self.logger.info("received {n} messages".format(n=counter))
|
|
self.data_feed.close()
|
|
self.context.term() |