ADR 0008: Refuse input that MT5 would quietly read as something else¶
- Status: accepted (3.0.0)
Context¶
Webhook payloads are loosely typed JSON. Several values that a missing or malformed field produces have a valid but unintended meaning in MT5:
sl=0/tp=0is "no stop", so the position opens unprotected and the call reports success.ticket=0may be read as "no filter" bypositions_get, which in a close would close the whole account.volume=Trueis the number 1, so it trades one lot.- NaN and infinities crashed with
ValueError/OverflowErrordeep inside price rounding, which broke the "every boundary returns aResult" rule.
The empty symbol "" was already refused for the same reason.
Decision¶
Refuse these values at the application boundary, before any terminal call:
- Volume and prices must be positive finite real numbers. Bools are excluded.
numbers.Realis used rather thanfloat, so numpy scalars from pandas-based bots still pass.Decimal, which is not aReal, is accepted explicitly. Stopenforces the same rule on itself in__post_init__: it stores a positive finite float, and a points distance drops its sign. It raisesValueErroron 0, bool, NaN, infinities and strings, because such aStopcannot exist. Plain floats passed assl/tpare checked without raising (stop_input) and come back asINVALID_STOPS. The number rule itself lives once, indomain/amounts.py. An earlier draft stored the raw value and checked it only instop_input. We dropped it because it leftStopwithout invariants,Stop.points(-1000) != Stop.points(1000), and every other consumer ofStopwould have needed its own check.- The price is checked inside
validate_entry, before the symbol lookup and after the side, comment and stops. A bad price never costs a terminal round trip, and a bad side is still the first error reported. - The legacy
BuyLimit/SellLimitcheck the level before comparing it with the quote. Otherwise a level of 0 would "cross" the market and be sent as a market order. - A stop is
None(no stop), a positive finite price or aStop. A stop that resolves to zero or below after tick-grid rounding is refused too. - A ticket is
Noneor a positive integer.
The rules live in domain/inputs.py as plain functions, not a port, because
they do no I/O. The legacy Broker converts numeric strings with float()
first, as 2.x did. It does not convert bools, so True is refused there as
well.
Consequences¶
- 2.x scripts that passed
sl=0to mean "no stop" now getNoneand an error log instead of a trade. MIGRATION.md says so, andNoneis the way to ask for no stop. Stop.points(0)raisesValueErrorwhen it is built, instead of the server rejecting it with 10016.- On the
Resultpaths (sl/tpfloats, volume, prices,Stop.parse), signalling-NaN Decimals and ints too large for a float are refused without raising.float(Decimal("sNaN"))andfloat(10**400)both raise, sopositive_floathandles both. Large but finite floats (1e305) pass that rule, but they overflow on the tick grid (1e305 / 1e-5isinf).SymbolSpec.price_on_gridis the one place every caller-supplied level is put on the grid, and it refuses those as well as levels that snap to 0. Quote prices come from the terminal already on the grid. TheStopconstructors raiseValueErroron any invalid value. - Broken data from the terminal is the same problem on the other side. A
point of 0 put a points stop on the entry price, and a point or tick size
of inf priced orders at NaN. These are the broker's fault and do not clear
on a resend, so they come back as
BROKEN_SYMBOL_DATA.INVALID_*would blame the caller, andTERMINAL_ERRORmeans "never sent, safe to resend" (ADR 0003), which would loop. Every entry callsSymbolSpec.check_tradable()(tick size, step, digits, volume limits), including market orders without stops.points_to_pricealso checkspoint, which only points stops need. The adapter refuses NaN and infinite quotes asNO_PRICE. Closing never runs these checks. We rejected an invariant on the wholeSymbolSpec: it made a position on such a symbol impossible to close, although closing needs only the filling flags.