From f3360cf6d635d2d2e041263726af19c5b35e66ed Mon Sep 17 00:00:00 2001 From: Vyacheslav Demidyuk Date: Fri, 21 Feb 2025 12:23:42 +0200 Subject: [PATCH] PyMtApi5: implemented function ChartFirst --- PyMtApi5/client.py | 17 +++++++++++++---- PyMtApi5/mt5apiclient.py | 7 +++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/PyMtApi5/client.py b/PyMtApi5/client.py index cef4479a..ea2ba154 100644 --- a/PyMtApi5/client.py +++ b/PyMtApi5/client.py @@ -65,6 +65,7 @@ class Mt5ApiApp: "ChartTimePriceToXY": self.process_chart_time_price_to_xy, "ChartXYToTimePrice": self.process_chart_xy_to_time_price, "ChartOpen": self.process_chart_open, + "ChartFirst": self.process_chart_first, } def on_disconnect(self, error_msg=None): @@ -96,15 +97,19 @@ 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"pieces count: {len(pieces)}") + if len(pieces) == 0 or len(pieces) > 2: print(f"! Invalid command format: {command.rstrip()}") return - if pieces[0] not in self.cmd_functions: - print(f"! Unknown command: {pieces[0]}") + command_name = pieces[0].rstrip() + if command_name not in self.cmd_functions: + print(f"! Unknown command: '{command_name}'") return + if len(pieces) == 1: + pieces.append("") params = pieces[1].rstrip() try: - self.cmd_functions[pieces[0]](mtapi, params) + self.cmd_functions[command_name](mtapi, params) except Exception as e: print(f"Failed to process command {command.rstrip()}: {e}") @@ -487,6 +492,10 @@ class Mt5ApiApp: result = mtapi.chart_open(pieces[0], period) print(f"> ChartOpen: response = {result}") + def process_chart_first(self, mtapi, _): + result = mtapi.chart_first() + print(f"> ChartFirst: 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 4a75b19d..144bdbba 100644 --- a/PyMtApi5/mt5apiclient.py +++ b/PyMtApi5/mt5apiclient.py @@ -498,8 +498,11 @@ class Mt5ApiClient: # ChartOpen def chart_open(self, symbol: str, period: ENUM_TIMEFRAMES): cmd_params = {"Symbol": symbol, "Timeframe": period}; - res = self.__send_command(self.__get_default_expert(), Mt5CommandType.ChartOpen, cmd_params) - return res + return self.__send_command(self.__get_default_expert(), Mt5CommandType.ChartOpen, cmd_params) + + # ChartFirst + def chart_first(self): + return self.__send_command(self.__get_default_expert(), Mt5CommandType.ChartFirst) # Private methods