Skip to content

feat: close-all and cancel-all default to the trader's own magic (ADR 0011) - #16

Merged
Akinzou merged 1 commit into
masterfrom
feat/own-magic-default
Nov 9, 2026
Merged

Akinzou merged 1 commit into
masterfrom
feat/own-magic-default

Conversation

@Akinzou

@Akinzou Akinzou commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Goal

Decided by the package owner: close_positions() and cancel_pending_orders() without a magic must not act on the whole account. Details are in ADR 0011.

What changed

  • Own magic by default. Without a magic, close-all and cancel-all touch only the trader's own magic, also when a symbol is given (close_positions("EURUSD")). The owner's question named only the no-argument call; the symbol case is the same hazard.
  • Whole account. It is spelled magic=ANY_MAGIC. An explicit magic=None still means "own", so a None read from a missing config key cannot widen the call.
  • ticket= is closed whatever its magic. Filtering it out would return Ok([]) with the named position still open.
  • Reads are unchanged. positions() and pending_orders() still list every magic.
  • Validation. A magic must be an integer from 0 to 2**64-1 (MT5's ulong). For anything else the filters return INVALID_ARGUMENT, connect() returns Err, and Trader() raises ValueError.
  • Broker keeps 2.x behaviour. Close and CancelOrders still act on every magic.
  • Never silent. Two cases log a warning:
    • A magic-0 trader runs a close or cancel that affects something. Manual trades also carry magic 0, so they are included; passing magic=0 explicitly acknowledges this.
    • A defaulted close or cancel matches nothing while items with other magics are in scope. The warning says how many were left open; this covers netting, where a bot's deal can merge into a manual position.
  • New module. application/magic_scope.py holds the rule, which keeps trader.py under 300 lines.

Verification

  • 353 tests pass with 99% coverage. ruff, ruff format, mypy --strict and the file-length check are clean.
  • On the demo account (MetaQuotes-Demo, hedging), pytest -m integration passes 8 of 8. New test: a position opened with another magic survives close_positions() and is closed by ticket.

Review history

Three /code-review high rounds were run with a severity filter fixed in advance: an unasked order, a blocked close, an exception escaping a Result boundary, or a regression on this branch. Fixed:

  • Round 1: connect(magic=None) made the default mean "every magic"; this was my bug and is a blocker. Also fixed: the wording of the magic-0 warning, the hedging/RETURN handling in the integration test, AnyMagic export, and passing checked.value to the filter.
  • Round 2:
    • A magic of 2**64 or more passed validation and could overflow in order_send.
    • The magic-0 warning suggested ANY_MAGIC, which widens the call.
    • The warning fired when nothing was affected.
    • The ADR's reason for keeping magic=0 was false: Broker does not use connect().
  • Round 3: on netting, a defaulted close could return Ok([]) in silence, a regression against the old behaviour. It now logs a "left open" warning. Also fixed: the own-magic error message and leftovers from a killed integration run.
  • The final fix commit had no separate review round.

Decision for the owner (not changed here)

  • Should connect() require magic? Today it defaults to 0, which manual trades share, so the protection only works once a bot sets its own magic; until then it warns. Making magic required is cheap before 3.0.0 and a breaking change after it. Recommended, but it changes the public API, so it is the owner's call.

Named debt

  • Netting: the magic filter cannot separate merged volume. This is a property of netting accounts; the case is surfaced by a warning, not detected via margin mode.
  • Read/write asymmetry: positions(magic=None) means all, close_positions(magic=None) means own. This is deliberate and documented.
  • Double validation: a magic is validated twice per close/cancel (resolve, then the query). Both paths use the same function, so they cannot diverge.
  • Integration test filling: the test copies the FOK/IOC/RETURN preference of SymbolSpec.market_filling with raw MT5 flags.
  • Noisy warning: a magic-0 bot logs the manual-trades warning on every defaulted close or cancel that affects something.

Merge

The owner authorised merging this after green CI and testing ("jak zrobisz wszystko zmergujesz i przetestujesz").

… 0011)

Decided by the package owner: close_positions() / cancel_pending_orders()
without a magic touch only the trader's own magic, with or without a symbol.
The whole account is spelled magic=ANY_MAGIC; an explicit magic=None is
still "own", so a None from a missing config key cannot widen the call.
A ticket is closed whatever its magic (filtering it would return Ok([]) for
a position the caller named). positions() / pending_orders() still list
everything. Broker.Close / CancelOrders pass ANY_MAGIC to keep 2.x.

A magic must be an integer from 0 to 2**64-1 (MT5's ulong): filters refuse
"42", True, 1.5, -1 and 2**64 as INVALID_ARGUMENT; connect() returns Err and
Trader() raises ValueError for an own magic that is None or ANY_MAGIC too,
because connect(magic=None) made the "own" default mean "every magic".

The default is never silent about what it cannot know:
- a magic-0 trader (the connect() default, shared with manual trades) logs a
  warning when a defaulted close/cancel affects something; magic=0 passed
  explicitly acknowledges it;
- a defaulted close/cancel that matches nothing while items with other
  magics are in scope logs how many it left open (netting merges deals into
  a position that keeps its first magic).
The rule lives in application/magic_scope.py; trader.py stays under 300 lines.

RED: tests/unit/test_own_magic.py failed for the default, None, ANY_MAGIC,
invalid and out-of-range magics, connect/Trader validation and both
warnings; ticket, explicit magic, Broker parity and symbol scoping passed
before the change and guard it. Close-mechanics tests now give their
positions the trader's own magic (42). Demo account: a position opened with
another magic survives close_positions() and closes by ticket.

Renamed test_cancel_removes_every_pending_order_of_the_symbol to
test_cancel_removes_the_traders_own_pending_orders_of_the_symbol; removed
Trader._own_unless_given / _warn_if_manual (moved to magic_scope). Repo-wide
grep (excluding .venv/.git/caches): no matches.
@Akinzou
Akinzou merged commit f7275da into master Nov 9, 2026
8 checks passed
@Akinzou
Akinzou deleted the feat/own-magic-default branch November 14, 2026 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant