From 669e3604f330315790bb504c8ef6466747217256 Mon Sep 17 00:00:00 2001 From: Vyacheslav Demidyuk Date: Fri, 24 Jan 2025 22:49:13 +0200 Subject: [PATCH] PyMtApi5: implemented function MarketBookGet --- PyMtApi5/client.py | 16 ++++++++++++++-- PyMtApi5/mt5apiclient.py | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/PyMtApi5/client.py b/PyMtApi5/client.py index 79bcef99..5ba34a4b 100644 --- a/PyMtApi5/client.py +++ b/PyMtApi5/client.py @@ -44,6 +44,7 @@ class Mt5ApiApp: "SymbolInfoSessionTrade": self.process_symbol_info_session_trade, "MarketBookAdd": self.process_market_book_add, "MarketBookRelease": self.process_market_book_release, + "MarketBookGet": self.process_market_book_get, } def on_disconnect(self, error_msg=None): @@ -76,13 +77,16 @@ class Mt5ApiApp: def process_command(self, mtapi, command): pieces = command.split(" ", 1) if len(pieces) != 2 or not pieces[0] or not pieces[1]: - print(f"! Invalid command format: {command}") + print(f"! Invalid command format: {command.rstrip()}") return if pieces[0] not in self.cmd_functions: print(f"! Unknown command: {pieces[0]}") return params = pieces[1].rstrip() - self.cmd_functions[pieces[0]](mtapi, params) + try: + self.cmd_functions[pieces[0]](mtapi, params) + except Exception as e: + print(f"Failed to process command {command.rstrip()}: {e}") def process_account_info_double(self, mtapi, parameters): property_id = mt5enums.ENUM_ACCOUNT_INFO_DOUBLE(int(parameters)) @@ -266,6 +270,14 @@ class Mt5ApiApp: result = mtapi.market_book_release(symbol) print(f"> MarketBookRelease: response = {result}") + def process_market_book_get(self, mtapi, parameters): + if len(parameters) == 0: + print(f"! Invalid parameters for command MarketBookGet: {parameters} - {len(parameters)}") + return + symbol = parameters + result = mtapi.market_book_get(symbol) + print(f"> MarketBookGet: response = {result}") + def mtapi_command_thread(self, mtapi): while mtapi.is_connected(): filename = "client.cmd" diff --git a/PyMtApi5/mt5apiclient.py b/PyMtApi5/mt5apiclient.py index 31f49918..a4fd2e4e 100644 --- a/PyMtApi5/mt5apiclient.py +++ b/PyMtApi5/mt5apiclient.py @@ -130,6 +130,15 @@ class MqlTradeResult: f"bid = {self.bid}, ask = {self.ask}, comment = {self.comment}, request_id = {self.request_id}" ) +class MqlBookInfo: + def __init__(self, mql_book_info): + self.book_type = ENUM_BOOK_TYPE(mql_book_info["type"]) + self.price = mql_book_info["price"] + self.volume = mql_book_info["volume"] + self.volume_real = mql_book_info["volume_real"] + + def __repr__(self): + return ( f"book_type = {self.book_type}, price = {self.price}, volume = {self.volume}, volume_real = {self.volume_real}" ) class Mt5ApiClient: def __init__(self, address, port, callback=None): @@ -376,6 +385,11 @@ class Mt5ApiClient: cmd_params = {"Symbol": symbol} return self.__send_command(self.__get_default_expert(), Mt5CommandType.MarketBookRelease, cmd_params) + # MarketBookGet + def market_book_get(self, symbol: str): + cmd_params = {"Symbol": symbol} + return self.__send_command(self.__get_default_expert(), Mt5CommandType.MarketBookGet, cmd_params) + # Private methods def __event_thread_func(self): @@ -404,8 +418,9 @@ class Mt5ApiClient: response_json = json.loads(response) error_code = int(response_json["ErrorCode"]) if error_code != 0: - self.__logger.warning(f"send_command: ErrorCode = {response.ErrorCode}. {response.ErrorMessage}") - raise Exception(f"Failed to send command: ErrorCode = {response.ErrorCode}. {response.ErrorMessage} ") + error_message = response_json["ErrorMessage"] + self.__logger.warning(f"send_command: ErrorCode = {error_code}. {error_message}") + raise Exception(f"Failed to send command: ErrorCode = {error_code}. {error_message} ") return response_json["Value"] def __process_tick_event(self, payload):