def subscribe_account_event(self, mode, callback, error_handler=None):
        check_should_not_none(mode, "mode")
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            connection.send(account_channel(mode))

        def json_parse(json_wrapper):
            account_event = AccountEvent()
            account_event.timestamp = convert_cst_in_millisecond_to_utc(
                json_wrapper.get_int("ts"))
            data = json_wrapper.get_object("data")
            account_event.change_type = data.get_string("event")
            list_array = data.get_array("list")
            account_change_list = list()
            for item in list_array.get_items():
                account_change = AccountChange()
                account_change.account_type = account_info_map.get_account_by_id(
                    self.__api_key, item.get_int("account-id")).account_type
                account_change.currency = item.get_string("currency")
                account_change.balance = item.get_float("balance")
                account_change.balance_type = item.get_string("type")
                account_change_list.append(account_change)
            account_event.account_change_list = account_change_list
            return account_event

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = True
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
    def subscribe_trade_event(self, symbols, callback, error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(trade_channel(val))
                time.sleep(0.01)

        def json_parse(json_wrapper):
            ch = json_wrapper.get_string("ch")
            parse = ChannelParser(ch)
            trade_event = TradeEvent()
            trade_event.symbol = parse.symbol
            trade_event.timestamp = convert_cst_in_millisecond_to_utc(
                json_wrapper.get_int("ts"))
            tick = json_wrapper.get_object("tick")
            data_array = tick.get_array("data")
            trade_list = list()
            for item in data_array.get_items():
                trade = Trade.json_parse(item)
                trade_list.append(trade)
            trade_event.trade_list = trade_list
            return trade_event

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = False
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
    def request_candlestick_event(self,
                                  symbols,
                                  interval,
                                  callback,
                                  from_ts_second,
                                  end_ts_second,
                                  auto_close,
                                  error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(interval, "interval")
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(
                    request_kline_channel(val, interval, from_ts_second,
                                          end_ts_second))
                time.sleep(0.01)

        def json_parse(json_wrapper):
            candle_stick_event_obj = CandlestickRequest.json_parse(
                json_wrapper)
            candle_stick_event_obj.interval = interval
            return candle_stick_event_obj

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.auto_close = auto_close
        request.is_trading = False
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
    def subscribe_order_update_new(self,
                                   symbols,
                                   callback,
                                   error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(orders_update_new_channel(val))
                time.sleep(0.01)

        def json_parse(json_wrapper):
            ch = json_wrapper.get_string("topic")
            parse = ChannelParser(ch)
            order_update_event = OrderUpdateNewEvent()
            order_update_event.symbol = parse.symbol
            order_update_event.timestamp = convert_cst_in_millisecond_to_utc(
                json_wrapper.get_int("ts"))
            data = json_wrapper.get_object("data")
            order = OrderUpdateNew.json_parse(data)

            order_update_event.data = order
            return order_update_event

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = True
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
Пример #5
0
    def subscribe_orders_update_event(self, symbol, callback, error_handler=None):
        check_should_not_none(symbol, "symbol")
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            connection.send(orders_update_channel(symbol))

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.auto_close = False
        request.is_trading = True
        request.json_parser = OrdersUpdateEvent.json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        request.api_version = ApiVersion.VERSION_V2
        return request
    def subscribe_price_depth_event(self,
                                    symbols,
                                    callback,
                                    error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(price_depth_channel(val))
                time.sleep(0.01)

        def json_parse(json_wrapper):
            ch = json_wrapper.get_string("ch")
            parse = ChannelParser(ch)
            price_depth_event = PriceDepthEvent()
            price_depth_event.symbol = parse.symbol
            price_depth_event.timestamp = convert_cst_in_millisecond_to_utc(
                json_wrapper.get_int("ts"))
            price_depth = PriceDepth()
            tick = json_wrapper.get_object("tick")
            bid_list = list()
            bids_array = tick.get_array("bids")
            for item in bids_array.get_items_as_array():
                depth_entry = DepthEntry()
                depth_entry.price = item.get_float_at(0)
                depth_entry.amount = item.get_float_at(1)
                bid_list.append(depth_entry)
            ask_list = list()
            asks_array = tick.get_array("asks")
            for item in asks_array.get_items_as_array():
                depth_entry = DepthEntry()
                depth_entry.price = item.get_float_at(0)
                depth_entry.amount = item.get_float_at(1)
                ask_list.append(depth_entry)
            price_depth.bids = bid_list
            price_depth.asks = ask_list
            price_depth_event.data = price_depth
            return price_depth_event

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = False
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
    def request_order_list_event(self,
                                 symbol,
                                 account_id,
                                 callback,
                                 order_states,
                                 client_req_id,
                                 auto_close,
                                 error_handler=None):
        check_should_not_none(symbol, "symbol")
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            connection.send(
                request_order_list_channel(symbol=symbol,
                                           account_id=account_id,
                                           states_str=order_states,
                                           client_req_id=client_req_id))

        def get_account_type_map(json_wrapper):
            #get account type mapping
            account_id_type_map = {}
            error_code = json_wrapper.get_int("err-code")
            if error_code == 0:
                order_list_json = json_wrapper.get_array("data")
                for order_json in order_list_json.get_items():
                    account_id = order_json.get_int("account-id")
                    account_type = account_info_map.get_account_by_id(
                        self.__api_key, account_id).account_type
                    account_id_type_map[account_id] = account_type
            return account_id_type_map

        def json_parse(json_wrapper):
            account_type_map = get_account_type_map(json_wrapper)
            req_obj = OrderListRequest.json_parse(json_wrapper,
                                                  account_type_map)
            req_obj = OrderListRequest.update_symbol(req_obj)
            return req_obj

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.auto_close = auto_close
        request.is_trading = True
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
    def request_account_balance_event(self,
                                      callback,
                                      client_req_id,
                                      auto_close,
                                      error_handler=None):
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            connection.send(request_account_list_channel(client_req_id))

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.auto_close = auto_close
        request.is_trading = True
        request.json_parser = AccountBalanceRequest.json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
    def subscribe_accounts_update_event(self, mode, callback, error_handler=None):
        check_should_not_none(callback, "callback")
        if str(mode) == AccountBalanceMode.TOTAL:
            mode = AccountBalanceMode.TOTAL
        else:
            mode = AccountBalanceMode.BALANCE

        def subscription_handler(connection):
            connection.send(accounts_update_channel(mode))

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.auto_close = False
        request.is_trading = True
        request.json_parser = AccountsUpdateEvent.json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        request.api_version = ApiVersion.VERSION_V2
        return request
    def subscribe_24h_trade_statistics_event(self,
                                             symbols,
                                             callback,
                                             error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(trade_statistics_channel(val))
                time.sleep(0.01)

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = False
        request.json_parser = TradeStatisticsEvent.json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
Пример #11
0
    def subscribe_order_update_new(self,
                                   symbols,
                                   callback,
                                   error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(orders_update_new_channel(val))
                time.sleep(0.01)

        def json_parse(json_wrapper):
            ch = json_wrapper.get_string("topic")
            parse = ChannelParser(ch)
            order_update_event = OrderUpdateNewEvent()
            order_update_event.symbol = parse.symbol
            order_update_event.timestamp = convert_cst_in_millisecond_to_utc(
                json_wrapper.get_int("ts"))
            data = json_wrapper.get_object("data")
            order = OrderUpdateNew()

            order.match_id = data.get_int("match-id")
            order.order_id = data.get_int("order-id")
            order.symbol = parse.symbol
            order.state = data.get_string("order-state")
            order.role = data.get_string("role")
            order.price = data.get_float("price")
            order.filled_amount = data.get_float("filled-amount")
            order.filled_cash_amount = data.get_float("filled-cash-amount")
            order.unfilled_amount = data.get_float("unfilled-amount")
            order.client_order_id = data.get_string("client-order-id")

            order_update_event.data = order
            return order_update_event

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = True
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
Пример #12
0
    def subscribe_candlestick_event(self,
                                    symbols,
                                    interval,
                                    callback,
                                    error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(interval, "interval")
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(kline_channel(val, interval))
                time.sleep(0.01)

        def json_parse(json_wrapper):
            ch = json_wrapper.get_string("ch")
            parse = ChannelParser(ch)
            candlestick_event = CandlestickEvent()
            candlestick_event.symbol = parse.symbol
            candlestick_event.interval = interval
            candlestick_event.timestamp = convert_cst_in_millisecond_to_utc(
                json_wrapper.get_int("ts"))
            tick = json_wrapper.get_object("tick")
            data = Candlestick()
            data.timestamp = convert_cst_in_second_to_utc(tick.get_int("id"))
            data.open = tick.get_float("open")
            data.close = tick.get_float("close")
            data.low = tick.get_float("low")
            data.high = tick.get_float("high")
            data.amount = tick.get_float("amount")
            data.count = tick.get_int("count")
            data.volume = tick.get_float("vol")
            candlestick_event.data = data
            return candlestick_event

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = False
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
    def subscribe_order_update(self, symbols, callback, error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(orders_channel(val))
                time.sleep(0.01)

        def json_parse(json_wrapper):
            ch = json_wrapper.get_string("topic")
            parse = ChannelParser(ch)
            order_update_event = OrderUpdateEvent()
            order_update_event.symbol = parse.symbol
            order_update_event.timestamp = convert_cst_in_millisecond_to_utc(
                json_wrapper.get_int("ts"))
            data = json_wrapper.get_object("data")
            order = Order()
            order.order_id = data.get_int("order-id")
            order.symbol = parse.symbol
            order.account_type = account_info_map.get_account_by_id(
                self.__api_key, data.get_int("account-id")).account_type
            order.amount = data.get_float("order-amount")
            order.price = data.get_float("order-price")
            order.created_timestamp = convert_cst_in_millisecond_to_utc(
                data.get_int("created-at"))
            order.order_type = data.get_string("order-type")
            order.filled_amount = data.get_float("filled-amount")
            order.filled_cash_amount = data.get_float("filled-cash-amount")
            order.filled_fees = data.get_float("filled-fees")
            order.state = data.get_string("order-state")
            order.source = data.get_string("order-source")
            order_update_event.data = order
            return order_update_event

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = True
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
Пример #14
0
    def subscribe_trade_clearing_event(self, symbols, callback, error_handler=None):
        if ("*" in symbols):
            symbols = ["*"]
        else:
            check_symbol_list(symbols)

        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(trade_clearing_channel(val))
                time.sleep(0.01)

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.auto_close = False
        request.is_trading = True
        request.json_parser = TradeClearingEvent.json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        request.api_version = ApiVersion.VERSION_V2
        return request
    def subscribe_price_depth_event(self,
                                    symbols,
                                    depth_step,
                                    callback,
                                    error_handler=None):
        check_symbol_list(symbols)
        new_step = PriceDepth.get_valid_depth_step(
            value=depth_step, defalut_value=DepthStep.STEP0)
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(price_depth_channel(val, new_step))
                time.sleep(0.01)

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = False
        request.json_parser = PriceDepthEvent.json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
Пример #16
0
    def subscribe_24h_trade_statistics_event(self,
                                             symbols,
                                             callback,
                                             error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(trade_statistics_channel(val))
                time.sleep(0.01)

        def json_parse(json_wrapper):
            ch = json_wrapper.get_string("ch")
            parse = ChannelParser(ch)
            trade_statistics_event = TradeStatisticsEvent()
            trade_statistics_event.symbol = parse.symbol
            ts = convert_cst_in_millisecond_to_utc(json_wrapper.get_int("ts"))
            trade_statistics_event.timestamp = ts
            tick = json_wrapper.get_object("tick")
            statistics = TradeStatistics()
            statistics.amount = tick.get_float("amount")
            statistics.open = tick.get_float("open")
            statistics.close = tick.get_float("close")
            statistics.high = tick.get_float("high")
            statistics.timestamp = ts
            statistics.count = tick.get_int("count")
            statistics.low = tick.get_float("low")
            statistics.volume = tick.get_float("vol")
            trade_statistics_event.trade_statistics = statistics
            return trade_statistics_event

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.is_trading = False
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
Пример #17
0
    def request_mbp_event(self,
                          symbols,
                          level,
                          callback,
                          auto_close,
                          error_handler=None):
        check_symbol_list(symbols)
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            for val in symbols:
                connection.send(request_mbp_channel(val, level))
                time.sleep(0.01)

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.auto_close = auto_close
        request.is_trading = False
        request.json_parser = MbpRequest.json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request
    def request_order_detail_event(self,
                                   order_id,
                                   callback,
                                   client_req_id,
                                   auto_close,
                                   error_handler=None):
        check_should_not_none(order_id, "order_id")
        check_should_not_none(callback, "callback")

        def subscription_handler(connection):
            connection.send(
                request_order_detail_channel(order_id, client_req_id))

        def get_account_type_map(json_wrapper):
            #get account type mapping
            account_id_type_map = {}
            data = json_wrapper.get_object("data")
            account_id = data.get_int("account-id")
            account_type = account_info_map.get_account_by_id(
                self.__api_key, account_id).account_type
            account_id_type_map[account_id] = account_type
            return account_id_type_map

        def json_parse(json_wrapper):
            account_type_map = get_account_type_map(json_wrapper)
            req_obj = OrderDetailRequest.json_parse(json_wrapper,
                                                    account_type_map)
            return req_obj

        request = WebsocketRequest()
        request.subscription_handler = subscription_handler
        request.auto_close = auto_close
        request.is_trading = True
        request.json_parser = json_parse
        request.update_callback = callback
        request.error_handler = error_handler
        return request