Exemple #1
0
    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()
                trade.amount = item.get_float("amount")
                trade.price = item.get_float("price")
                trade.trade_id = item.get_string("id")
                trade.direction = item.get_string("direction")
                trade.timestamp = convert_cst_in_millisecond_to_utc(item.get_int("ts"))
                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
Exemple #2
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