11"""Utility module for MetaTrader 5 integration.
2+
23Provides helper functions and classes for trading operations.
34"""
45
5- from datetime import datetime
6+ import logging
7+ from datetime import datetime , timezone
68
79import MetaTrader5 as Mt5
810
11+ # Configure logging
12+ logger = logging .getLogger (__name__ )
13+ logger .setLevel (logging .INFO )
14+
15+ # Create console handler with formatting
16+ console_handler = logging .StreamHandler ()
17+ formatter = logging .Formatter ("%(asctime)s - %(name)s - %(levelname)s - %(message)s" )
18+ console_handler .setFormatter (formatter )
19+ logger .addHandler (console_handler )
20+
921
1022class Utilities :
1123 """A utility class for handling trading-related functionalities."""
@@ -32,22 +44,20 @@ def check_trade_availability(self, symbol: str, count_until: int) -> bool:
3244 if len (Mt5 .positions_get (symbol = symbol )) == 1 :
3345 self .__recent_trade = True
3446
35- if len (Mt5 .positions_get (symbol = symbol )) != 1 and self .__recent_trade :
36- if not self .__allow_to_count :
37- self .__allow_to_count = True
38- self .__allowed_to_trade = False
39- self .__recent_trade = False
47+ if len (Mt5 .positions_get (symbol = symbol )) != 1 and self .__recent_trade and not self .__allow_to_count :
48+ self .__allow_to_count = True
49+ self .__allowed_to_trade = False
4050
41- if datetime .now ().second == 0 and self .__counter_flag and self .__allow_to_count :
42- print (f"Trading will be allowed in { count_until - self .__minutes_counter } minutes." )
51+ if datetime .now (timezone . utc ).second == 0 and self .__counter_flag and self .__allow_to_count :
52+ logger . info (f"Trading will be allowed in { count_until - self .__minutes_counter } minutes." )
4353 self .__minutes_counter += 1
4454 self .__counter_flag = False
4555
46- if datetime .now ().second == 59 :
56+ if datetime .now (timezone . utc ).second == 59 :
4757 self .__counter_flag = True
4858
4959 if self .__minutes_counter == count_until :
50- print ("Trading is allowed.\n " )
60+ logger . info ("Trading is allowed.\n " )
5161 self .__reset_counters ()
5262
5363 return self .__allowed_to_trade
0 commit comments