Ejemplo n.º 1
0
    def sub(self, codes, subType, port):

        #行情上下文实例
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=port)
        quote_ctx.start()
        # 判断监听类型
        subType_index = SubAType.subTypes.index(subType)
        print('subType_index = %s' % subType_index)
        handler = None
        if subType_index is SubAType.subTypes.index(SubType.QUOTE):
            handler = StockQuoteTest()
        elif subType_index is SubAType.subTypes.index(SubType.ORDER_BOOK):
            handler = OrderBookTest()
        elif subType_index is SubAType.subTypes.index(SubType.TICKER):
            handler = TickerTest()
        elif subType_index is SubAType.subTypes.index(SubType.RT_DATA):
            handler = RTDataTest()
        elif subType_index is SubAType.subTypes.index(SubType.BROKER):
            handler = BrokerTest()
        else:
            handler = CurKlineTest()
        # 设置监听
        handler.set_loggerDir(self.dir)
        quote_ctx.set_handler(handler)
        # 触发订阅
        ret_code_sub, ret_data_sub = quote_ctx.subscribe(codes, subType)
        print(quote_ctx.query_subscription())
        #记录订阅结果
        self.logger.info('subType = ' + subType + ' ret_code_sub = ' +
                         str(ret_code_sub) + ' ret_data_sub = ' +
                         str(ret_data_sub))
        return ret_code_sub
Ejemplo n.º 2
0
    def test_maxReqPerSec_get_cur_kline(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
        code = 'HK.00700'
        ktype = SubType.K_DAY
        quote_ctx.subscribe(code_list=code, subtype_list=ktype)

        req_times_succ = 0
        req_times_total = 0
        t_start = time.time()
        while time.time() <= (t_start + 1):
            ret_code, ret_data = quote_ctx.get_cur_kline(code=code,
                                                         ktype=ktype,
                                                         num=10)
            req_times_total += 1
            if ret_code is RET_OK:
                req_times_succ += 1
            else:
                self.logger.info('get_cur_kline req_times_total = ' +
                                 str(req_times_total) + ' ret_code = ' +
                                 str(ret_code) + ' ret_data = ' + ret_data)
        quote_ctx.unsubscribe(code_list=code, subtype_list=ktype)
        quote_ctx.close()
        self.logger.info('get_cur_kline req_times_total = ' +
                         str(req_times_total) + ' req_times_succ = ' +
                         str(req_times_succ))
Ejemplo n.º 3
0
 def test3(self):
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
     ret_code, ret_data = quote_ctx.get_stock_basicinfo(
         Market.HK, SecurityType.STOCK)
     codes = ['HK.00700', 'HK.01752', 'SZ.000858', 'SH.600109', 'US.GOOG']
     subTypes = SubType.RT_DATA
     print(quote_ctx.unsubscribe(codes, subTypes))
Ejemplo n.º 4
0
    def test_maxReqPerSec_get_broker_queue(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
        code = 'HK.00700'
        ktype = SubType.BROKER
        quote_ctx.subscribe(code_list=code, subtype_list=ktype)

        req_times_succ = 0
        req_times_total = 0
        t_start = time.time()
        while time.time() <= (t_start + 1):
            ret_code, bid_frame_table, ask_frame_table = quote_ctx.get_broker_queue(
                code=code)
            req_times_total += 1
            if ret_code is RET_OK:
                req_times_succ += 1
            else:
                self.logger.info('get_broker_queue req_times_total = ' +
                                 str(req_times_total) + ' ret_code = ' +
                                 str(ret_code) + ' ret_data = ' +
                                 bid_frame_table + ask_frame_table)
        quote_ctx.unsubscribe(code_list=code, subtype_list=ktype)
        quote_ctx.close()
        self.logger.info('get_broker_queue req_times_total = ' +
                         str(req_times_total) + ' req_times_succ = ' +
                         str(req_times_succ))
Ejemplo n.º 5
0
 def sub(self):
     #日志
     logger_dir = time.strftime('%Y-%m-%d', time.localtime(time.time()))
     self.logger = Logs().getNewLogger(name=self.__class__.__name__,
                                       dir=logger_dir)
     #行情上下文实例
     # quote_ctx = futuquant.OpenQuoteContext(host= host, port= port_kweek )
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
     quote_ctx.start()
     # 设置监听
     handler = CurKlineTest()  #周K
     handler.set_loggerDir(logger_dir)
     quote_ctx.set_handler(handler)
     #订阅
     sub_weight = 2  #订阅权重
     sub_limit = 500
     # codes_len = sub_limit//sub_weight
     codes_len = 100
     codes = get_codes_cvs()[0:codes_len]
     subtype = SubType.K_WEEK  #订阅类型
     ret_code_sub, ret_data_sub = quote_ctx.subscribe(codes, subtype)
     # 记录订阅结果
     self.logger.info('subType = ' + subtype + ' ret_code_sub = ' +
                      str(ret_code_sub) + ' ret_data_sub = ' +
                      str(ret_data_sub))
Ejemplo n.º 6
0
    def test_maxReqPerSec_unsubscribe(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
        #获取测试股票
        stock_types = [
            SecurityType.STOCK, SecurityType.WARRANT, SecurityType.ETF,
            SecurityType.BOND, SecurityType.IDX
        ]
        codes = []
        for stock_type in stock_types:
            ret_code, ret_data = quote_ctx.get_stock_basicinfo(
                market=Market.HK, stock_type=stock_type)
            codes += ret_data['code'].tolist()
        #订阅,创造反订阅的条件
        subtype = SubType.K_1M
        quote_ctx.subscribe(code_list=codes, subtype_list=subtype)
        # time.sleep(61)

        req_times_succ = 0
        req_times_total = 0
        t_start = time.time()
        while time.time() <= (t_start + 1):
            ret_code, ret_data = quote_ctx.unsubscribe(
                code_list=codes[req_times_total], subtype_list=subtype)
            req_times_total += 1
            if ret_code is RET_OK:
                req_times_succ += 1
            else:
                self.logger.info('unsubscribe req_times_total = ' +
                                 str(req_times_total) + ' ret_code = ' +
                                 str(ret_code) + ' ret_data = ' + ret_data)
        quote_ctx.close()
        self.logger.info('unsubscribe req_times_total = ' +
                         str(req_times_total) + ' req_times_succ = ' +
                         str(req_times_succ))
Ejemplo n.º 7
0
def main():
    quote_ctx = ft.OpenQuoteContext(host=futud_host, port=futud_port)
    # ret, data = quote_ctx.get_market_snapshot('HK.00700')
    # data.to_csv("tmp_0700.csv", index=False)
    # print(data.iloc[:, 0:4])

    ret_subscribe = quote_ctx.subscribe(stock_code_list, [
        SubType.QUOTE, SubType.TICKER, SubType.BROKER, SubType.ORDER_BOOK,
        SubType.RT_DATA
    ])
    print(ret_subscribe)
    quote_ctx.start()

    stock_quote_handler = StockQuoteHandler(quote_ctx)
    quote_ctx.set_handler(stock_quote_handler)
    # rt_data_handler = RTDataHandler()
    # quote_ctx.set_handler(rt_data_handler)

    print("...before sleeping")
    time.sleep(60 * 60)
    print("...end sleeping")

    quote_ctx.close()

    return
Ejemplo n.º 8
0
 def test2(self):
     # 经纪队列
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
     code = 'HK.00700'
     quote_ctx.set_handler(BrokerTest())
     quote_ctx.subscribe(code, SubType.BROKER)
     print(quote_ctx.get_broker_queue(code))
Ejemplo n.º 9
0
 def __init__(self):
     self.last_dbcheck_time = 0 #最后一次查看db的时间
     self.todo_orders = None  #所有待触发的订单
     self.order_db = db_util()
     self.order_db.init_db()
     self.auto_trade = auto_trade("config.ini")
     self.quote_context = ft.OpenQuoteContext(host='127.0.0.1', port=11111)
Ejemplo n.º 10
0
    def sub_K_100(self):
        #挂机测稳定性:K线种类数(8)*股票个数<=100
        kTypes = [
            SubType.K_1M, SubType.K_5M, SubType.K_15M, SubType.K_30M,
            SubType.K_60M, SubType.K_DAY, SubType.K_WEEK, SubType.K_MON
        ]
        limit = 100
        code_list = get_codes_cvs()

        # 上下文实例
        quote_ctx = futuquant.OpenQuoteContext(host='172.18.10.58', port=11111)
        quote_ctx.start()

        #设置监听
        handler = CurKlineTest()
        handler.set_loggerDir('sub_K_100')
        quote_ctx.set_handler(handler)
        #订阅
        sk = 0
        for code in code_list:
            for kType in kTypes:
                ret_code, ret_data = quote_ctx.subscribe(code, kType)
                if ret_code is RET_OK:
                    sk += 1
                    if sk == limit:
                        break
            if sk == limit:
                break
Ejemplo n.º 11
0
    def context_setting(self):
        """
        API trading and quote context setting
        :returns: trade context, quote context
        """
        if self.unlock_password == "":
            raise Exception("请先配置交易解锁密码! password: {}".format(
                self.unlock_password))

        quote_ctx = ft.OpenQuoteContext(host=self.api_svr_ip,
                                        port=self.api_svr_port)

        if 'HK.' in self.stock:
            trade_ctx = ft.OpenHKTradeContext(host=self.api_svr_ip,
                                              port=self.api_svr_port)

            if self.trade_env == ft.TrdEnv.REAL:
                ret_code, ret_data = trade_ctx.unlock_trade(
                    self.unlock_password)
                if ret_code == ft.RET_OK:
                    print('解锁交易成功!')
                else:
                    raise Exception("请求交易解锁失败: {}".format(ret_data))
            else:
                print('解锁交易成功!')
        elif 'US.' in self.stock:
            if self.trade_env != 0:
                raise Exception("美股交易接口不支持仿真环境 trade_env: {}".format(
                    self.trade_env))
            trade_ctx = ft.OpenUSTradeContext(host=self.api_svr_ip,
                                              port=self.api_svr_port)
        else:
            raise Exception("stock输入错误 stock: {}".format(self.stock))

        return quote_ctx, trade_ctx
Ejemplo n.º 12
0
    def test1(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
        ret_code, state_dict = quote_ctx.get_global_state()

        print(ret_code)
        print(state_dict)
        quote_ctx.close()
Ejemplo n.º 13
0
 def __init__(self):
     # 日志
     self.logger = Logs().getNewLogger(name=self.__class__.__name__)
     # 行情上下文实例
     self.quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1',
                                                 port=11111)
     self.quote_ctx.start()
Ejemplo n.º 14
0
def enum_all_index(ip, port):
    quote_ctx = ft.OpenQuoteContext(ip, port)
    ret, data_frame = quote_ctx.get_stock_basicinfo(
        market=ft.Market.SH, stock_type=ft.SecurityType.IDX)
    data_frame.to_csv("index_sh.txt",
                      index=True,
                      sep=' ',
                      columns=['code', 'name'])
    print('market SH index data saved!')
    ret, data_frame = quote_ctx.get_stock_basicinfo(
        market=ft.Market.SZ, stock_type=ft.SecurityType.IDX)
    data_frame.to_csv("index_sz.txt",
                      index=True,
                      sep=' ',
                      columns=['code', 'name'])
    print('market SZ index data saved!')
    ret, data_frame = quote_ctx.get_stock_basicinfo(
        market=ft.Market.HK, stock_type=ft.SecurityType.IDX)
    data_frame.to_csv("index_hk.txt",
                      index=True,
                      sep=' ',
                      columns=['code', 'name'])
    print('market HK index data saved!')
    ret, data_frame = quote_ctx.get_stock_basicinfo(
        market=ft.Market.US, stock_type=ft.SecurityType.IDX)
    data_frame.to_csv("index_us.txt",
                      index=True,
                      sep=' ',
                      columns=['code', 'name'])
    print('market US index data saved!')
    quote_ctx.close()
Ejemplo n.º 15
0
    def __init__(self):
        # 加密通道
        # SysConfig.enable_proto_encrypt(True)
        # SysConfig.enable_proto_encrypt(True)

        self.quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1',
                                                    port=11118)
        self.quote_ctx.start()
Ejemplo n.º 16
0
 def test2(self):
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
     quote_ctx.start()
     # 设置异步数据监听
     handler = CurKlineTest()
     quote_ctx.set_handler(handler)
     # codes = get_codes_cvs()[:2]
     print(quote_ctx.subscribe('HK.00857', SubType.K_1M))
Ejemplo n.º 17
0
 def test2(self):
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
     code = 'HK.00772'
     #订阅
     quote_ctx.subscribe(code, SubType.RT_DATA)
     #调用待测接口
     ret_code, ret_data = quote_ctx.get_rt_data(code)
     print(ret_code)
     print(ret_data)
Ejemplo n.º 18
0
    def __init__(self):
        # 加密通道
        # SysConfig.enable_proto_encrypt(True)
        # SysConfig.enable_proto_encrypt(True)


        # port=11111挂机CentOs7  1010503
        self.quote_ctx=futuquant.OpenQuoteContext(host='172.18.10.58',port=11111)
        self.quote_ctx.start()
Ejemplo n.º 19
0
    def test1(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
        code_list = ['HK.00700']
        ret_code, ret_data = quote_ctx.get_autype_list(code_list)
        quote_ctx.close()

        print(ret_code)
        # print(ret_data)
        for data in ret_data.iterrows():
            print(data)
Ejemplo n.º 20
0
 def test1(self):
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
     quote_ctx.start()
     #设置监听
     handler = BrokerTest()
     quote_ctx.set_handler(handler)
     codes = get_codes_cvs()[:50]
     #订阅
     for code in codes:
         quote_ctx.subscribe(code, SubType.BROKER)
Ejemplo n.º 21
0
 def test2(self):
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11114)
     code = 'HK.00700'
     ktype = SubType.BROKER
     print(quote_ctx.subscribe(code, ktype))
     # print(quote_ctx.query_subscription())
     # time.sleep(60)
     print(quote_ctx.unsubscribe(code, ktype))
     # print(quote_ctx.query_subscription())
     quote_ctx.close()
Ejemplo n.º 22
0
    def test1(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
        plate_code = 'HK.BK1102'
        ret_code, ret_data = quote_ctx.get_plate_stock(plate_code)
        quote_ctx.close()

        print(ret_code)
        # print(ret_data)
        for data in ret_data.iterrows():
            print(data)
Ejemplo n.º 23
0
    def test1(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)

        ret_code, ret_data = quote_ctx.get_stock_basicinfo(market='HK',
                                                           stock_type='STOCK')
        # code_list = ret_data['code'].tolist()[0:20]
        code_list = ['HK.24505', 'HK.00700']
        ret_code, ret_data = quote_ctx.get_market_snapshot('HK.00700')
        quote_ctx.close()
        print(ret_code)
        print(ret_data)
Ejemplo n.º 24
0
 def test1(self):
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
     quote_ctx.start()
     # 设置异步数据监听
     handler = TickerTest()
     quote_ctx.set_handler(handler)
     codes = ['HK.00700', 'HK.00939', 'HK.00941', 'US.AAPL', 'US.GOOG']
     #订阅股票
     print(quote_ctx.subscribe(codes, SubType.TICKER))
     # 调用待测接口
     for code in codes:
         print(quote_ctx.get_rt_ticker(code, 1000))
Ejemplo n.º 25
0
    def test5(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
        ret_code, ret_data = quote_ctx.get_stock_basicinfo(
            Market.HK, SecurityType.STOCK)
        codes = ret_data['code'].tolist()[:50]
        subTypes = [
            SubType.QUOTE, SubType.ORDER_BOOK, SubType.BROKER, SubType.TICKER,
            SubType.RT_DATA, SubType.K_1M, SubType.K_5M, SubType.K_15M,
            SubType.K_30M, SubType.K_60M, SubType.K_DAY, SubType.K_WEEK,
            SubType.K_MON
        ]

        quote_ctx.subscribe(codes, subTypes)
Ejemplo n.º 26
0
 def test1(self):
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
     quote_ctx.start()
     # 设置异步数据监听
     handler = TickerTest()
     quote_ctx.set_handler(handler)
     codes = ['HK.00700']
     for code in codes:
         #订阅股票
         quote_ctx.subscribe(code, SubType.TICKER)
         # 调用待测接口
         ret_code, ret_data = quote_ctx.get_rt_ticker(code, 1000)
         print(ret_code)
         print(ret_data)
Ejemplo n.º 27
0
    def test1(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
        quote_ctx.start()
        #设置监听
        handler = OrderBookTest()
        quote_ctx.set_handler(handler)

        #订阅股票
        code = 'HK.999010'
        quote_ctx.subscribe(code, SubType.ORDER_BOOK)
        #调用待测接口
        ret_code, ret_data = quote_ctx.get_order_book(code)
        print(ret_code)
        print(ret_data)
Ejemplo n.º 28
0
    def test1(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1',port=11111)
        code = 'HK.00700'
        start = '2018-07-01'
        end = '2018-05-17'
        ktype = KLType.K_WEEK
        autype = AuType.QFQ
        fields = KL_FIELD.ALL_REAL

        ret_code , ret_data = quote_ctx.get_history_kline(code, start, end, ktype, autype, fields)

        print(ret_code)
        print(ret_data)
        quote_ctx.close()
Ejemplo n.º 29
0
 def test1(self):
     quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
     quote_ctx.start()
     # 设置异步数据监听
     handler = TickerTest()
     quote_ctx.set_handler(handler)
     #所有正股
     codes_hk = quote_ctx.get_stock_basicinfo(
         market=Market.HK, stock_type=SecurityType.STOCK).tolist()[:100]
     codes_us = quote_ctx.get_stock_basicinfo(
         market=Market.US, stock_type=SecurityType.STOCK)[:100]
     #订阅股票
     codes = codes_hk + codes_us
     print('sub', quote_ctx.subscribe(codes, SubType.TICKER))
Ejemplo n.º 30
0
    def test1(self):
        quote_ctx = futuquant.OpenQuoteContext(host='127.0.0.1', port=11111)
        quote_ctx.start()
        #设置监听
        handler = RTDataTest()
        quote_ctx.set_handler(handler)
        # code = 'HK.00772'
        codes = ['HK.00700', 'HK.01752', 'SZ.000858', 'SH.600109', 'US.GOOG']

        for code in codes:
            #订阅
            quote_ctx.subscribe(code, SubType.RT_DATA)
            #调用待测接口
            ret_code, ret_data = quote_ctx.get_rt_data(code)
            print(ret_code)
            print(ret_data)