Skip to content

Commit e71c56e

Browse files
committed
optimize
1 parent afcee10 commit e71c56e

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

pymt5adapter/context.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def connected(*,
2727
logger: Callable = None,
2828
raise_on_errors: bool = False,
2929
debug_logging: bool = False,
30-
# force_namedtuple: bool = False,
30+
convert_namedtuples_to_dict:bool=False,
3131
**kwargs
3232
) -> None:
3333
"""Context manager for managing the connection with a MT5 terminal using the python ``with`` statement.
@@ -59,8 +59,9 @@ def connected(*,
5959
args = helpers.reduce_args(args)
6060
mt5_keys = ('path', 'portable', 'server', 'login', 'password', 'timeout')
6161
mt5_kwargs = {k: v for k, v in args.items() if k in mt5_keys}
62-
_state.global_debugging = bool(debug_logging)
63-
_state.raise_on_errors = bool(raise_on_errors)
62+
_state.global_debugging = debug_logging
63+
_state.raise_on_errors = raise_on_errors
64+
# _state.convert_namedtuples_to_dict = convert_namedtuples_to_dict
6465
# _state.force_namedtuple = bool(force_namedtuple)
6566
_state.log = logger or print
6667
log = _state.log

pymt5adapter/core.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,29 @@
1010
from .state import global_state as _state
1111
from .types import *
1212

13+
# _OMIT_FUNCS_FROM_CONVERSION = []
1314

1415
class MT5Error(Exception):
1516
pass
1617

1718

1819
def _context_manager_modified(f):
1920
@functools.wraps(f)
20-
def wrapper(*args, **kwargs):
21+
def context_manager_modified_wrapper(*args, **kwargs):
2122
result = f(*args, **kwargs)
2223
if _state.global_debugging:
2324
call_sig = f"{f.__name__}({_h.args_to_str(args, kwargs)})"
2425
_state.log(f"[{call_sig}][{last_error()}][{str(result)[:80]}]")
2526
# make sure we log before we raise
26-
if _state.raise_on_errors:
27+
if _state.raise_on_errors and not result: # no need to check last error if we got a result
2728
error_code, description = last_error()
2829
if error_code != _const.RES_S_OK:
2930
raise MT5Error(error_code, description)
31+
# if _state.convert_namedtuples_to_dict and result:
32+
# result = _h.as_dict_all(result)
3033
return result
3134

32-
return wrapper
35+
return context_manager_modified_wrapper
3336

3437

3538
@_context_manager_modified
@@ -167,10 +170,9 @@ def symbols_get(*,
167170
symbols = _mt5.symbols_get(group=group) if group else _mt5.symbols_get()
168171
if symbols is None and _state.raise_on_errors:
169172
build = version()
170-
if build:
171-
if build[1] < _const.MIN_TERMINAL_BUILD :
172-
raise MT5Error(_const.RES_X_TERMINAL_VERSION_OUTDATED,
173-
"The terminal build needs to be updated to support this feature.")
173+
if build and build[1] < _const.MIN_TERMINAL_BUILD :
174+
raise MT5Error(_const.RES_X_TERMINAL_VERSION_OUTDATED,
175+
"The terminal build needs to be updated to support this feature.")
174176
else:
175177
error_code, des = last_error()
176178
if error_code == _const.RES_S_OK:
@@ -215,9 +217,14 @@ def symbol_info_tick(symbol) -> Tick:
215217
:param symbol:
216218
:return:
217219
"""
218-
symbol = _h.any_symbol(symbol)
219-
return _mt5.symbol_info_tick(symbol)
220+
try:
221+
return _mt5.symbol_info_tick(symbol)
222+
except Exception:
223+
symbol = _h.any_symbol(symbol)
224+
return _mt5.symbol_info_tick(symbol)
220225

226+
#direct access to API function without any added overhead
227+
symbol_info_tick_fast = _mt5.symbol_info_tick
221228

222229
@_context_manager_modified
223230
def symbol_select(symbol, enable: bool = True) -> bool:

pymt5adapter/state.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ class _GlobalState:
66

77
def __init__(self):
88
self.__dict__ = self.__shared_state
9-
if 'global_debugging' not in self.__shared_state:
9+
if 'max_bars' not in self.__shared_state:
1010
self.set_defaults()
1111

1212
def set_defaults(self):
13-
self.raise_on_errors = False
13+
# self.convert_namedtuples_to_dict = False
1414
self.global_debugging = False
15+
self.raise_on_errors = False
1516
self.max_bars = 100_000
1617
self.log = print
1718

tests/test_calendar.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_calendar_events():
1515
t2 = perf_counter()
1616
events = calendar_events(time_from=f, time_to=t, importance=Importance.HIGH)
1717
t2 = perf_counter() - t2
18-
assert t2 * 1000 < t1 # verify cache speed
18+
assert t2 * 1000 < t1 # verify cache speed
1919
assert type(events) is list
2020
assert len(events) > 0
2121
for e in events:
@@ -46,8 +46,9 @@ def test_camel_to_snake():
4646
'NItems' : 'n_items'
4747
}
4848
for k, v in controls.items():
49-
assert cal._camel_to_snake(k) == v
50-
49+
res = cal._camel_to_snake(k)
50+
for k, v in controls.items():
51+
assert (res := cal._camel_to_snake(k)) == v
5152

5253
def test_normalize_time():
5354
control_minutes = 15

0 commit comments

Comments
 (0)