Coverage for src/PythonMetaTrader5/domain/amounts.py: 100%
14 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"""The one rule for "a positive finite number", shared by Stop and the input checks."""
3from __future__ import annotations
5import math
6from decimal import Decimal
7from numbers import Real
10def positive_float(value: object, *, unsigned: bool = False) -> float | None:
11 """``value`` as a positive finite float, or None when it is not one.
13 numbers.Real rather than float, so numpy scalars from pandas-based bots
14 pass, plus Decimal (not a Real) for bots that keep money as Decimal. bool
15 is excluded because it is a Real (True == 1) and would trade one lot.
16 NaN Decimals are refused before float(): float(Decimal("sNaN")) raises.
17 ``unsigned`` drops the sign first (a distance), after the NaN check, because
18 abs(Decimal("sNaN")) raises too.
19 """
20 if isinstance(value, bool) or not isinstance(value, (Real, Decimal)):
21 return None
22 if isinstance(value, Decimal) and value.is_nan():
23 return None
24 try:
25 number = abs(float(value)) if unsigned else float(value)
26 except ArithmeticError:
27 # None rather than raising: a number outside float range (10**400, or a
28 # numpy longdouble under np.seterr(over="raise")) is bad input like NaN,
29 # and raising would break the Result boundary. ValueError/TypeError are
30 # not caught: they are bugs in the caller's own type and must keep
31 # their traceback.
32 return None
33 return number if math.isfinite(number) and number > 0 else None