Coverage for src/PythonMetaTrader5/__init__.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-12 12:29 +0000

1"""PythonMetaTrader5: a safe, typed trading layer over the MetaTrader5 package. 

2 

3from PythonMetaTrader5 import connect, Stop, Ok, Err 

4 

5match connect(12345678, "password", "MetaQuotes-Demo", magic=1001): 

6 case Ok(trader): 

7 trader.open_position("EURUSD", "buy", 0.1, sl=Stop.points(1000)) 

8 case Err(error): 

9 print(error) 

10""" 

11 

12from typing import Any 

13 

14# `X as X`: explicit re-exports, because __all__ is computed (see __getattr__). 

15from PythonMetaTrader5.application.batch import BatchFailure as BatchFailure 

16from PythonMetaTrader5.application.execution import RetryPolicy as RetryPolicy 

17from PythonMetaTrader5.application.trader import Trader as Trader 

18from PythonMetaTrader5.connection import connect as connect 

19from PythonMetaTrader5.domain.errors import ErrorKind as ErrorKind 

20from PythonMetaTrader5.domain.errors import TradeError as TradeError 

21from PythonMetaTrader5.domain.inputs import ANY_MAGIC as ANY_MAGIC 

22from PythonMetaTrader5.domain.inputs import AnyMagic as AnyMagic 

23from PythonMetaTrader5.domain.market import PendingKind as PendingKind 

24from PythonMetaTrader5.domain.market import PendingOrder as PendingOrder 

25from PythonMetaTrader5.domain.market import Position as Position 

26from PythonMetaTrader5.domain.market import Tick as Tick 

27from PythonMetaTrader5.domain.market import TradeReceipt as TradeReceipt 

28from PythonMetaTrader5.domain.orders import Side as Side 

29from PythonMetaTrader5.domain.stops import Stop as Stop 

30from PythonMetaTrader5.domain.stops import StopKind as StopKind 

31from PythonMetaTrader5.legacy import mt5_namespace as _mt5_namespace 

32from PythonMetaTrader5.legacy.broker import Broker as Broker 

33from PythonMetaTrader5.result import Err as Err 

34from PythonMetaTrader5.result import Ok as Ok 

35from PythonMetaTrader5.result import Result as Result 

36 

37_OWN_NAMES = ( 

38 "ANY_MAGIC", 

39 "AnyMagic", 

40 "BatchFailure", 

41 "Broker", 

42 "Err", 

43 "ErrorKind", 

44 "Ok", 

45 "PendingKind", 

46 "PendingOrder", 

47 "Position", 

48 "Result", 

49 "RetryPolicy", 

50 "Side", 

51 "Stop", 

52 "StopKind", 

53 "Tick", 

54 "TradeError", 

55 "TradeReceipt", 

56 "Trader", 

57 "connect", 

58) 

59 

60 

61def __getattr__(name: str) -> Any: 

62 # 2.x re-exported the whole MetaTrader5 namespace and its README relied on it 

63 # (`symbol_info("EURUSD")` after a star import), so those names resolve here, 

64 # lazily (ADR 0010). A star import asks for __all__ through this hook too. 

65 # Names defined in this module always win, e.g. our Tick over MT5's struct. 

66 if name == "__all__": 

67 mt5_names = _mt5_namespace.public_names() 

68 return [*_OWN_NAMES, *(n for n in mt5_names if n not in globals())] 

69 return _mt5_namespace.resolve(name) 

70 

71 

72def __dir__() -> list[str]: 

73 return sorted({*globals(), *__getattr__("__all__")})