Example #1
0
    def req_trade_detail(self, symbols: str, callback, error_handler=None):
        """
        Subscribe price depth event. If the price depth is updated,
        server will send the data to client and onReceive in callback will be called.

        :param symbols: The symbols, like "btcusdt". Use comma to separate multi symbols, like "btcusdt,ethusdt".
        :param callback: The implementation is required. onReceive will be called if receive server's update.
            example: def callback(trade_event: 'TradeEvent'):
                        pass
        :param error_handler: The error handler will be called if subscription failed or error
        happen between client and Huobi server
            example: def error_handler(exception: 'HuobiApiException')
                        pass
        :return:  No return
        """
        symbol_list = symbols.split(",")
        check_symbol_list(symbol_list)
        check_should_not_none(callback, "callback")

        def request_trade_detail_channel(symbol):
            channel = dict()
            channel["req"] = "market." + symbol + ".trade.detail"
            channel["id"] = str(get_current_timestamp())
            return json.dumps(channel)

        def subscription(connection):
            for symbol in symbol_list:
                connection.send(request_trade_detail_channel(symbol))
                time.sleep(0.01)

        self.market_service_socket.execute_subscribe_v1(
            subscription, callback, error_handler)
Example #2
0
    def req_candlestick(self,
                        symbols: str,
                        interval: CandlestickInterval,
                        callback,
                        from_ts_second=None,
                        end_ts_second=None,
                        error_handler=None):
        """
        Subscribe candlestick/kline event. If the candlestick/kline is updated,
        server will send the data to client and onReceive in callback will be called.

        :param symbols: The symbols, like "btcusdt". Use comma to separate multi symbols, like "btcusdt,ethusdt".
        :param interval: The candlestick/kline interval, MIN1, MIN5, DAY1 etc.
        :param callback: The implementation is required. onReceive will be called if receive server's update.
            example: def callback(candlestick_event: 'CandlestickEvent'):
                        pass
        :param from_ts_second : data from timestamp [it's second]
        :param end_ts_second : data util timestamp [it's second]
        :param error_handler: The error handler will be called if subscription failed or error happen
        between client and Huobi server
            example: def error_handler(exception: 'HuobiApiException')
                        pass
        :return: No return
        """

        symbol_list = symbols.split(",")
        check_symbol_list(symbol_list)
        check_should_not_none(interval, "interval")
        check_should_not_none(callback, "callback")

        def request_kline_channel(symbol,
                                  _interval,
                                  _from_ts_second=None,
                                  to_ts_second=None):
            channel = dict()
            channel["req"] = "market." + symbol + ".kline." + _interval
            channel["id"] = str(get_current_timestamp())
            if _from_ts_second:
                channel["from"] = int(_from_ts_second)
            if to_ts_second:
                channel["to"] = int(to_ts_second)
            return json.dumps(channel)

        def subscription(connection):
            for symbol in symbol_list:
                connection.send(
                    request_kline_channel(symbol, interval, from_ts_second,
                                          end_ts_second))
                time.sleep(0.01)

        self.market_service_socket.execute_subscribe_v1(
            subscription, callback, error_handler)
Example #3
0
    def req_mbp(self,
                symbols: str,
                levels: 'int',
                callback,
                auto_close=True,
                error_handler=None):
        """
        Subscribe mbp event. If the mbp is updated,
        server will send the data to client and onReceive in callback will be called.

        :param symbols: The symbols, like "btcusdt". Use comma to separate multi symbols, like "btcusdt,ethusdt".
        :param levels: level, 5,10,20,150. current only support 150
        :param callback: The implementation is required. onReceive will be called if receive server's update.
            example: def callback(price_depth_event: 'PriceDepthEvent'):
                        pass
        :param auto_close : close websocket connection after get data
        :param error_handler: The error handler will be called if subscription failed or error happen
        between client and Huobi server
            example: def error_handler(exception: 'HuobiApiException')
                        pass

        :return:  No return
        """
        check_should_not_none(symbols, "symbol")
        symbol_list = symbols.split(",")
        check_symbol_list(symbol_list)
        check_should_not_none(levels, "levels")
        check_should_not_none(callback, "callback")

        def request_mbp_channel(symbol, level):
            channel = dict()
            channel["req"] = "market.{symbol}.mbp.{level}".format(
                symbol=symbol, level=level)
            channel["id"] = str(get_current_timestamp())
            return json.dumps(channel)

        def subscription(connection):
            for symbol in symbol_list:
                connection.send(request_mbp_channel(symbol, levels))
                time.sleep(0.01)

        self.market_service_socket.execute_subscribe_mbp(
            subscription, callback, error_handler)
Example #4
0
    def req_price_depth(self,
                        symbols: str,
                        depth_step: str,
                        callback,
                        error_handler=None):
        """
        Subscribe price depth event. If the price depth is updated,
        server will send the data to client and onReceive in callback will be called.

        :param symbols: The symbols, like "btcusdt". Use comma to separate multi symbols, like "btcusdt,ethusdt".
        :param depth_step: The depth precision, string from step0 to step5.
        :param callback: The implementation is required. onReceive will be called if receive server's update.
            example: def callback(price_depth_event: 'PriceDepthEvent'):
                        pass
        :param error_handler: The error handler will be called if subscription failed or error happen
                              between client and Huobi server
            example: def error_handler(exception: 'HuobiApiException')
                        pass

        :return:  No return
        """
        symbol_list = symbols.split(",")
        check_symbol_list(symbol_list)
        step = MarketClient.get_valid_depth_step(value=depth_step,
                                                 defalut_value=DepthStep.STEP0)
        check_should_not_none(callback, "callback")

        def request_price_depth_channel(symbol, step_type="step0"):
            channel = dict()
            channel["req"] = "market." + symbol + ".depth." + step_type
            channel["id"] = str(get_current_timestamp())
            return json.dumps(channel)

        def subscription(connection):
            for symbol in symbol_list:
                connection.send(request_price_depth_channel(symbol, step))
                time.sleep(0.01)

        self.market_service_socket.execute_subscribe_v1(
            subscription, callback, error_handler)