Coverage for src/PythonMetaTrader5/legacy/arguments.py: 100%
29 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-12 12:29 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-12 12:29 +0000
1"""2.x argument conventions translated into 3.x values.
3Split out of broker.py (300-line limit); everything here is pure.
4"""
6from __future__ import annotations
8import logging
9from typing import Any
11from PythonMetaTrader5.domain.stops import Stop, StopKind
12from PythonMetaTrader5.result import Err, Ok, Result
14log = logging.getLogger("PythonMetaTrader5.legacy")
17def number(value: Any) -> Any:
18 """Convert numeric strings with float() as 2.x did, raising on garbage as it did.
20 Only strings are converted: float(True) would turn a stray bool into one
21 lot, so every other type goes on unchanged and is checked by the Trader.
22 """
23 return float(value) if isinstance(value, str) else value
26def legacy_stop(value: Any, kind: str) -> Result[Stop | None, str]:
27 if value is None:
28 return Ok(None)
29 match (kind or "absolute").lower():
30 case "absolute":
31 stop_kind = StopKind.PRICE
32 case "offset":
33 stop_kind = StopKind.POINTS
34 case other:
35 # 2.x opened the position with neither SL nor TP here.
36 log.error("invalid stop type %r (use 'absolute' or 'offset')", other)
37 return Err(other)
38 parsed = Stop.parse(value, stop_kind)
39 if isinstance(parsed, Err):
40 # sl=0 meant "no stop" to MT5 in 2.x; refusing it beats an unprotected entry.
41 log.error("%s", parsed.error)
42 return Err(parsed.error.message)
43 return Ok(parsed.value)
46def no_ticket(ticket: int | None) -> bool:
47 if ticket is None:
48 return True
49 # 2.x forwarded it as `position` on an opening deal, which on hedging
50 # accounts turns an opposite-side entry into a close of that position.
51 log.error("the ticket argument is not supported for entries; use Close(ticket=...)")
52 return False