示例#1
0
 def subscribe_at_startup(self):
     self.started_at_startup = True
     stock_api.subscribe_stock(self.reader, self.code,
                               self.tick_data_handler)
     stock_api.subscribe_stock_bidask(self.reader, self.code,
                                      self.ba_data_handler)
     stock_api.subscribe_stock_subject(self.reader, self.code,
                                       self.subject_handler)
示例#2
0
 def RequestCybosSubject(self, request, context):
     global is_subscribe_subject
     if request.code not in is_subscribe_subject:
         stock_api.subscribe_stock_subject(morning_client.get_reader(),
                                           request.code,
                                           self.handle_subject_tick)
         _LOGGER.info('Start Subscribe Subject %s', request.code)
         is_subscribe_subject[request.code] = True
     else:
         _LOGGER.info('Already Subscribe Subject %s', request.code)
     return Empty()
示例#3
0
 def start_watch(self):
     print('request min data')
     min_data = stock_api.request_stock_today_data(self.reader, self.code)
     print('today min len', len(min_data))
     if len(min_data) > 0:
         print('Subscribe Start', self.code)
         self.min_data = min_data
         stock_api.subscribe_stock(self.reader, self.code,
                                   self.tick_data_handler)
         stock_api.subscribe_stock_bidask(self.reader, self.code,
                                          self.ba_data_handler)
         stock_api.subscribe_stock_subject(self.reader, self.code,
                                           self.subject_handler)
         return True
     return False
示例#4
0
def consumer():
    global message_reader
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = (message.SERVER_IP, message.CLIENT_SOCKET_PORT)
    sock.connect(server_address)

    message_reader = stream_readwriter.MessageReader(sock)
    message_reader.start()

    #stock_api.subscribe_trade(message_reader, display_trade_result)
    while True:
        val = q.get(True)
        command = val.decode('ascii').rstrip()
        #print(command)

        if command == 'long':
            print(stock_api.request_long_list(message_reader))
        elif command.startswith('stats'):
            print(stock_api.request_subscribe_stat(message_reader))
        elif command.startswith('subscribe_code'):
            codes = stock_api.request_subscribe_codes(message_reader)
            for code in codes:
                if len(code) > 10 or not code.startswith('A'):
                    print(code)
        elif command.startswith('statc'):
            cinfo = stock_api.request_collector_stat(message_reader)
            print('total collector', len(cinfo))
            print('total subscribe count', sum([c['subscribe_count'] for c in cinfo]))
            for i, ci in enumerate(cinfo):
                print(i, '\t', 'name', ci['name'], 'vendor', ci['vendor'], 'subscribe count', ci['subscribe_count'])
        elif command.startswith('trade_subscribe'):
            stock_api.subscribe_trade(message_reader, display_trade_result)
        elif command.startswith('trade_stop_subscribe'):
            stock_api.stop_subscribe_trade(message_reader)
        elif command.startswith('min_data'):
            min_detail = command.split(',')
            if len(min_detail) != 2:
                print('min_data,code')
            else:
                result = stock_api.request_stock_minute_data(message_reader, min_detail[1], date(2020,2,3), date(2020,2,3))
                print(result)
        elif command.startswith('todaym'):
            todaym_detail = command.split(',')
            if len(todaym_detail) != 2:
                print('todaym,code')
            else:
                result = stock_api.request_stock_today_data(message_reader, todaym_detail[1])
                if len(result) > 1:
                    print('DATA LEN', len(result))
                    print('HEAD', result[0])
                    print(result[1])
                    print('TAIL', result[-1])

                print(result)
        elif command.startswith('todayt'):
            todaym_detail = command.split(',')
            if len(todaym_detail) != 2:
                print('todayt,code')
            else:
                result = stock_api.request_stock_today_tick_data(message_reader, todaym_detail[1])
                if len(result) > 1:
                    print('DATA LEN', len(result))
                    print('HEAD', result[0])
                    print(result[1])
                    print('TAIL', result[-1])
        elif command.startswith('buy') or command.startswith('sell'):
            buy_detail = command.split(',')
            print(buy_detail)
            if len(buy_detail) != 4:
                print('buy|sell,code,price,quantity')
            else:
                is_buy = True
                if buy_detail[0] == 'buy':
                    pass
                elif buy_detail[0] == 'sell':
                    is_buy = False
                else:
                    print('wrong buy/sell command')
                    continue
                code = buy_detail[1]
                price = int(buy_detail[2])
                quantity = int(buy_detail[3])
                result = stock_api.order_stock(message_reader, code, price, quantity, is_buy)
                print(result)
        elif command.startswith('modify'):
            modify_detail = command.split(',')
            if len(modify_detail) != 4:
                print('modify,order_num,code,price')
            else:
                order_num = int(modify_detail[1])
                code = modify_detail[2]
                price = int(modify_detail[3])
                result = stock_api.modify_order(message_reader, order_num, code, price)
                print(result)
        elif command.startswith('cancel'):
            cancel_detail = command.split(',')
            if len(cancel_detail) != 4:
                print('cancel,order_num,code,amount')
            else:
                order_num = int(cancel_detail[1])
                code = cancel_detail[2]
                amount = int(cancel_detail[3])
                result = stock_api.cancel_order(message_reader, order_num, code, amount)
                print(result)
        elif command.startswith('queue'):
            print(stock_api.request_order_in_queue(message_reader))
        elif command.startswith('balance'):
            print(stock_api.get_balance(message_reader)['balance'])
        elif command.startswith('subject'):
            subject_detail = command.split(',')
            if len(subject_detail) != 2:
                print('subject,code')
            else:
                code = subject_detail[1]
                stock_api.subscribe_stock_subject(message_reader, code, display_subject_data)
        elif command.startswith('stop_subject'):
            subject_detail = command.split(',')
            if len(subject_detail) != 2:
                print('stop_subject,code')
            else:
                code = subject_detail[1]
                stock_api.stop_subscribe_stock_subject(message_reader, code)
        elif command.startswith('stop_bidask'):
            bidask_detail = command.split(',')
            if len(bidask_detail) != 2:
                print('stop_bidask,code')
            else:
                code = bidask_detail[1]
                stock_api.stop_subscribe_stock_bidask(message_reader, code)
        elif command.startswith('bidask'):
            bidask_detail = command.split(',')
            if len(bidask_detail) != 2:
                print('bidask,code')
            else:
                code = bidask_detail[1]
                stock_api.subscribe_stock_bidask(message_reader, code, display_bidask_data)
        elif command.startswith('stock'):
            stock_detail = command.split(',')
            if len(stock_detail) != 2:
                print('stock,code')
            else:
                code = stock_detail[1]
                stock_api.subscribe_stock(message_reader, code, display_stock_data)
        elif command.startswith('stop_stock'):
            stock_detail = command.split(',')
            if len(stock_detail) != 2:
                print('stop_stock,code')
            else:
                code = stock_detail[1]
                stock_api.stop_subscribe_stock(message_reader, code)
        elif command.startswith('req'):
            req_detail = command.split(',')
            if len(req_detail) != 2:
                print('req,code')
            else:
                code = req_detail[1]
                print(stock_api.request_stock_day_data(message_reader, code, date(2020,1,31), date(2020,1,31)))
        elif command.startswith('index'):
            index_detail = command.split(',')
            if len(index_detail) != 2:
                print('index,code')
            else:
                code = index_detail[1]
                stock_api.subscribe_index(message_reader, code, display_index_data)
        elif command.startswith('stop_index'):
            index_detail = command.split(',')
            if len(index_detail) != 2:
                print('index,code')
            else:
                code = index_detail[1]
                stock_api.stop_subscribe_index(message_reader, code)
        elif command.startswith('abroad'):
            abroad_detail = command.split(',')
            if len(abroad_detail) != 2:
                print('abroad,code')
            else:
                code = abroad_detail[1]
                print(stock_api.request_abroad_data(message_reader, code, message.PERIOD_DAY, 30))
        elif command.startswith('uscode'):
            #print(stock_api.request_us_stock_code(message_reader, message.USTYPE_ALL))
            print(len(stock_api.request_us_stock_code(message_reader, message.USTYPE_ALL)))
        elif command.startswith('world'):
            world_detail = command.split(',')
            if len(world_detail) != 2:
                print('world,code')
            else:
                code = world_detail[1]
                stock_api.subscribe_world(message_reader, code, display_world_data)
        elif command.startswith('stop_world'):
            world_detail = command.split(',')
            if len(world_detail) != 2:
                print('stop_world,code')
            else:
                code = world_detail[1]
                stock_api.stop_subscribe_world(message_reader, code)

        elif command.startswith('investor'):
            inv_detail = command.split(',')
            if len(inv_detail) != 2:
                print('investor,code')
            else:
                code = inv_detail[1]
                print(stock_api.request_investor_data(message_reader, code, date(2020,1,31), date(2020,1,31)))
        elif command.startswith('kinvestorc'):
            invc_detail = command.split(',')
            if len(invc_detail) != 2:
                print('investorc,code')
            else:
                code = invc_detail[1]
                print(stock_api.request_investor_accumulate_data(message_reader, code, date(2020,2,7), date(2020,2,7)))
        elif command.startswith('alarm'):
            stock_api.subscribe_alarm(message_reader, display_alarm_data)
        elif command.startswith('stop_alarm'):
            stock_api.stop_subscribe_alarm(message_reader)
        elif command.startswith('code_to_name'):
            code_detail = command.split(',')
            if len(code_detail) != 2:
                print('code_to_name,code')
            else:
                code = code_detail[1]
                print(stock_api.request_code_to_name(message_reader, code))