Beispiel #1
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 = OpenQuoteContext(host=self.api_svr_ip, port=self.api_svr_port)

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

        return quote_ctx, trade_ctx
Beispiel #2
0
 def start(self, host=ct.FUTU_HOST, port=ct.FUTU_PORT):
     with self.lock:
         if self.quote_ctx is None:
             self.quote_ctx = OpenQuoteContext(host, port)
         else:
             self.quote_ctx.start()
         self.sub_dict = self.get_subscribed_dict()
         logger.debug("self.sub_dict:%s" % self.sub_dict)
         self._status = True
Beispiel #3
0
    def connectQuote(self):
        """连接行情功能"""
        self.quoteCtx = OpenQuoteContext(self.host, self.port)

        # 继承实现处理器类
        class QuoteHandler(StockQuoteHandlerBase):
            """报价处理器"""
            gateway = self  # 缓存Gateway对象

            def on_recv_rsp(self, rsp_str):
                ret_code, content = super(QuoteHandler,
                                          self).on_recv_rsp(rsp_str)
                if ret_code != RET_OK:
                    return RET_ERROR, content
                self.gateway.processQuote(content)
                return RET_OK, content

        class OrderBookHandler(OrderBookHandlerBase):
            """订单簿处理器"""
            gateway = self

            def on_recv_rsp(self, rsp_str):
                ret_code, content = super(OrderBookHandler,
                                          self).on_recv_rsp(rsp_str)
                if ret_code != RET_OK:
                    return RET_ERROR, content
                self.gateway.processOrderBook(content)
                return RET_OK, content

        # 设置回调处理对象
        self.quoteCtx.set_handler(QuoteHandler())
        self.quoteCtx.set_handler(OrderBookHandler())

        # 启动行情
        self.quoteCtx.start()

        self.writeLog(u'行情接口连接成功')
Beispiel #4
0
 def _init_quote_context(self):
     self._quote_context = OpenQuoteContext(
         str(self._mod_config.api_svr.ip),
         int(self._mod_config.api_svr.port))
     return self._quote_context
Beispiel #5
0
                                sell_4_order_num=a['Ask'][3][2],
                                buy_5_price=a['Bid'][4][0], buy_5_volume=a['Bid'][4][1],
                                buy_5_order_num=a['Bid'][4][2],
                                sell_5_price=a['Ask'][4][0], sell_5_volume=a['Ask'][4][1],
                                sell_5_order_num=a['Ask'][4][2],
                                buy_6_price=a['Bid'][5][0], buy_6_volume=a['Bid'][5][1],
                                buy_6_order_num=a['Bid'][5][2],
                                sell_6_price=a['Ask'][5][0], sell_6_volume=a['Ask'][5][1],
                                sell_6_order_num=a['Ask'][5][2],
                                buy_7_price=a['Bid'][6][0], buy_7_volume=a['Bid'][6][1],
                                buy_7_order_num=a['Bid'][6][2],
                                sell_7_price=a['Ask'][6][0], sell_7_volume=a['Ask'][6][1],
                                sell_7_order_num=a['Ask'][6][2],
                                buy_8_price=a['Bid'][7][0], buy_8_volume=a['Bid'][7][1],
                                buy_8_order_num=a['Bid'][7][2],
                                sell_8_price=a['Ask'][7][0], sell_8_volume=a['Ask'][7][1],
                                sell_8_order_num=a['Ask'][7][2],
                                buy_9_price=a['Bid'][8][0], buy_9_volume=a['Bid'][8][1],
                                buy_9_order_num=a['Bid'][8][2],
                                sell_9_price=a['Ask'][8][0], sell_9_volume=a['Ask'][8][1],
                                sell_9_order_num=a['Ask'][8][2],
                                buy_10_price=a['Bid'][9][0], buy_10_volume=a['Bid'][9][1],
                                buy_10_order_num=a['Bid'][9][2],
                                sell_10_price=a['Ask'][9][0], sell_10_volume=a['Ask'][9][1],
                                sell_10_order_num=a['Ask'][9][2])

    return quote_dict
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
x=get_quote(code_list=['HK.00700'],quote_ctx=quote_ctx)
print(x)
quote_ctx.close()