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 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 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 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 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 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())