Exemplo n.º 1
0
    def subscribe_data(self, code, frequence, data_host, data_port, data_user, data_password, model='py'):
        """[summary]

        Arguments:
            code {[type]} -- [description]
            frequence {[type]} -- [description]
        """

        if frequence.endswith('min'):
            if model == 'py':
                self.sub = subscriber(exchange='realtime_{}_{}'.format(
                    frequence, code), host=data_host, port=data_port, user=data_user, password=data_password)
            elif model == 'rust':
                self.sub = subscriber_routing(exchange='realtime_{}'.format(
                    code), routing_key = frequence, host=data_host, port=data_port, user=data_user, password=data_password)
            self.sub.callback = self.callback
        elif frequence.endswith('s'):


            import re
            self._num_cached  = 2*int(re.findall(r'\d+',self.frequence)[0])
            self.sub = subscriber_routing(exchange='CTPX', routing_key=code, host=data_host, port=data_port, user=data_user, password=data_password)
            self.sub.callback = self.second_callback
        elif frequence.endswith('tick'):
            self._num_cached = 1
            self.sub = subscriber_routing(exchange='CTPX', routing_key=code, host=data_host, port=data_port, user=data_user, password=data_password)
            self.sub.callback = self.tick_callback
Exemplo n.º 2
0
    def subscribe_multi(self, codelist, frequence, data_host, data_port, data_user, data_password, model='py'):
        if frequence.endswith('min'):
            if model == 'rust':
                self.sub = subscriber_routing(exchange='realtime_{}'.format(
                    codelist[0]), routing_key=frequence, host=data_host, port=data_port, user=data_user, password=data_password)
                for item in codelist[1:]:
                    self.sub.add_sub(exchange='realtime_{}'.format(
                        item), routing_key=frequence)
            elif model == 'py':
                self.sub = subscriber_routing(exchange='realtime_{}'.format(
                    codelist[0].lower()), routing_key=frequence, host=data_host, port=data_port, user=data_user, password=data_password)
                for item in codelist[1:]:
                    print(item)
                    self.sub.add_sub(exchange='realtime_{}'.format(
                        item.lower()), routing_key=frequence)
            self.sub.callback = self.callback
        elif frequence.endswith('tick'):

            self._num_cached = 1
            self.sub = subscriber_routing(exchange='CTPX', routing_key=codelist[0].lower(), host=data_host, port=data_port, user=data_user, password=data_password)
            for item in codelist[1:]:
                print(item)
                self.sub.add_sub(exchange='CTPX', routing_key=item.lower())

            self.sub.callback = self.tick_callback
Exemplo n.º 3
0
def debug_sub(exchange, model, routing_key, user, password, host):
    if model == 'fanout':
        subscriber(host=host, user=user, password=password,
                   exchange=exchange).start()
    elif model == 'direct':
        subscriber_routing(host=host,
                           user=user,
                           password=password,
                           exchange=exchange,
                           routing_key=routing_key).start()
Exemplo n.º 4
0
 def __init__(self,
              delay=10.5,
              date: datetime.datetime = None,
              log_dir='./log',
              debug=False):
     super().__init__(name='QA_REALTIME_COLLECTOR_STOCK_BAR',
                      thread_num=None,
                      timeout=0.5)
     cur_time = datetime.datetime.now() if date is None else date
     # set qa_tdx_excutor is debug mode
     self.debug = debug
     self.cur_year = cur_time.year
     self.cur_month = cur_time.month
     self.cur_day = cur_time.day
     self.isRequesting = False
     self.delay = delay  # 数据获取请求间隔
     self.code_list = []
     self.sub = subscriber_routing(host=eventmq_ip,
                                   exchange='QARealtime_Market',
                                   routing_key='stock')
     self.sub.callback = self.callback
     self.pub = publisher(host=eventmq_ip, exchange='realtime_stock_min')
     self.log_dir = log_dir
     self.pre_market_data = None
     self.last_update_time = cur_time
     threading.Thread(target=self.sub.start, daemon=True).start()
     logger.info("QA_REALTIME_COLLECTOR_STOCK_BAR INIT, delay %s" %
                 self.delay)
Exemplo n.º 5
0
 def __init__(self, username, password):
     super().__init__(name='QAREALTIME_COLLECTOR_STOCK')
     self.user = QA_User(username=username, password=password)
     self.sub = subscriber_routing(exchange='QARealtime_Market',
                                   routing_key='stock')
     self.sub.callback = self.callback
     threading.Thread(target=self.sub.start, daemon=True).start()
Exemplo n.º 6
0
 def __init__(self):
     super().__init__()
     self.subscriber = subscriber_routing(
         host='192.168.2.116',
         exchange='AcMD.FullData',
         durable=True,
         vhost='/',
         routing_key='All')
 def __init__(self):
     super().__init__(name='QAREALTIME_COLLECTOR_STOCK')
     self.codelist = []
     self.sub = subscriber_routing(host=eventmq_ip,
                                   exchange='QARealtime_Market',
                                   routing_key='stock')
     self.sub.callback = self.callback
     self.pub = publisher(host=eventmq_ip, exchange='stocktransaction')
     threading.Thread(target=self.sub.start, daemon=True).start()
Exemplo n.º 8
0
 def __init__(self):
     super().__init__()
     self.subscriber = subscriber_routing(
         host='192.168.2.116',
         exchange='AcMD.Monitor',
         durable=True,
         vhost='/',
         routing_key='All')
     self.db  = pymongo.MongoClient().qa.monitor
Exemplo n.º 9
0
def sub(code):
    x = subscriber_routing(exchange='CTPX', routing_key=code)

    import json

    def callback(a, b, c, data):
        print(json.loads(data))

    x.callback = callback
    x.start()
Exemplo n.º 10
0
 def __init__(self, simid, realid, realamount=1):
     self.sub = subscriber_routing(exchange='QAORDER_ROUTER',
                                   host=eventmq_ip,
                                   routing_key=simid)
     self.pub = publisher_routing(exchange='QAORDER_ROUTER',
                                  host=eventmq_ip,
                                  routing_key=realid)
     self.realamount = realamount
     self.realid = realid
     self.simid = simid
Exemplo n.º 11
0
    def __init__(self, code, host='192.168.2.116', port=5672):
        super().__init__()

        code = 'SH' + code if code.startswith('6') else 'Z$' + code
        self.subscriber = subscriber_routing(host=host,
                                             port=port,
                                             exchange='AcMD.L2',
                                             durable=True,
                                             vhost='/',
                                             routing_key=code)
        self.mgdb = pymongo.MongoClient().qa.LEVEL2
 def __init__(self, code):
     self.data = {}
     self.min5_data = {}
     self.pro = producer.publisher(host=eventmq_ip, exchange='1min_{}'.format(code), 
             user=market_data_user, password=market_data_password)
     self.pro_realtimemin = producer.publisher(host=eventmq_ip, exchange='realtime_min_{}'.format(
         code), user=market_data_user, password=market_data_password)
     self.is_send = False
     self.last_volume = 0
     self.c = consumer.subscriber_routing(host=eventmq_ip,
                                          exchange='CTPX', routing_key=code, user=market_data_user, password=market_data_password)
 def __init__(self, delay=30.5):
     super().__init__(name='QA_REALTIME_COLLECTOR_STOCK_BAR')
     # 数据获取请求间隔
     self.isOK = True
     self.delay = delay
     self.codelist = []
     self.sub = subscriber_routing(host=eventmq_ip,
                                   exchange='QARealtime_Market',
                                   routing_key='stock')
     self.sub.callback = self.callback
     self.pub = publisher(host=eventmq_ip, exchange='realtime_stock_min')
     print("QA_REALTIME_COLLECTOR_STOCK_BAR INIT, delay %s" % self.delay)
     threading.Thread(target=self.sub.start, daemon=True).start()
    def __init__(self,
                 frequency='5min',
                 date: datetime.datetime = None,
                 log_dir='./log'):
        """
        暂时不支持单个股票重采样
        :param frequency:
        """
        super().__init__()
        logger.info("QA实时股票Bar重采样,初始化...周期: %s" % frequency)
        if isinstance(frequency, float):
            self.frequency = int(frequency)
        elif isinstance(frequency, str):
            _frequency = frequency.replace('min', '')
            if str.isnumeric(_frequency):
                self.frequency = int(_frequency)
            else:
                logger.error("不支持的周期 unknownFrequency: %s" % frequency)
                return
        elif isinstance(frequency, int):
            self.frequency = frequency
        else:
            logger.error("不支持的周期 unknownFrequency: %s" % frequency)
            return

        self.market_data = None

        # 接收stock tick 数据
        self.sub = subscriber(host=eventmq_ip, exchange='realtime_stock_min')
        self.sub.callback = self.on_message_callback
        self.stock_sub = subscriber_routing(host=eventmq_ip,
                                            exchange='QARealtime_Market',
                                            routing_key='stock')
        self.stock_sub.callback = self.on_stock_subscribe_message_callback
        # 发送重采样的数据
        self.pub = publisher(host=eventmq_ip,
                             exchange='realtime_stock_{}_min'.format(
                                 self.frequency))
        self.count = 0
        self.code_list = []
        cur_time = datetime.datetime.now() if date is None else date
        self.cur_year = cur_time.year
        self.cur_month = cur_time.month
        self.cur_day = cur_time.day
        # 多进程计算
        self.cpu_count = multiprocessing.cpu_count() - 1
        self.log_dir = log_dir
        threading.Thread(target=self.sub.start, daemon=True).start()
        threading.Thread(target=self.stock_sub.start, daemon=True).start()
Exemplo n.º 15
0
def debug_sub(exchange, model, routing_key, user, password, host, port,
              durable):
    if model == 'fanout':
        subscriber(host=host,
                   port=port,
                   user=user,
                   password=password,
                   exchange=exchange).start()
    elif model == 'direct':
        subscriber_routing(host=host,
                           port=port,
                           user=user,
                           password=password,
                           exchange=exchange,
                           routing_key=routing_key,
                           durable=durable).start()
    elif model == 'topic':
        subscriber_topic(host=host,
                         port=port,
                         user=user,
                         password=password,
                         exchange=exchange,
                         routing_key=routing_key,
                         durable=durable).start()
Exemplo n.º 16
0
def node(shell_cmd):
    """run shell
    Arguments:
        shell_cmd {[type]} -- [description]
    Node
    """
    node_id = uuid.uuid4()
    listener = subscriber_routing(host=eventmq_ip,
                                  port=eventmq_port,
                                  user=eventmq_username,
                                  password=eventmq_password,
                                  exchange='qaschedule',
                                  routing_key=str(node_id))
    listener.callback = callback
    listener.start()
Exemplo n.º 17
0
    def __init__(self, username, password, model="SIM", broker_name="QAPaperTrading", trade_host=mongo_ip, init_cash=1000000,
                 eventmq_ip=eventmq_ip, eventmq_port=eventmq_port):
        super().__init__(username, password, model, broker_name, trade_host, init_cash)

        self.pub = producer.publisher_routing(
            exchange='qamdgateway', host=eventmq_ip, port=eventmq_port, durable=True)
        self.subrealtime = subscriber_topic(
            host=eventmq_ip, port=eventmq_port, exchange='QASMG', routing_key=username)

        self.ordersub = subscriber_routing(
            host=eventmq_ip, port=eventmq_port, exchange='QAORDER_ROUTER', routing_key=username)

        self.subrealtime.callback = self.realtime_data_callback
        self.ordersub.callback = self.listen_order_callback

        threading.Thread(target=self.subrealtime.start).start()
        threading.Thread(target=self.ordersub.start).start()

        self.pub_acc = producer.publisher_topic(
            exchange='qaStandardAcc', host=eventmq_ip, port=eventmq_port,  durable=True)
    def __init__(self, code, subexchange='tick'):
        self.data = {}
        self.is_send = False
        self.last_volume = 0

        self.pro = producer.publisher(exchange='bar_1min_{}'.format(code),
                                      user='******',
                                      password='******',
                                      host=eventmq_ip)
        self.pro_realtimemin = producer.publisher(
            exchange='realtime_min_{}'.format(code),
            user='******',
            password='******',
            host=eventmq_ip)
        self.c = consumer.subscriber_routing(exchange='tick',
                                             routing_key=code,
                                             user='******',
                                             password='******',
                                             host=eventmq_ip)

        print('start ctptick collector {}'.format(code))
Exemplo n.º 19
0
    def __init__(self, account_cookie, password, wsuri, broker_name='simnow', portfolio='default',
                 eventmq_ip=eventmq_ip, eventmq_port=eventmq_port, sig=True, ping_gap=1,
                 bank_password=None, capital_password=None, appid=None,
                 if_restart=True, taskid=None, database_ip = mongo_ip):

        super().__init__(name='QATRADER_{}'.format(account_cookie))
        self.account_cookie = account_cookie
        self.password = password
        self.broker_name = broker_name
        self.wsuri = wsuri
        self.pub = producer.publisher_routing(
            exchange=trade_server_account_exchange, host=eventmq_ip, port=eventmq_port)
        self.ws = self.generate_websocket()
        """几个涉及跟数据库交互的client
        """
        database = pymongo.MongoClient(mongo_ip)
        self.account_client = database.QAREALTIME.account
        self.settle_client = database.QAREALTIME.history
        self.xhistory = database.QAREALTIME.hisaccount
        self.orders_client = database.QAREALTIME.orders
        self.portfolio = portfolio
        self.connection = True
        self.message = {'password': password, 'wsuri': wsuri, 'broker_name': broker_name, 'portfolio': portfolio, 'taskid': taskid, 'updatetime': str(
            datetime.datetime.now()), 'accounts': {}, 'orders': {}, 'positions': {}, 'trades': {}, 'banks': {}, 'transfers': {}, 'event': {},  'eventmq_ip': eventmq_ip,
            'ping_gap': ping_gap, 'account_cookie': account_cookie, 'bank_password': bank_password, 'capital_password': capital_password, 'settlement': {},
            'bankid': 0, 'money': 0, 'investor_name': ''}
        self.last_update_time = datetime.datetime.now()
        self.sig = sig
        self.if_restart = if_restart
        self.ping_gap = ping_gap
        self.bank_password = bank_password
        self.capital_password = capital_password
        self.tempPass = ''
        self.appid = appid

        self.sub = consumer.subscriber_routing(host=eventmq_ip, port=eventmq_port, user=trade_server_user, password=trade_server_password,
                                               exchange=trade_server_order_exchange, routing_key=self.account_cookie)
        self.pub_transaction = producer.publisher_routing(
            host=eventmq_ip, exchange='QATRANSACTION')
        self.sub.callback = self.callback
    def __init__(self):

        super().__init__()
        self.ws = websocket.WebSocketApp('wss://openmd.shinnytech.com/t/md/front/mobile',
                                         on_pong=on_pong,
                                         on_message=self.on_message,
                                         on_error=on_error,
                                         on_close=on_close)

        def _onopen(ws):
            def run():
                ws.send(peek())
            threading.Thread(target=run, daemon=False).start()

        self.quoteclient = pymongo.MongoClient(host=mongo_ip).QAREALTIME.realtimeQuote
        self.ws.on_open = _onopen
        self.data = {}
        self.subscribe_list = ['SHFE.rb1910', 'DCE.j1909']
        self.sub = subscriber_routing(host=eventmq_ip, exchange='QARealtime_Market', routing_key='future')
        self.sub.callback = self.callback
        threading.Thread(target=self.ws.run_forever,
                         name='market_websock', daemon=False).start()
        threading.Thread(target=self.sub.start,
                         name='market_subscriber', daemon=True).start()
Exemplo n.º 21
0
#!/usr/bin/env python

import QAPUBSUB
import threading

from QAPUBSUB.consumer import subscriber_routing
from QAPUBSUB.producer import publisher_routing

z1 = subscriber_routing(exchange='xx', routing_key='x1')
z2 = subscriber_routing(exchange='xx', routing_key='x2')
z3 = subscriber_routing(exchange='xx', routing_key='x2')

z1.callback = lambda a, b, c, x: print('FROM X1 {}'.format(x))
z2.callback = lambda a, b, c, x: print('FROM X2 {}'.format(x))
z3.callback = lambda a, b, c, x: print('FROM X3 {}'.format(x))
p = publisher_routing(exchange='xx')

threading.Thread(target=z1.start).start()

threading.Thread(target=z2.start).start()
threading.Thread(target=z3.start).start()
p.pub('xxxxx', routing_key='x1')

p.pub('1', routing_key='x2')
"""
在exchange为 xx的mq中


routing_key = x1 ==>  有一个订阅者 z1
routing_key = x2 ==>  有两个订阅者 z2, z3
Exemplo n.º 22
0
from QAPUBSUB import consumer
import json

c = consumer.subscriber_routing(exchange='QARealtime_Market', routing_key='0')


# c = consumer.subscriber(exchange='ctp')
def callback(a, b, c, data):
    data = json.loads(data)
    print(data)
    if data['topic'] == 'subscribe':
        print('receive new subscribe: {}'.format(data['code']))
        new_ins = data['code'].replace('_', '.').split(',')

        import copy
        if isinstance(new_ins, list):
            for item in new_ins:
                print(item)
                # self.subscribe(item)
    #     else:
    #         self.subscribe(new_ins)
    # if data['topic'] == 'unsubscribe':
    #     print('receive new unsubscribe: {}'.format(data['code']))
    #     new_ins = data['code'].replace('_', '.').split(',')

    #     import copy
    #     if isinstance(new_ins, list):
    #         for item in new_ins:
    #             self.unsubscribe(item)
    #     else:
    #         self.unsubscribe(new_ins)
Exemplo n.º 23
0
from QAPUBSUB import consumer

c = consumer.subscriber_routing(user='******',
                                password='******',
                                host='192.168.2.116',
                                routing_key='100002',
                                exchange='QAACCOUNT')
c.callback = lambda x, y, body, z: print(z)
c.start()
Exemplo n.º 24
0
def test_sub(block_id):
    # block_id = 'udf'
    c = consumer.subscriber_routing(
        exchange='realtime_block_{}'.format(block_id), routing_key=block_id)
    c.callback = lambda a, b, c, data: print(data)
    c.start()