Repository navigation
feat: close-all and cancel-all default to the trader's own magic (ADR 0011) - #16
Merged
Merged
Conversation
… 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Goal
Decided by the package owner:
close_positions()andcancel_pending_orders()without a magic must not act on the whole account. Details are in ADR 0011.What changed
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.magic=ANY_MAGIC. An explicitmagic=Nonestill means "own", so aNoneread from a missing config key cannot widen the call.ticket=is closed whatever its magic. Filtering it out would returnOk([])with the named position still open.positions()andpending_orders()still list every magic.INVALID_ARGUMENT,connect()returnsErr, andTrader()raisesValueError.Brokerkeeps 2.x behaviour.CloseandCancelOrdersstill act on every magic.magic=0explicitly acknowledges this.application/magic_scope.pyholds the rule, which keepstrader.pyunder 300 lines.Verification
pytest -m integrationpasses 8 of 8. New test: a position opened with another magic survivesclose_positions()and is closed byticket.Review history
Three
/code-review highrounds 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: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,AnyMagicexport, and passingchecked.valueto the filter.order_send.ANY_MAGIC, which widens the call.magic=0was false:Brokerdoes not useconnect().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.Decision for the owner (not changed here)
connect()requiremagic? 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. Makingmagicrequired 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
positions(magic=None)means all,close_positions(magic=None)means own. This is deliberate and documented.SymbolSpec.market_fillingwith raw MT5 flags.Merge
The owner authorised merging this after green CI and testing ("jak zrobisz wszystko zmergujesz i przetestujesz").