def open(self, symbol_list, interval): streams = StringWrapper() streams.add_string_array(symbol_list, True) streams.combine_string_items( "", CEIUtils.string_replace("@kline_{0}", interval), "/") self.__connection.connect( CEIUtils.string_replace("/ws/{0}", streams.to_string()))
def open(self, channel, name, on_connect): on_ping_event = WebSocketEvent(True) def on_ping_event_trigger(msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) json_checker = JsonChecker() json_checker.check_equal("op", "ping", root_obj) return json_checker.complete() on_ping_event.set_trigger(on_ping_event_trigger) def on_ping_event_event(connection, msg): ts = CEIUtils.get_now("Unix_ms") json_result = JsonWrapper() json_result.add_json_string("op", "pong") json_result.add_json_string("ts", ts) connection.send(json_result.to_json_string()) on_ping_event.set_event(on_ping_event_event) self.__connection.register_event(on_ping_event) def on_connect_event(connection): login = JsonWrapper() login.add_json_string("op", "login") obj = JsonWrapper() obj.add_json_string("Name", name) login.add_json_object("param", obj) obj0 = JsonWrapper() obj0.add_json_number("[]", float("1")) obj0.add_json_number("[]", float("2")) login.add_json_object("array", obj0) connection.send(login.to_json_string()) on_connect(connection) self.__connection.set_on_connect(on_connect_event) self.__connection.connect(CEIUtils.string_replace("/websocket/{0}", channel))
def subscript_order(self, symbol, on_sub): on_sub_event = WebSocketEvent(False) def on_sub_event_trigger(msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) json_checker = JsonChecker() json_checker.check_equal("action", "sub", root_obj) json_checker.value_include("ch", "orders", root_obj) return json_checker.complete() on_sub_event.set_trigger(on_sub_event_trigger) def on_sub_event_event(connection, msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) code_var = Code() code_var.code = root_obj.get_int("code") on_sub(code_var) on_sub_event.set_event(on_sub_event_event) self.__connection.register_event(on_sub_event) json = JsonWrapper() json.add_json_string("action", "sub") json.add_json_string("ch", CEIUtils.string_replace("orders#{0}", symbol)) self.__connection.send(json.to_json_string())
def restful_auth(request, option): request.add_header_string("X-MBX-APIKEY", option.api_key) ts = CEIUtils.get_now("Unix_ms") request.add_query_string("timestamp", ts) query_string = CEIUtils.combine_query_string(request, CEIUtils.Constant.NONE, "&") post_body = CEIUtils.get_request_info(request, CEIUtils.Constant.POSTBODY, CEIUtils.Constant.NONE) buffer = StringWrapper() buffer.append_string_item(query_string) buffer.append_string_item(post_body) buffer.combine_string_items("", "", "") hmac = CEIUtils.hmacsha256(buffer.to_string(), option.secret_key) output = CEIUtils.hex(hmac) request.add_query_string("signature", output)
def on_depth_event_trigger(msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) json_checker = JsonChecker() json_checker.check_equal( "ch", CEIUtils.string_replace("market.{0}.depth.{1}", symbol, type_u), root_obj) return json_checker.complete()
def request_depth(self, symbol, type_u, on_depth): on_depth_event = WebSocketEvent(True) def on_depth_event_trigger(msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) json_checker = JsonChecker() json_checker.check_equal( "ch", CEIUtils.string_replace("market.{0}.depth.{1}", symbol, type_u), root_obj) return json_checker.complete() on_depth_event.set_trigger(on_depth_event_trigger) def on_depth_event_event(connection, msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) depth_var = Depth() depth_var.ch = root_obj.get_string("ch") obj = root_obj.get_object("tick") obj0 = obj.get_array("bids") for item in obj0.array(): quote_var = Quote() quote_var.price = item.get_decimal("[0]") quote_var.amount = item.get_decimal("[1]") if depth_var.bids is None: depth_var.bids = list() depth_var.bids.append(quote_var) obj1 = obj.get_array("asks") for item2 in obj1.array(): quote_var3 = Quote() quote_var3.price = item2.get_decimal("[0]") quote_var3.amount = item2.get_decimal("[1]") if depth_var.asks is None: depth_var.asks = list() depth_var.asks.append(quote_var3) on_depth(depth_var) on_depth_event.set_event(on_depth_event_event) self.__connection.register_event(on_depth_event) ts = CEIUtils.get_now("Unix_ms") json = JsonWrapper() json.add_json_string( "sub", CEIUtils.string_replace("market.{0}.depth.{1}", symbol, type_u)) json.add_json_string("id", ts) self.__connection.send(json.to_json_string())
def on_candlestick_event_trigger(msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) json_checker = JsonChecker() json_checker.check_equal( "rep", CEIUtils.string_replace("market.{0}.kline.{1}", symbol, period), root_obj) return json_checker.complete()
def request_candlestick(self, symbol, period, on_candlestick): on_candlestick_event = WebSocketEvent(False) def on_candlestick_event_trigger(msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) json_checker = JsonChecker() json_checker.check_equal( "rep", CEIUtils.string_replace("market.{0}.kline.{1}", symbol, period), root_obj) return json_checker.complete() on_candlestick_event.set_trigger(on_candlestick_event_trigger) def on_candlestick_event_event(connection, msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) candlestick_var = Candlestick() obj = root_obj.get_array("data") for item in obj.array(): candlestick_data_var = CandlestickData() candlestick_data_var.id = item.get_int("id") candlestick_data_var.amount = item.get_decimal("amount") candlestick_data_var.count = item.get_int("count") candlestick_data_var.open = item.get_decimal("open") candlestick_data_var.close = item.get_decimal("close") candlestick_data_var.low = item.get_decimal("low") candlestick_data_var.high = item.get_decimal("high") candlestick_data_var.vol = item.get_decimal("vol") if candlestick_var.data is None: candlestick_var.data = list() candlestick_var.data.append(candlestick_data_var) on_candlestick(candlestick_var) on_candlestick_event.set_event(on_candlestick_event_event) self.__connection.register_event(on_candlestick_event) ts = CEIUtils.get_now("Unix_ms") json = JsonWrapper() json.add_json_string( "req", CEIUtils.string_replace("market.{0}.kline.{1}", symbol, period)) json.add_json_string("id", ts) self.__connection.send(json.to_json_string())
def open(self, channel, name): def on_connect_event(connection): login = JsonWrapper() login.add_json_string("op", "echo") obj = JsonWrapper() obj.add_json_string("Name", name) login.add_json_object("param", obj) connection.send("login") self.__connection.set_on_connect(on_connect_event) self.__connection.connect(CEIUtils.string_replace("/websocket/{0}", channel))
def cancel_order(self, order_id): request = RestfulRequest(self.__option) request.set_target( CEIUtils.string_replace("/v1/order/orders/{0}/submitcancel", str(order_id))) request.set_method(RestfulRequest.Method.POST) Procedures.restful_auth(request, self.__option) response = RestfulConnection.query(request) root_obj = JsonWrapper.parse_from_string(response.get_string()) order_idvar = OrderID() order_idvar.status = root_obj.get_string("status") order_idvar.data = root_obj.get_int("data") return order_idvar
def subscript_candlestick(self, symbol, period, on_candlestick): on_candlestick_event = WebSocketEvent(True) def on_candlestick_event_trigger(msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) json_checker = JsonChecker() json_checker.check_equal( "ch", CEIUtils.string_replace("market.{0}.kline.{1}", symbol, period), root_obj) return json_checker.complete() on_candlestick_event.set_trigger(on_candlestick_event_trigger) def on_candlestick_event_event(connection, msg): root_obj = JsonWrapper.parse_from_string(msg.get_string()) candlestick_data_var = CandlestickData() obj = root_obj.get_object("tick") candlestick_data_var.id = obj.get_int("id") candlestick_data_var.amount = obj.get_decimal("amount") candlestick_data_var.count = obj.get_int("count") candlestick_data_var.open = obj.get_decimal("open") candlestick_data_var.close = obj.get_decimal("close") candlestick_data_var.low = obj.get_decimal("low") candlestick_data_var.high = obj.get_decimal("high") candlestick_data_var.vol = obj.get_decimal("vol") on_candlestick(candlestick_data_var) on_candlestick_event.set_event(on_candlestick_event_event) self.__connection.register_event(on_candlestick_event) ts = CEIUtils.get_now("Unix_ms") json = JsonWrapper() json.add_json_string( "sub", CEIUtils.string_replace("market.{0}.kline.{1}", symbol, period)) json.add_json_string("id", ts) self.__connection.send(json.to_json_string())
def restful_auth(request, option): timestamp = CEIUtils.get_now("%Y':'%m':'%d'T'#H':'%M':'%S") request.add_query_string("AccessKeyId", option.api_key) request.add_query_string("SignatureMethod", "HmacSHA256") request.add_query_string("SignatureVersion", "2") request.add_query_string("Timestamp", timestamp) query_string = CEIUtils.combine_query_string(request, CEIUtils.Constant.ASC, "&") method = CEIUtils.get_request_info(request, CEIUtils.Constant.METHOD, CEIUtils.Constant.UPPERCASE) host = CEIUtils.get_request_info(request, CEIUtils.Constant.HOST, CEIUtils.Constant.NONE) target = CEIUtils.get_request_info(request, CEIUtils.Constant.TARGET, CEIUtils.Constant.NONE) buffer = StringWrapper() buffer.append_string_item(method) buffer.append_string_item(host) buffer.append_string_item(target) buffer.append_string_item(query_string) buffer.combine_string_items("", "", "\\n") hmacsha256 = CEIUtils.hmacsha256(buffer.to_string(), option.secret_key) result = CEIUtils.base64(hmacsha256) request.add_query_string("Signature", result)
def on_ping_event_event(connection, msg): ts = CEIUtils.get_now("Unix_ms") json_result = JsonWrapper() json_result.add_json_string("op", "pong") json_result.add_json_string("ts", ts) connection.send(json_result.to_json_string())
def on_any_message_event_event(connection, msg): decoded = CEIUtils.gzip(msg.get_bytes()) msg.upgrade(decoded)
def url(self, input_u): request = RestfulRequest(self.__option) request.set_target(CEIUtils.string_replace("/restful/get/url/{0}", input_u)) request.set_method(RestfulRequest.Method.GET) response = RestfulConnection.query(request) return response.get_string()