示例#1
0
    def get_deposit_history(self, currency, from_id, size):
        check_symbol(currency)
        check_should_not_none(from_id, "from_id")
        check_should_not_none(size, "size")

        builder = UrlParamsBuilder()
        builder.put_url("currency", currency)
        builder.put_url("type", "deposit")
        builder.put_url("from", from_id)
        builder.put_url("size", size)
        request = self.__create_request_by_get_with_signature(
            "/v1/query/deposit-withdraw", builder)

        def parse(json_wrapper):
            deposits = list()
            data_array = json_wrapper.get_array("data")
            for item in data_array.get_items():
                deposit = Deposit()
                deposit.id = item.get_int("id")
                deposit.currency = item.get_string("currency")
                deposit.tx_hash = item.get_string("tx-hash")
                deposit.amount = item.get_float("amount")
                deposit.address = item.get_string("address")
                deposit.address_tag = item.get_string("address-tag")
                deposit.fee = item.get_float("fee")
                deposit.withdraw_state = item.get_string("state")
                deposit.created_timestamp = convert_cst_in_millisecond_to_utc(
                    item.get_int("created-at"))
                deposit.updated_timestamp = convert_cst_in_millisecond_to_utc(
                    item.get_int("updated-at"))
                deposits.append(deposit)
            return deposits

        request.json_parser = parse
        return request
    def get_margin_balance_detail(self, symbol):
        check_symbol(symbol)
        builder = UrlParamsBuilder()
        builder.put_url("symbol", symbol)
        request = self.__create_request_by_get_with_signature("/v1/margin/accounts/balance", builder)

        def parse(json_wrapper):
            margin_balance_detail_list = list()
            data_array = json_wrapper.get_array("data")
            for item_in_data in data_array.get_items():
                margin_balance_detail = MarginBalanceDetail()
                margin_balance_detail.id = item_in_data.get_int("id")
                margin_balance_detail.type = item_in_data.get_string("type")
                margin_balance_detail.symbol = item_in_data.get_string("symbol")
                margin_balance_detail.state = item_in_data.get_string("state")
                margin_balance_detail.fl_price = item_in_data.get_float("fl-price")
                margin_balance_detail.fl_type = item_in_data.get_string("fl-type")
                margin_balance_detail.risk_rate = item_in_data.get_float("risk-rate")
                balance_list = list()
                list_array = item_in_data.get_array("list")
                for item_in_list in list_array.get_items():
                    balance = Balance()
                    balance.currency = item_in_list.get_string("currency")
                    balance.balance_type = item_in_list.get_string("type")
                    balance.balance = item_in_list.get_float("balance")
                    balance_list.append(balance)
                margin_balance_detail.sub_account_balance_list = balance_list
                margin_balance_detail_list.append(margin_balance_detail)
            return margin_balance_detail_list

        request.json_parser = parse
        return request
    def get_market_trade(self, symbol):
        check_symbol(symbol)
        builder = UrlParamsBuilder()
        builder.put_url("symbol", symbol)
        request = self.__create_request_by_get("/market/trade", builder)

        def parse(json_wrapper):
            tick_obj = json_wrapper.get_object("tick")
            data_array = tick_obj.get_array("data")
            trade_list = list()

            for item in data_array.get_items():
                local_trade = Trade()
                local_trade.price = item.get_float("price")
                local_trade.amount = item.get_float("amount")
                local_trade.trade_id = item.get_int("id")
                local_trade.timestamp = convert_cst_in_millisecond_to_utc(
                    item.get_int("ts"))
                local_trade.direction = item.get_string("direction")
                trade_list.append(local_trade)

            return trade_list

        request.json_parser = parse
        return request
    def get_etf_swap_config(self, etf_symbol):
        check_symbol(etf_symbol)
        builder = UrlParamsBuilder()
        builder.put_url("etf_name", etf_symbol)
        request = self.__create_request_by_get("/etf/swap/config", builder)

        def parse(json_wrapper):
            data = json_wrapper.get_object("data")
            etf_swap_config = EtfSwapConfig()
            etf_swap_config.purchase_max_amount = data.get_int(
                "purchase_max_amount")
            etf_swap_config.purchase_min_amount = data.get_int(
                "purchase_min_amount")
            etf_swap_config.redemption_max_amount = data.get_int(
                "redemption_max_amount")
            etf_swap_config.redemption_min_amount = data.get_int(
                "redemption_min_amount")
            etf_swap_config.purchase_fee_rate = data.get_float(
                "purchase_fee_rate")
            etf_swap_config.redemption_fee_rate = data.get_float(
                "redemption_fee_rate")
            etf_swap_config.status = data.get_string("etf_status")
            unit_price_data_array = data.get_array("unit_price")
            unit_price_list = list()
            for item in unit_price_data_array.get_items():
                unit_price = UnitPrice()
                unit_price.currency = item.get_string("currency")
                unit_price.amount = item.get_float("amount")
                unit_price_list.append(unit_price)
            etf_swap_config.unit_price_list = unit_price_list
            return etf_swap_config

        request.json_parser = parse
        return request
示例#5
0
 def test_request(self):
     builder = UrlParamsBuilder()
     huobi.impl.utils.apisignature.utc_now = mock.Mock(return_value="123")
     create_signature("123", "456", "GET", "http://host/url", builder)
     self.assertEqual(
         "?AccessKeyId=123&SignatureVersion=2&SignatureMethod=HmacSHA256&Timestamp=123&Signature=Hhiaq8xYQPiBZOyWV37MdQutLo4f0ZOHiJtG3p%2BnILc%3D",
         builder.build_url())
     return
 def get_order_by_client_order_id(self, client_order_id):
     check_should_not_none(client_order_id, "clientOrderId")
     path = "/v1/order/orders/getClientOrder"
     builder = UrlParamsBuilder()
     builder.put_url("clientOrderId", client_order_id)
     request = self.__create_request_by_get_with_signature(path, builder)
     request.json_parser = self.get_order_json_parse
     return request
    def cancel_client_order(self, client_order_id):
        check_should_not_none(client_order_id, "client-order-id")
        path = "/v1/order/orders/submitCancelClientOrder"
        builder = UrlParamsBuilder()
        builder.put_post("client-order-id", client_order_id)
        request = self.__create_request_by_post_with_signature(path, builder)

        def parse(json_wrapper):
            return

        request.json_parser = parse
        return request
    def repay_loan(self, load_id, amount):
        check_should_not_none(load_id, "load_id")
        check_should_not_none(amount, "amount")
        builder = UrlParamsBuilder()
        builder.put_post("amount", amount)
        path = "/v1/margin/orders/{}/repay"
        path = path.format(load_id)
        request = self.__create_request_by_post_with_signature(path, builder)

        def parse(json_wrapper):
            return json_wrapper.get_int("data")

        request.json_parser = parse
        return request
    def get_etf_candlestick(self, symbol, interval, size=None):
        check_symbol(symbol)
        check_range(size, 1, 2000, "size")
        check_should_not_none(interval, "interval")
        builder = UrlParamsBuilder()
        builder.put_url("symbol", symbol)
        builder.put_url("period", interval)
        builder.put_url("limit", size)
        request = self.__create_request_by_get(
            "/quotation/market/history/kline", builder)

        def parse(json_wrapper):
            candlestick_list = list()
            data_array = json_wrapper.get_array("data")
            for item in data_array.get_items():
                candlestick = Candlestick()
                candlestick.open = item.get_float("open")
                candlestick.close = item.get_float("close")
                candlestick.low = item.get_float("low")
                candlestick.high = item.get_float("high")
                candlestick.amount = item.get_float("amount")
                candlestick.count = 0
                candlestick.volume = item.get_float("vol")
                candlestick_list.append(candlestick)
            return candlestick_list

        request.json_parser = parse
        return request
示例#10
0
    def on_open(self, ws):
        #print("### open ###")
        self.logger.info("[Sub][" + str(self.id) + "] Connected to server")
        self.ws = ws
        self.last_receive_time = get_current_timestamp()
        self.state = ConnectionState.CONNECTED
        self.__watch_dog.on_connection_created(self)
        if self.request.is_trading:
            try:
                if self.request.api_version == ApiVersion.VERSION_V1:
                    builder = UrlParamsBuilder()
                    create_signature(self.__api_key, self.__secret_key, "GET",
                                     self.url, builder)
                    builder.put_url("op", "auth")
                    self.send(builder.build_url_to_json())
                elif self.request.api_version == ApiVersion.VERSION_V2:
                    builder = UrlParamsBuilder()
                    create_signature_v2(self.__api_key, self.__secret_key,
                                        "GET", self.url, builder)
                    self.send(builder.build_url_to_json())
                else:
                    self.on_error(
                        "api version for create the signature fill failed")

            except Exception as e:
                self.on_error("Unexpected error when create the signature: " +
                              str(e))
        else:
            if self.request.subscription_handler is not None:
                self.request.subscription_handler(self)
        return
示例#11
0
    def cancel_orders(self, symbol, order_id_list):
        check_symbol(symbol)
        check_should_not_none(order_id_list, "order_id_list")
        check_list(order_id_list, 1, 50, "order_id_list")
        string_list = list()
        for order_id in order_id_list:
            string_list.append(str(order_id))
        builder = UrlParamsBuilder()
        builder.put_post("order-ids", string_list)
        request = self.__create_request_by_post_with_signature("/v1/order/orders/batchcancel", builder)

        def parse(json_wrapper):
            return

        request.json_parser = parse
        return request
    def get_match_results_by_order_id(self, order_id):
        check_should_not_none(order_id, "order_id")
        path = "/v1/order/orders/{}/matchresults"
        path = path.format(order_id)
        request = self.__create_request_by_get_with_signature(
            path, UrlParamsBuilder())

        def parse(json_wrapper):
            match_result_list = list()
            data_array = json_wrapper.get_array("data")
            for item in data_array.get_items():
                match_result = MatchResult()
                match_result.id = item.get_int("id")
                match_result.created_timestamp = convert_cst_in_millisecond_to_utc(
                    item.get_int("created-at"))
                match_result.filled_amount = item.get_float("filled-amount")
                match_result.filled_fees = item.get_float("filled-fees")
                match_result.match_id = item.get_int("match-id")
                match_result.order_id = item.get_int("order-id")
                match_result.price = item.get_float("price")
                match_result.source = item.get_string("source")
                match_result.symbol = item.get_string("symbol")
                match_result.order_type = item.get_string("type")
                match_result.role = item.get_string("role")
                match_result.filled_points = item.get_string("filled-points")
                match_result.fee_deduct_currency = item.get_string(
                    "fee-deduct-currency")
                match_result_list.append(match_result)
            return match_result_list

        request.json_parser = parse
        return request
    def get_specify_account_balance(self, sub_id):
        path = "/v1/account/accounts/{}"
        path = path.format(sub_id)
        request = self.__create_request_by_get_with_signature(
            path, UrlParamsBuilder())

        def parse(json_wrapper):
            complete_sub_account_list = list()
            data_array = json_wrapper.get_array("data")
            for item in data_array.get_items():
                complete_sub_account = CompleteSubAccountInfo()
                complete_sub_account.id = item.get_int("id")
                complete_sub_account.account_type = item.get_string("type")
                balances = list()
                data_array_in = item.get_array("list")
                for item_in in data_array_in.get_items():
                    balance = Balance()
                    balance.currency = item_in.get_string("currency")
                    balance.type = item_in.get_string("type")
                    balance.balance = item_in.get_float("balance")
                    balances.append(balance)
                complete_sub_account.balances = balances
                complete_sub_account_list.append(complete_sub_account)
            return complete_sub_account_list

        request.json_parser = parse
        return request
示例#14
0
 def __create_request_by_get(self, url, builder=UrlParamsBuilder()):
     request = RestApiRequest()
     request.method = "GET"
     request.host = self.__server_url
     request.header.update({'Content-Type': 'application/json'})
     request.url = url + builder.build_url()
     return request
示例#15
0
    def get_symbols_by_usdt_btc_eth(self):
        request = self.__create_request_by_get("/v1/common/symbols",
                                               UrlParamsBuilder())

        def parse(json_wrapper):
            symbol_map = dict()
            symbol_map['usdt'] = list()
            symbol_map['eth'] = list()
            symbol_map['btc'] = list()
            data_array = json_wrapper.get_array("data")
            for item in data_array.get_items():
                local_symbol = Symbol()
                local_symbol.base_currency = item.get_string("base-currency")
                local_symbol.quote_currency = item.get_string("quote-currency")
                local_symbol.price_precision = item.get_int("price-precision")
                local_symbol.amount_precision = item.get_int(
                    "amount-precision")
                local_symbol.symbol_partition = item.get_string(
                    "symbol-partition")
                local_symbol.symbol = item.get_string("symbol")
                if local_symbol.quote_currency == 'usdt':
                    symbol_map['usdt'].append(local_symbol)
                elif local_symbol.quote_currency == 'eth':
                    symbol_map['eth'].append(local_symbol)
                elif local_symbol.quote_currency == 'btc':
                    symbol_map['btc'].append(local_symbol)
            return symbol_map

        request.json_parser = parse
        return request
    def get_symbols(self):
        request = self.__create_request_by_get("/v1/common/symbols",
                                               UrlParamsBuilder())

        def parse(json_wrapper):
            symbols = list()
            data_array = json_wrapper.get_array("data")
            for item in data_array.get_items():
                local_symbol = Symbol()
                local_symbol.base_currency = item.get_string("base-currency")
                local_symbol.quote_currency = item.get_string("quote-currency")
                local_symbol.price_precision = item.get_int("price-precision")
                local_symbol.amount_precision = item.get_int(
                    "amount-precision")
                local_symbol.symbol_partition = item.get_string(
                    "symbol-partition")
                local_symbol.symbol = item.get_string("symbol")
                local_symbol.state = item.get_string("state")
                local_symbol.value_precision = item.get_string(
                    "value-precision")
                local_symbol.min_order_amt = item.get_string("min-order-amt")
                local_symbol.max_order_amt = item.get_string("max-order-amt")
                local_symbol.min_order_value = item.get_string(
                    "min-order-value")
                local_symbol.leverage_ratio = item.get_string_or_default(
                    "leverage-ratio", 0)
                symbols.append(local_symbol)
            return symbols

        request.json_parser = parse
        return request
示例#17
0
    def get_order(self, symbol, order_id):
        check_symbol(symbol)
        check_should_not_none(order_id, "order_id")
        path = "/v1/order/orders/()"
        path = path.format(order_id)
        request = self.__create_request_by_get_with_signature(
            path, UrlParamsBuilder())

        def parse(json_wrapper):
            data = json_wrapper.get_object("data")
            order = Order()
            order.order_id = data.get_int("id")
            order.symbol = data.get_string("symbol")
            order.price = data.get_float("price")
            order.amount = data.get_float("amount")
            order.account_type = account_info_map.get_account_by_id(
                self.__api_key, data.get_int("account-id")).account_type
            order.created_timestamp = convert_cst_in_millisecond_to_utc(
                data.get_int("created-at"))
            order.canceled_timestamp = convert_cst_in_millisecond_to_utc(
                data.get_int("canceled-at"))
            order.finished_timestamp = convert_cst_in_millisecond_to_utc(
                data.get_int("finished-at"))
            order.order_type = data.get_string("type")
            order.filled_amount = data.get_float("field-amount")
            order.filled_cash_amount = data.get_float("field-cash-amount")
            order.filled_fees = data.get_float("field-fees")
            order.source = data.get_string("source")
            order.state = data.get_string("state")
            return order

        request.json_parser = parse
        return request
示例#18
0
    def get_exchange_timestamp(self):
        request = self.__create_request_by_get("/v1/common/timestamp", UrlParamsBuilder())

        def parse(json_wrapper):
            return convert_cst_in_millisecond_to_utc(json_wrapper.get_int("data"))

        request.json_parser = parse
        return request
示例#19
0
    def transfer_between_parent_and_sub(self, sub_uid, currency, amount, transfer_type):
        check_currency(currency)
        check_should_not_none(sub_uid, "sub_uid")
        check_should_not_none(amount, "amount")
        check_should_not_none(transfer_type, "transfer_type")
        builder = UrlParamsBuilder()
        builder.put_post("sub-uid", sub_uid)
        builder.put_post("amount", amount)
        builder.put_post("currency", currency)
        builder.put_post("type", transfer_type)
        request = self.__create_request_by_post_with_signature("/v1/subuser/transfer", builder)

        def parse(json_wrapper):
            return json_wrapper.get_int("data")

        request.json_parser = parse
        return request
示例#20
0
    def get_currencies(self):
        request = self.__create_request_by_get("/v1/common/currencys", UrlParamsBuilder())

        def parse(json_wrapper):
            return json_wrapper.get_array("data").get_items_as_string()

        request.json_parser = parse
        return request
 def get_order(self, symbol, order_id):
     check_symbol(symbol)
     check_should_not_none(order_id, "order_id")
     path = "/v1/order/orders/{}"
     path = path.format(order_id)
     request = self.__create_request_by_get_with_signature(
         path, UrlParamsBuilder())
     request.json_parser = self.get_order_json_parse
     return request
示例#22
0
    def get_best_quote(self, symbol):
        check_symbol(symbol)
        builder = UrlParamsBuilder()
        builder.put_url("symbol", symbol)
        request = self.__create_request_by_get("/market/detail/merged", builder)

        def parse(json_wrapper):
            best_quote = BestQuote()
            best_quote.timestamp = convert_cst_in_millisecond_to_utc(json_wrapper.get_int("ts"))
            tick = json_wrapper.get_object("tick")
            ask_array = tick.get_array("ask")
            best_quote.ask_price = ask_array.get_float_at(0)
            best_quote.ask_amount = ask_array.get_float_at(1)
            bid_array = tick.get_array("bid")
            best_quote.bid_price = bid_array.get_float_at(0)
            best_quote.bid_amount = bid_array.get_float_at(1)
            return best_quote

        request.json_parser = parse
        return request
    def get_fee_rate(self, symbols):
        check_symbol(symbols)
        builder = UrlParamsBuilder()
        builder.put_url("symbols", symbols)
        request = self.__create_request_by_get_with_signature(
            "/v1/fee/fee-rate/get", builder)

        def parse(json_wrapper):
            fee_list = list()
            data_array = json_wrapper.get_array("data")
            for item_in_data in data_array.get_items():
                fee_rate = FeeRate()
                fee_rate.symbol = item_in_data.get_string("symbol")
                fee_rate.maker_fee = item_in_data.get_string("maker-fee")
                fee_rate.symbol = item_in_data.get_string("taker-fee")
                fee_list.append(fee_rate)
            return fee_list

        request.json_parser = parse
        return request
示例#24
0
    def cancel_order(self, symbol, order_id):
        check_symbol(symbol)
        check_should_not_none(order_id, "order_id")
        path = "/v1/order/orders/{}/submitcancel"
        path = path.format(order_id)
        request = self.__create_request_by_post_with_signature(path, UrlParamsBuilder())

        def parse(json_wrapper):
            return

        request.json_parser = parse
        return request
示例#25
0
    def cancel_withdraw(self, currency, withdraw_id):
        check_symbol(currency)
        check_should_not_none(withdraw_id, "withdraw_id")
        path = "/v1/dw/withdraw-virtual/{}/cancel"
        path = path.format(withdraw_id)
        request = self.__create_request_by_post_with_signature(path, UrlParamsBuilder())

        def parse(json_wrapper):
            return

        request.json_parser = parse
        return request
示例#26
0
 def __create_request_by_get_with_signature(self,
                                            url,
                                            builder=UrlParamsBuilder()):
     request = RestApiRequest()
     request.method = "GET"
     request.host = self.__server_url
     create_signature(self.__api_key, self.__secret_key, request.method,
                      request.host + url, builder)
     request.header.update(
         {"Content-Type": "application/x-www-form-urlencoded"})
     request.url = url + builder.build_url()
     return request
示例#27
0
    def get_24h_trade_statistics(self, symbol):
        check_symbol(symbol)
        builder = UrlParamsBuilder()
        builder.put_url("symbol", symbol)
        request = self.__create_request_by_get("/market/detail", builder)

        def parse(json_wrapper):
            tick = json_wrapper.get_object("tick")
            trade_statistics = TradeStatistics()
            trade_statistics.timestamp = convert_cst_in_millisecond_to_utc(json_wrapper.get_int("ts"))
            trade_statistics.amount = tick.get_float("amount")
            trade_statistics.open = tick.get_float("open")
            trade_statistics.close = tick.get_float("close")
            trade_statistics.high = tick.get_float("high")
            trade_statistics.low = tick.get_float("low")
            trade_statistics.count = tick.get_int("count")
            trade_statistics.volume = tick.get_float("vol")
            return trade_statistics

        request.json_parser = parse
        return request
    def get_price_depth(self, symbol, size=None):
        check_symbol(symbol)
        check_range(size, 1, 150, "size")
        builder = UrlParamsBuilder()
        builder.put_url("symbol", symbol)
        builder.put_url("type", "step0")
        request = self.__create_request_by_get("/market/depth", builder)

        def parse(json_wrapper):
            tick = json_wrapper.get_object("tick")
            dp = PriceDepth()
            dp.timestamp = convert_cst_in_millisecond_to_utc(
                tick.get_int("ts"))
            bid_array = tick.get_array("bids")
            ask_array = tick.get_array("asks")
            bids = list()
            asks = list()
            for i in range(0, size):
                bid_entry = bid_array.get_array_at(i)
                entry = DepthEntry()
                entry.price = bid_entry.get_float_at(0)
                entry.amount = bid_entry.get_float_at(1)
                bids.append(entry)
            for i in range(0, size):
                ask_entry = ask_array.get_array_at(i)
                entry = DepthEntry()
                entry.price = ask_entry.get_float_at(0)
                entry.amount = ask_entry.get_float_at(1)
                asks.append(entry)
            dp.bids = bids
            dp.asks = asks
            return dp

        request.json_parser = parse
        return request
    def get_historical_trade(self, symbol, form_id, size):
        check_symbol(symbol)
        check_range(size, 1, 2000, "size")
        builder = UrlParamsBuilder()
        builder.put_url("symbol", symbol)
        builder.put_url("size", size)
        request = self.__create_request_by_get("/market/history/trade",
                                               builder)

        def parse(json_wrapper):
            data_array = json_wrapper.get_array("data")
            trade_list = list()
            for item in data_array.get_items():
                data_array_in = item.get_array("data")
                for item_in in data_array_in.get_items():
                    local_trade = Trade()
                    local_trade.price = item_in.get_float("price")
                    local_trade.amount = item_in.get_float("amount")
                    local_trade.trade_id = item_in.get_int("id")
                    local_trade.timestamp = convert_cst_in_millisecond_to_utc(
                        item_in.get_int("ts"))
                    local_trade.direction = item_in.get_string("direction")
                    trade_list.append(local_trade)
            return trade_list

        request.json_parser = parse
        return request
    def transfer(self, symbol, from_account, to_account, currency, amount):
        check_symbol(symbol)
        check_should_not_none(from_account, "from_account")
        check_should_not_none(to_account, "to_account")
        check_should_not_none(currency, "currency")
        check_should_not_none(amount, "amount")
        if from_account == AccountType.SPOT and to_account == AccountType.MARGIN:
            address = "/v1/dw/transfer-in/margin"
        elif from_account == AccountType.MARGIN and AccountType.SPOT:
            address = "/v1/dw/transfer-out/margin"
        else:
            raise HuobiApiException(HuobiApiException.INPUT_ERROR,
                                    "[Input] incorrect transfer type")
        builder = UrlParamsBuilder()
        builder.put_post("currency", currency)
        builder.put_post("symbol", symbol)
        builder.put_post("amount", amount)
        request = self.__create_request_by_post_with_signature(
            address, builder)

        def parse(json_wrapper):
            if json_wrapper.get_string("status") == "ok":
                return json_wrapper.get_int("data")

        request.json_parser = parse
        return request