@devries-cameron20 / open-alice / commits / 285f262
Fork 0

Alice — an open-source AI trading bot covering equities, crypto, commodities, forex, and macro

Update mt5.py

Cameron “Camo” De Vries committed Nov 6, 2026 at 10:19 UTC 285f2620235a52ddf83000ebc91f4b791e50975e
1 file changed +43 -1
src/open_alice/broker/mt5.py
+43 -1
@@ -85,6 +85,7 @@ class MT5Broker:
85 self.terminal_path = terminal_path
86 self.connected = False
87 self.account_info: MT5AccountInfo | None = None
88 + self._demo_identity: tuple[int, str] | None = None
89
90 def connect(self) -> bool:
91 """Initialize connection to the MT5 terminal.
@@ -108,7 +109,12 @@ class MT5Broker:
109 return False
110
111 self.connected = True
111 - self.account_info = self._get_account_info()
112 + try:
113 + self._assert_demo_account(pin=True)
114 + self.account_info = self._get_account_info()
115 + except Exception:
116 + self.disconnect()
117 + raise
118 print(f"Connected to MT5 account #{self.account_info.login}")
119 return True
120
@@ -119,6 +125,8 @@ class MT5Broker:
125 mt5 = _load_mt5()
126 mt5.shutdown()
127 self.connected = False
128 + self._demo_identity = None
129 + self.account_info = None
130 print("Disconnected from MT5")
131
132 def _require_connected(self) -> Any:
@@ -126,6 +134,38 @@ class MT5Broker:
134 raise ConnectionError("Not connected to MT5. Call connect() first.")
135 return _load_mt5()
136
137 + def _assert_demo_account(self, *, pin: bool = False) -> None:
138 + """Fail closed on real/unknown accounts and terminal account switches.
139 +
140 + This build supports demo accounts only. Server names and CLI flags are
141 + not evidence of account type. Re-read immediately before every send.
142 + """
143 + mt5 = self._require_connected()
144 + info = mt5.account_info()
145 + mode = getattr(info, "trade_mode", None)
146 + demo_mode = getattr(mt5, "ACCOUNT_TRADE_MODE_DEMO", None)
147 + login = getattr(info, "login", None)
148 + server = getattr(info, "server", None)
149 + if (
150 + type(mode) is not int
151 + or type(demo_mode) is not int
152 + or mode != demo_mode
153 + or type(login) is not int
154 + or login <= 0
155 + or not isinstance(server, str)
156 + or not server
157 + ):
158 + raise RuntimeError("Demo-only safety: account type or identity is not verified demo")
159 + identity = (login, server)
160 + if self.login is not None and login != self.login:
161 + raise RuntimeError("Demo-only safety: login does not match requested account")
162 + if self.server is not None and server != self.server:
163 + raise RuntimeError("Demo-only safety: server does not match requested account")
164 + if pin:
165 + self._demo_identity = identity
166 + elif self._demo_identity is None or identity != self._demo_identity:
167 + raise RuntimeError("Demo-only safety: terminal account changed; reconnect required")
168 +
169 def _get_account_info(self) -> MT5AccountInfo:
170 """Fetch account information from MT5."""
171 mt5 = self._require_connected()
@@ -258,6 +298,7 @@ class MT5Broker:
298 "type_time": mt5.ORDER_TIME_GTC,
299 "type_filling": symbol_info.get("filling_mode", mt5.ORDER_FILLING_IOC),
300 }
301 + self._assert_demo_account()
302 result = mt5.order_send(request)
303 if result is None or result.retcode != mt5.TRADE_RETCODE_DONE:
304 code = result.retcode if result is not None else mt5.last_error()
@@ -329,6 +370,7 @@ class MT5Broker:
370 "type_time": mt5.ORDER_TIME_GTC,
371 "type_filling": mt5.ORDER_FILLING_IOC,
372 }
373 + self._assert_demo_account()
374 result = mt5.order_send(request)
375 if result is None or result.retcode != mt5.TRADE_RETCODE_DONE:
376 code = result.retcode if result is not None else mt5.last_error()