def request_account_list_channel(client_req_id=None): channel = dict() channel["op"] = "req" channel["topic"] = "accounts.list" channel["cid"] = str(client_req_id) if client_req_id else str( get_current_timestamp()) return json.dumps(channel)
def account_channel(mode): channel = dict() channel["op"] = "sub" channel["cid"] = str(get_current_timestamp()) channel["topic"] = "accounts" channel["mode"] = mode return json.dumps(channel)
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
def on_message(self, message): self.last_receive_time = get_current_timestamp() json_wrapper = parse_json_from_string(gzip.decompress(message).decode("utf-8")) #print("RX: " + gzip.decompress(message).decode("utf-8")) if json_wrapper.contain_key("status") and json_wrapper.get_string("status") != "ok": error_code = json_wrapper.get_string_or_default("err-code", "Unknown error") error_msg = json_wrapper.get_string_or_default("err-msg", "Unknown error") self.on_error(error_code + ": " + error_msg) elif json_wrapper.contain_key("err-code") and json_wrapper.get_int("err-code") != 0: error_code = json_wrapper.get_string_or_default("err-code", "Unknown error") error_msg = json_wrapper.get_string_or_default("err-msg", "Unknown error") self.on_error(error_code + ": " + error_msg) elif json_wrapper.contain_key("op"): op = json_wrapper.get_string("op") if op == "notify": self.__on_receive(json_wrapper) elif op == "ping": ping_ts = json_wrapper.get_string("ts") self.__process_ping_on_trading_line(ping_ts) elif op == "auth": if self.request.subscription_handler is not None: self.request.subscription_handler(self) elif op == "req": self.__on_receive(json_wrapper) elif json_wrapper.contain_key("ch"): self.__on_receive(json_wrapper) elif json_wrapper.contain_key("rep"): self.__on_receive(json_wrapper) elif json_wrapper.contain_key("ping"): ping_ts = json_wrapper.get_string("ping") self.__process_ping_on_market_line(ping_ts) else: print("unknown data process, RX: " + gzip.decompress(message).decode("utf-8"))
def request_order_detail_channel(order_id, client_req_id=None): channel = dict() channel["op"] = "req" channel["topic"] = "orders.detail" channel["order-id"] = str(order_id) channel["cid"] = str(client_req_id) if client_req_id else str( get_current_timestamp()) return json.dumps(channel)
def request_kline_channel(symbol, interval, from_ts_second=None, to_ts_second=None): channel = dict() channel["req"] = "market." + symbol + ".kline." + interval channel["id"] = str(get_current_timestamp()) if from_ts_second: channel["from"] = int(from_ts_second) if to_ts_second: channel["to"] = int(to_ts_second) return json.dumps(channel)
def request_order_list_channel(symbol, account_id, states_str=None, client_req_id=None): channel = dict() try: channel["op"] = "req" channel["account-id"] = account_id channel["topic"] = "orders.list" channel["symbol"] = symbol if states_str and len(states_str): channel["states"] = str(states_str) channel["cid"] = str(client_req_id) if client_req_id else str( get_current_timestamp()) except Exception as e: print(e) return json.dumps(channel)
def watch_dog_job(*args): watch_dog_instance = args[0] for connection in watch_dog_instance.connection_list: if connection.state == ConnectionState.CONNECTED: if watch_dog_instance.is_auto_connect: ts = get_current_timestamp() - connection.last_receive_time if ts > watch_dog_instance.receive_limit_ms: watch_dog_instance.logger.warning( "[Sub][" + str(connection.id) + "] No response from server") connection.re_connect_in_delay( watch_dog_instance.connection_delay_failure) elif connection.in_delay_connection(): watch_dog_instance.logger.warning("[Sub] call re_connect") connection.re_connect() pass elif connection.state == ConnectionState.CLOSED_ON_ERROR: if watch_dog_instance.is_auto_connect: connection.re_connect_in_delay( watch_dog_instance.connection_delay_failure) pass
def trade_statistics_channel(symbol): channel = dict() channel["sub"] = "market." + symbol + ".detail" channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def on_message(self, message): self.last_receive_time = get_current_timestamp() if isinstance(message, (str)): #print("RX string : ", message) json_wrapper = parse_json_from_string(message) elif isinstance(message, (bytes)): #print("RX bytes: " + gzip.decompress(message).decode("utf-8")) json_wrapper = parse_json_from_string( gzip.decompress(message).decode("utf-8")) else: print("RX unknow type : ", type(message)) return if json_wrapper.contain_key( "status") and json_wrapper.get_string("status") != "ok": error_code = json_wrapper.get_string_or_default( "err-code", "Unknown error") error_msg = json_wrapper.get_string_or_default( "err-msg", "Unknown error") self.on_error(error_code + ": " + error_msg) elif json_wrapper.contain_key( "err-code") and json_wrapper.get_int("err-code") != 0: error_code = json_wrapper.get_string_or_default( "err-code", "Unknown error") error_msg = json_wrapper.get_string_or_default( "err-msg", "Unknown error") self.on_error(error_code + ": " + error_msg) elif json_wrapper.contain_key("op"): op = json_wrapper.get_string("op") if op == "notify": self.__on_receive(json_wrapper) elif op == "ping": ping_ts = json_wrapper.get_string("ts") self.__process_ping_on_trading_line(ping_ts) elif op == "auth": if self.request.subscription_handler is not None: self.request.subscription_handler(self) elif op == "req": self.__on_receive(json_wrapper) elif json_wrapper.contain_key("action"): # for V2 action_name = json_wrapper.get_string("action") if action_name == "ping": action_data = json_wrapper.get_object("data") ping_ts = action_data.get_string("ts") self.__process_ping_on_v2_trade(ping_ts) elif action_name == "sub": action_code = json_wrapper.get_int("code") if action_code == 200: logging.info("subscribe ACK received") else: logging.error("receive error data : " + message) elif action_name == "req": # action_code = json_wrapper.get_int("code") if action_code == 200: logging.info("signature ACK received") if self.request.subscription_handler is not None: self.request.subscription_handler(self) else: logging.error("receive error data : " + message) elif action_name == "push": action_data = json_wrapper.get_object("data") if action_data: self.__on_receive(json_wrapper) else: logging.error("receive error push data : " + message) elif json_wrapper.contain_key("ch"): self.__on_receive(json_wrapper) elif json_wrapper.contain_key("rep"): self.__on_receive(json_wrapper) elif json_wrapper.contain_key("ping"): ping_ts = json_wrapper.get_string("ping") self.__process_ping_on_market_line(ping_ts) else: print("unknown data process, RX: " + gzip.decompress(message).decode("utf-8"))
def orders_update_new_channel(symbol): channel = dict() channel["op"] = "sub" channel["cid"] = str(get_current_timestamp()) channel["topic"] = "orders." + symbol + ".update" return json.dumps(channel)
def kline_channel(symbol, interval): channel = dict() channel["sub"] = "market." + symbol + ".kline." + interval channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def __process_ping_on_trading_line(self): self.send("{\"op\":\"pong\",\"ts\":" + str(get_current_timestamp()) + "}") return
def price_depth_channel(symbol): channel = dict() channel["sub"] = "market." + symbol + ".depth.step0" channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def __process_ping_on_market_line(self): self.send("{\"pong\":" + str(get_current_timestamp()) + "}") return
def request_trade_channel(symbol): channel = dict() channel["req"] = "market." + symbol + ".trade.detail" channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def price_depth_channel(symbol, step_type=DepthStep.STEP0): channel = dict() channel["sub"] = "market." + symbol + ".depth." + step_type channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def request_price_depth_channel(symbol, step_type="step0"): channel = dict() channel["req"] = "market." + symbol + ".depth." + step_type channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def request_mbp_channel(symbol, levels): channel = dict() channel["req"] = "market.{symbol}.mbp.{levels}".format(symbol=symbol, levels=levels) channel["id"] = str(get_current_timestamp()) return json.dumps(channel)